這是應用程序的代碼。我一直在嘗試使用eclipse IDE來運行它。我還添加了所有必需的java郵件jar文件,即 dsn.jar,imap.jar,mailapi.jar,pop3.jar,smtp.jar,mail.jar
。 但它給出了以下錯誤Could not connect to SMTP host: smtp.gmail.com, port: 587
。無法連接到SMTP主機:smtp.gmail.com,端口:587;嵌套異常是:java.net.ConnectException:連接超時:連接
沒有防火牆阻止訪問,因爲在ping smtp.gmail.com時收到回覆。 我甚至嘗試了這種方式:
- 首先登錄在設備上的瀏覽器的Gmail帳戶,您要設置/使用客戶端
- 轉到這裏,並啓用「不太安全」的應用程序訪問:https://www.google.com/settings/security/lesssecureapps
- 然後轉到此處:https://accounts.google.com/b/0/DisplayUnlockCaptcha然後單擊繼續。
- 然後立即回到您的客戶端並重試。
javax.mail.MessagingException的:無法連接到SMTP主機: smtp.gmail.com,端口:587; 嵌套的異常是: java.net.ConnectException:連接超時:連接 at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972) at com.sun.mail.smtp.SMTPTransport.protocolConnect (SMTPTransport.java:642) at javax.mail.Service.connect(Service.java:317) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service的.java:125) 在javax.mail.Transport.send0(Transport.java:194) 在javax.mail.Transport.send(Transport.java:124) 在PlainTextEmailSender.sendPlainTextEmail(PlainTextEmailSender.java:50) 在PlainTextEmailSender.main(PlainTextEmailSender.java:73) 導致:java.net.ConnectException:連接超時:連接 在java.net.DualStackPlainSocketImpl.connect0(本地方法) 在java.net.DualStackPlainSocketImpl.socketConnect(未知源) 在java.net.AbstractPlainSocketImpl.doConnect (未知來源) 在java.net.AbstractPlainSocketImpl.connectToAddress(未知來源) 在java.net.AbstractPlainSocketImpl.connect(未知來源) 在java.net.PlainSocketImpl.connect(未知來源) 在java.net.SocksSocketImpl (Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java: 319) 在com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233) 在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
package net.codejava.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class PlainTextEmailSender {
public void sendPlainTextEmail(String host, String port,
final String userName, final String password, String toAddress,
String subject, String message) throws AddressException,
MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// set plain text message
msg.setText(message);
// sends the e-mail
Transport.send(msg);
}
/**
* Test the send e-mail method
*
*/
public static void main(String[] args) {
// SMTP server information
String host = "smtp.gmail.com";
String port = "587";
String mailFrom = "user_name";
String password = "password";
// outgoing message information
String mailTo = "email_address";
String subject = "Hello my friend";
String message = "Hi guy, Hope you are doing well. Duke.";
PlainTextEmailSender mailer = new PlainTextEmailSender();
try {
mailer.sendPlainTextEmail(host, port, mailFrom, password, mailTo,
subject, message);
System.out.println("Email sent.");
} catch (Exception ex) {
System.out.println("Failed to sent email.");
ex.printStackTrace();
}
}
}
不,我沒有嘗試連接使用telnet客戶端,但我用cmd來檢查ping響應是否發送到smtp.gmail.com的ping請求。它在那裏工作得很好。 –
正如你所說,我已經爲異常提供了完整的堆棧跟蹤。如果你能幫助我,這將是一件好事。 –
其實我想在我的項目中使用它,我想發送一個隨機生成的用戶ID和密碼到特定用戶的電子郵件ID。但是,這個郵件的東西不工作:/ –