所以,我的問題如下:我有一個我用Java編寫的郵件客戶端,在用POP3檢查郵件後,我無法通過SMTP發送郵件。Javamail問題:檢查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);
}
在此處發佈代碼 – skaffman
屬性位於單獨的行中,但此stachoverflow將其弄亂了一點點:) – Hectai
您只需要瞭解如何在此格式化代碼。從您的編輯器中複製代碼(正確編寫),選擇它並按下CODE按鈕(使用010101)或按下Ctrl + K。在消息編輯器下方,您可以看到消息預覽。 – BalusC