【发布时间】:2016-12-01 04:14:22
【问题描述】:
这是应用程序的代码。我一直在尝试使用 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。
没有防火墙阻止访问,因为在 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:连接超时:连接 在 com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972) 在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642) 在 javax.mail.Service.connect(Service.java:317) 在 javax.mail.Service.connect(Service.java:176) 在 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.connect(未知来源) 在 java.net.Socket.connect(未知来源) 在 java.net.Socket.connect(未知来源) 在 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。但是这封邮件不起作用:/