这是应用程序的代码。我一直在尝试使用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。但是,这个邮件的东西不工作:/ –