我想使用Gmail的SMTP服務器發送郵件。你能告訴我爲什麼當我運行波紋管代碼時它不會連接到服務器。無法使用javax.mail發送到Gmail SMTP
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
public class SendTrick {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "465");
props.put("mail.from", "[email protected]");
props.put("mail.smtp.host", "smtp.gmail.com");
Session session = Session.getInstance(props, null);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
"[email protected]");
msg.setSubject("JavaMail hello world example");
msg.setText("Hello, world!\n");
Transport.send(msg);
} catch (MessagingException mex) {
System.out.println("send failed, exception: " + mex);
}
}
}
日誌中的例外是
發送失敗,異常:javax.mail.MessagingException的:無法 連接到SMTP主機:smtp.gmail.com端口:25;嵌套的例外是: java.net.ConnectException:連接被拒絕:連接
請提供一個堆棧跟蹤。 –
仔細看看你的屬性地圖,特別是'mail.smtp.host' ... – home
你的代碼似乎沒有被服務器認證。這是一個故意的涵義嗎? –