我想使用Java發送郵件,但它不工作使用本教程泰國我發現這裏http://www.tutorialspoint.com/java/java_sending_email.htm。不能使用JavaMail發送任何郵件
這是我的主要方法
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("smtp.laposte.net", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
而且我有以下錯誤:
javax.mail.MessagingException的:無法連接到SMTP主機:本地主機,端口:25; 嵌套的異常是: java.net.ConnectException:連接被拒絕:連接 在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972) at com.sun.mail.smtp.SMTPTransport.protocolConnect( (Service.java:180)上的javax.mail.Service.connect(Service.java:176) 處的javax.mail.Service.connect(Service.java:295) 上的SMTPTransport.java:642) 。的java:125) 在javax.mail.Transport.send0(Transport.java:194) 在javax.mail.Transport.send(Transport.java:124) 在javamail.JavaMail.main(JavaMail.java:58) 引起:java.net.ConnectException:連接被拒絕:在java.net.DualStackPlainSocke處連接 tImpl.connect0(本機方法) 在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) 在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 在java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl .java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java (Socket.java:538) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher .java:319) at com.sun.mail.util.Socke tFetcher.getSocket(SocketFetcher.java:233) 在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938) ... 7更 生成成功(總時間:1秒)
然後在網絡上找到另一個有效的例子嗎? –
嘗試以下Tipu Sultan建議的內容。如果這不起作用,那麼在網絡上找到另一個工作的例子:P –
也是,你知道在你的程序中你必須把正確的Gmail用戶ID和正確的密碼? –