Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 电子邮件客户端的连接失败_Java_Email_Client_Jakarta Mail - Fatal编程技术网

Java 电子邮件客户端的连接失败

Java 电子邮件客户端的连接失败,java,email,client,jakarta-mail,Java,Email,Client,Jakarta Mail,我连接电子邮件服务器时出错。我认为问题在于连接,因为从服务器下载消息总是失败 我的部分代码: // Connect to e-mail server. public void connect() { // Display connect dialog. ConnectDialog dialog = new ConnectDialog(this); dialog.show(); // Build connection URL from connect dialog

我连接电子邮件服务器时出错。我认为问题在于连接,因为从服务器下载消息总是失败

我的部分代码:

// Connect to e-mail server.
public void connect() {
    // Display connect dialog.
    ConnectDialog dialog = new ConnectDialog(this);
    dialog.show();

    // Build connection URL from connect dialog settings.
    StringBuffer connectionUrl = new StringBuffer();
    connectionUrl.append(dialog.getTypes() + "://");
    connectionUrl.append(dialog.getUsername() + ":");
    connectionUrl.append(dialog.getPassword() + "@");
    connectionUrl.append(dialog.getServer() + "/");

/* Display dialog stating that messages are
   currently being downloaded from server. */
    final DownloadingDialog downloadingDialog =
            new DownloadingDialog(this);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            downloadingDialog.show();
        }
    });

    // Establish JavaMail session and connect to server.
    Store store = null;
    try {
        // Initialize JavaMail session with SMTP server.
        Properties props = new Properties();
        props.put("mail.smtp.host", dialog.getSmtpServer());
        session = Session.getDefaultInstance(props, null);

        // Connect to e-mail server.
        URLName urln = new URLName(connectionUrl.toString());
        store = session.getStore(urln);
        store.connect();
    } catch (Exception e) {
        // Close the downloading dialog.
        downloadingDialog.dispose();

        // Show error dialog.
        showError("Unable to connect.", true);
    }

    // Download message headers from server.
    try {
        // Open main "INBOX" folder.
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);

        // Get folder's list of messages.
        Message[] messages = folder.getMessages();

        // Retrieve message headers for each message in folder.
        FetchProfile profile = new FetchProfile();

        profile.add(FetchProfile.Item.ENVELOPE);
        folder.fetch(messages, profile);

        // Put messages in table.
        tableModel.setMessages(messages);
    } catch (Exception e) {
        // Close the downloading dialog.
        downloadingDialog.dispose();

        // Show error dialog.
        showError("Unable to download messages.", true);
    }

    // Close the downloading dialog.
    downloadingDialog.dispose();
}

// Show error dialog and exit afterwards if necessary.
private void showError(String message, boolean exit) {
    JOptionPane.showMessageDialog(this, message, "Error",
            JOptionPane.ERROR_MESSAGE);
    if (exit)
        System.exit(0);
}

// Get a message's content.
public static String getMessageContent(Message message)
throws Exception {
    Object content = message.getContent();
    if (content instanceof Multipart) {
        StringBuffer messageContent = new StringBuffer();
        Multipart multipart = (Multipart) content;
        for (int i = 0; i < multipart.getCount(); i++) {
            Part part = (Part) multipart.getBodyPart(i);
            if (part.isMimeType("text/plain")) {
                messageContent.append(part.getContent().toString());
            }
        }
        return messageContent.toString();
    } else {
        return content.toString();
    }
}

// Run the E-mail Client.
public static void main(String[] args) {
    EmailClient2 client = new EmailClient2();
    client.show();

    // Display connect dialog.
    client.connect();
}
//连接到电子邮件服务器。
公共void connect(){
//显示“连接”对话框。
ConnectDialog=新建ConnectDialog(此);
dialog.show();
//从“连接”对话框设置生成连接URL。
StringBuffer connectionUrl=新的StringBuffer();
connectionUrl.append(dialog.getTypes()+“:/”;
connectionUrl.append(dialog.getUsername()+“:”);
connectionUrl.append(dialog.getPassword()+“@”);
connectionUrl.append(dialog.getServer()+“/”;
/*显示对话框,说明消息是
当前正在从服务器下载*/
最终下载对话框下载对话框=
新建下载对话框(此对话框);
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
下载dialog.show();
}
});
//建立JavaMail会话并连接到服务器。
Store=null;
试一试{
//使用SMTP服务器初始化JavaMail会话。
Properties props=新属性();
put(“mail.smtp.host”,dialog.getSmtpServer());
session=session.getDefaultInstance(props,null);
//连接到电子邮件服务器。
URLName urln=新的URLName(connectionUrl.toString());
store=session.getStore(urln);
store.connect();
}捕获(例外e){
//关闭下载对话框。
下载dialog.dispose();
//显示错误对话框。
淋浴ROR(“无法连接”,true);
}
//从服务器下载消息头。
试一试{
//打开主“收件箱”文件夹。
Folder Folder=store.getFolder(“收件箱”);
文件夹。打开(文件夹。读写);
//获取文件夹的邮件列表。
Message[]messages=folder.getMessages();
//检索文件夹中每个邮件的邮件标题。
FetchProfile profile=新的FetchProfile();
profile.add(FetchProfile.Item.信封);
文件夹。获取(消息、配置文件);
//将消息放入表中。
tableModel.setMessages(消息);
}捕获(例外e){
//关闭下载对话框。
下载dialog.dispose();
//显示错误对话框。
淋浴ROR(“无法下载邮件”,true);
}
//关闭下载对话框。
下载dialog.dispose();
}
//显示错误对话框并在必要时退出。
私有无效错误(字符串消息,布尔退出){
showMessageDialog(此消息为“错误”,
JOptionPane.ERROR\u消息);
如果(退出)
系统出口(0);
}
//获取消息的内容。
公共静态字符串getMessageContent(消息消息)
抛出异常{
对象内容=message.getContent();
if(多部分的内容实例){
StringBuffer messageContent=新建StringBuffer();
多部分多部分=(多部分)内容;
对于(int i=0;i
}

运行应用程序时:

服务器:pop.gmail.com 用户名:********@gmail.com 密码:************* smtp服务器:smtp.gmail.com


我的错误消息“无法连接”

在您的
catch
块中,最好实际打印
异常
,以便查看发生了什么wrong@ScaryWombat我想你是对的,但是我的代码中没有错误,也没有显示异常。因为只有失败的连接。或者通过我的互联网连接?JavaMail常见问题解答已经发布。