JavaMail 和 Hotmail 无法在 Android 上运行

发布于 2024-09-19 18:34:13 字数 2259 浏览 10 评论 0原文

我在使用 JavaMail 从 hotmail 地址发送电子邮件时遇到问题。我验证了我可以通过 telnet 端口 587 连接到 smtp.live.com。有趣的事情(对我来说)是如果我更改:

host = "smtp.gmail.com" t.connect(主机、用户名、密码);

它在默认端口上可以正常连接到 Gmail 并发送电子邮件。

但如果我将代码更改为:

host = "smtp.live.com" t.connect(主机,587,用户名,密码); 它给我以下错误:

javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.live.com,端口:587;

嵌套异常是:

java.io.IOException:SSL握手失败:SSL库中失败,通常是协议错误

错误:140770FC:SSL例程:SSL23_GET_SERVER_HELLO:未知协议(external / openssl / ssl / s23_clnt.c:604 0xaf076228:0x00000000)

使用 session.setDebug(true) 我得到以下信息:

09-15 01:57:37.280: INFO/System.out(720): DEBUG: getProvider() 返回 javax.mail.Provider[TRANSPORT,smtps,com.sun. mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc.,1.4.1] 09-15 01:57:37.300: INFO/System.out(720): 调试 SMTP: useEhlo true, useAuth true 09-15 01:57:37.310: INFO/System.out(720): DEBUG SMTP: 尝试连接到主机“smtp.live.com”,端口 587,isSSL true 09-15 01:57:37.330:信息/SSLSocketFactory(720):使用工厂org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl@4007ed70 09-15 01:57:37.490: 调试/NativeCrypto(720): SSL_OP_NO_SSLv3 已设置 09-15 01:57:37.538: ERROR/NativeCrypto(720): 连接期间出现未知错误 1

​​看起来 Hotmail 与 OpenSSL 的配合不太好。有人有解决方案吗?

下面是我的代码...以防万一它有帮助。

预先感谢,

J

String host = "smtp.live.com";

String username = foo@hotmail; 

String password = "**"; 

Transport t = null;

Properties props = new Properties();

props.put("mail.smtps.auth", "true");

//props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

Session session = Session.getInstance(props);

session.setDebug(true);

try{

MimeMessage msg = new MimeMessage(session);

msg.setSubject("Testing SMTP-SSL");

msg.setContent("This is a test", "text/plain");

msg.setFrom(new InternetAddress(username));


msg.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(username, false));

t = session.getTransport("smtps");

t.connect(host,587, username, password);

t.sendMessage(msg, msg.getAllRecipients());

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

t.close();

} catch (MessagingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} 

I am having trouble sending emails from a hotmail address using JavaMail. I verified that I can connect to smtp.live.com via telnet port 587. The interesting thing (to me) is if I change:

host = "smtp.gmail.com"
t.connect(host, username, password);

It connects to Gmail just fine on the default port and sends an email.

But if I change the code to:

host = "smtp.live.com"
t.connect(host,587, username, password);
It gives me the following error:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.live.com, port: 587;

nested exception is:

java.io.IOException: SSL handshake failure: Failure in SSL library, usually a protocol error

error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:604 0xaf076228:0x00000000)

With session.setDebug(true) I get this info:

09-15 01:57:37.280: INFO/System.out(720): DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc.,1.4.1]
09-15 01:57:37.300: INFO/System.out(720): DEBUG SMTP: useEhlo true, useAuth true
09-15 01:57:37.310: INFO/System.out(720): DEBUG SMTP: trying to connect to host "smtp.live.com", port 587, isSSL true
09-15 01:57:37.330: INFO/SSLSocketFactory(720): Using factory org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl@4007ed70
09-15 01:57:37.490: DEBUG/NativeCrypto(720): SSL_OP_NO_SSLv3 is set
09-15 01:57:37.538: ERROR/NativeCrypto(720): Unknown error 1 during connect

Looks like Hotmail isn't playing nice with OpenSSL. Does anyone have a solution for this?

Below is my code in...just in case it helps.

Thanks in advance,

J

String host = "smtp.live.com";

String username = foo@hotmail; 

String password = "**"; 

Transport t = null;

Properties props = new Properties();

props.put("mail.smtps.auth", "true");

//props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

Session session = Session.getInstance(props);

session.setDebug(true);

try{

MimeMessage msg = new MimeMessage(session);

msg.setSubject("Testing SMTP-SSL");

msg.setContent("This is a test", "text/plain");

msg.setFrom(new InternetAddress(username));


msg.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(username, false));

t = session.getTransport("smtps");

t.connect(host,587, username, password);

t.sendMessage(msg, msg.getAllRecipients());

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

t.close();

} catch (MessagingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2024-09-26 18:34:13

我在向 Hotmail/Outlook 发送电子邮件时遇到了同样的问题...

我通过在属性中将套接字工厂端口始终添加到 578 来解决它,例如:

props.put("mail.smtp.socketFactory.port", "587");

对于 hotmail 情况,端口是 25。

props.put("mail.smtp.port", "25");

有点晚了,但也许有帮助;)

I had the same problem with sending emails to Hotmail/Outlook...

I solved it by adding the socket factory port always to 578 in your properties like:

props.put("mail.smtp.socketFactory.port", "587");

and for hotmail case the port is 25.

props.put("mail.smtp.port", "25");

A bit late but maybe it helps ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文