java邮件smtp连接问题
我正在尝试下面的示例,使用从 此处 选取的 ssl 发送邮件,并更改为我的凭据如下并得到如图所示的异常,您能帮我解决
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class SimpleSSLMail {
private static final String SMTP_HOST_NAME = "xx.xx.xx.xx";
private static final int SMTP_HOST_PORT = 25 ;//465;
private static final String SMTP_AUTH_USER = "[email protected]";
private static final String SMTP_AUTH_PWD = "xxxx";
public static void main(String[] args) throws Exception{
new SimpleSSLMail().test();
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
// props.put("mail.smtps.quitwait", "false");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("[email protected]"));
transport.connect
(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
异常吗:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host:xx.xx.xx.xx, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
出现此类问题的原因是网络问题。可能您需要身份验证,您可能有错误的用户名、密码。
不管怎样,你的代码中遗漏了很多东西。
获得与 gmail 服务器的正确 SSL 连接的示例
我在您的代码中没有看到这一点。那么验证方法在哪里?
只需查看以下链接:
在 Java 中使用 Gmail 发送电子邮件 这里您可以选择使用 TLS 或 SSl
Java 邮件 API
通过 Java 发送电子邮件
java邮件系统中使用的按键供大家参考
This type of problem occurs due to a network problem . May be you require authentication, you may have wrong username , password .
Anyways you are missing in a lot of things in your code.
Example to obtain a proper SSL connection with gmail server
I don't see this any where in your code. Then where is the method to authenticate ?
Just check out the following links :
Sending E-mail Using Gmail In Java Here you have a choice of using TLS or SSl
Java Mail API
E-mailing through Java
keys used in java mailing system for your reference