Javamail问题:用POP3查看邮件后无法通过SMTP发送邮件
所以,我的问题如下:我有一个用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("[email protected]"));
message.setContent("<h1>Hello world</h1>", "text/html");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("[email protected]"));
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);
}
So, my problem is the following: I have a mail client that I wrote in Java and I cannot send mails through SMTP after I check my mails with POP3.
The exception I caught says that the transport protocol = null.
The code is working fine, because I have no issue before POP3 connection. I am sure I close that connection and they are all private functions, so the variables are not effective on each other.
Hope I told everything.
Thanks for any idea.
The codes:
pop3 connection
// 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 - mail sending
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("[email protected]"));
message.setContent("<h1>Hello world</h1>", "text/html");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("[email protected]"));
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 pop 和 smtp 模块不是独立的:它们共享默认会话。您最好创建自己的会话,一个用于 pop,一个用于 smtp,而不是依赖 javamail 的默认会话 (Session.getDefaultInstance)。
Your pop and smtp modules not independent: they are sharing the default session. Instead of relying on javamail's default session (Session.getDefaultInstance), you'd be better off creating your own sessions, one for pop and one for smtp.