JavaMail和Hotmail无法在Android上运行

时间:2010-09-16 00:37:13

标签: smtp javamail hotmail

我无法使用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,port:587;

嵌套异常是:

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

错误:140770FC:SSL例程:SSL23_GET_SERVER_HELLO:未知协议(外部/ 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,公司,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:尝试连接主机“smtp.live.com”,端口587,isSSL true 09-15 01:57:37.330:INFO / SSLSocketFactory(720):使用工厂org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl@4007ed70 09-15 01:57:37.490:DEBUG / NativeCrypto(720):设置SSL_OP_NO_SSLv3 09-15 01:57:37.538:ERROR / NativeCrypto(720):连接时出现未知错误

看起来Hotmail与OpenSSL的搭配并不好。有人有解决方案吗?

以下是我的代码......以防它有用。

提前致谢,

Ĵ

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();

}

} 

1 个答案:

答案 0 :(得分:0)

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

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

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

对于hotmail案例,端口是25。

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

有点晚但也许有帮助;)

相关问题