Javamail问题:在使用POP3检查邮件后,无法通过SMTP发送邮件

Javamail问题:在使用POP3检查邮件后,无法通过SMTP发送邮件,java,smtp,jakarta-mail,pop3,Java,Smtp,Jakarta Mail,Pop3,所以,我的问题是:我有一个用Java编写的邮件客户端,在我用POP3检查邮件后,我无法通过SMTP发送邮件 我捕获的异常表示传输协议=null 代码运行良好,因为我在POP3连接之前没有问题。我确信我关闭了这个连接,它们都是私有函数,所以变量之间并没有相互作用 希望我把一切都告诉你了 谢谢你的建议 代码: pop3连接 // Connect to the POP3 server Session session = Session.getDefaultInstance(props, nul

所以,我的问题是:我有一个用Java编写的邮件客户端,在我用POP3检查邮件后,我无法通过SMTP发送邮件

我捕获的异常表示传输协议=null

代码运行良好,因为我在POP3连接之前没有问题。我确信我关闭了这个连接,它们都是私有函数,所以变量之间并没有相互作用

希望我把一切都告诉你了

谢谢你的建议

代码:

pop3连接

  // Connect to the POP3 server
  Session session = Session.getDefaultInstance(props, null);
  Store store = session.getStore("pop3");
  store.connect(host, username, password);

  // Open the folder
  Folder inbox = store.getFolder("INBOX");

  inbox.open(Folder.READ_ONLY);

  // Get the messages from the server
  Message[] messages = inbox.getMessages();


  fromMailAddress.setText(userAccount);

  // Close the connection
  // but don't remove the messages from the server
  inbox.close(false);
  store.close();
  props.clear();
    }
catch (Exception ex) {
     JOptionPane.showMessageDialog(null,"user input error", "error", JOptionPane.ERROR_MESSAGE);


    }
smtp-邮件发送

    Properties property = new Properties();
    property.setProperty("mail.transport.protocol", "smtp");
    property.setProperty("mail.host", "mymailserver");
    property.setProperty("mail.user", "myusername");
    property.setProperty("mail.password", "mypassword");
    Session mailSession = Session.getDefaultInstance(property, null);
    mailSession.setDebug(true);
try{
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setSubject("HTML  mail with images");
    message.setFrom(new InternetAddress("myaddress@gmail.com"));
    message.setContent("<h1>Hello world</h1>", "text/html");
    message.addRecipient(Message.RecipientType.TO,
    new InternetAddress("xyz@gmail.com"));

    transport.connect();
    transport.sendMessage(message,
        message.getRecipients(Message.RecipientType.TO));
    transport.close();
    property.clear();
    }
    catch(Exception ex)
    {

            JOptionPane.showMessageDialog(null,"e-mail sending failed", "Error", JOptionPane.ERROR_MESSAGE);
    }
Properties属性=新属性();
setProperty(“mail.transport.protocol”、“smtp”);
setProperty(“mail.host”、“mymailserver”);
setProperty(“mail.user”、“myusername”);
setProperty(“mail.password”、“mypassword”);
Session mailSession=Session.getDefaultInstance(属性,null);
mailSession.setDebug(true);
试一试{
传输=mailSession.getTransport();
mimessage message=新mimessage(mailSession);
message.setSubject(“带有图像的HTML邮件”);
message.setFrom(新的InternetAddress(“myaddress@gmail.com"));
message.setContent(“helloworld”、“text/html”);
message.addRecipient(message.RecipientType.TO,
新因特网地址(“xyz@gmail.com"));
transport.connect();
transport.sendMessage(消息,
message.getRecipients(message.RecipientType.TO));
transport.close();
property.clear();
}
捕获(例外情况除外)
{
JOptionPane.showMessageDialog(null,“电子邮件发送失败”,“错误”,JOptionPane.Error\u消息);
}

您的pop和smtp模块不独立:它们共享默认会话。与其依赖javamail的默认会话(session.getDefaultInstance),不如创建自己的会话,一个用于pop,一个用于smtp。

属性在单独的行中,但这个流有点搞砸了:)您只需要在这里学习如何格式化代码。从编辑器中复制粘贴(正确意图)代码,选择它并按代码按钮(使用010101)或按Ctrl+K。在消息编辑器下方,您可以看到消息的预览。好的,下次我会。现在我正在等待想法,为什么我不能在pop3之后发送邮件…我想知道您是否可以给我一个创建自己会话的示例,这将非常有用,谢谢。请尝试session.getInstance(属性)。您将希望保持会话的状态(而不是每次调用getInstance)。