以非Gmail帐户编程方式发送电子邮件出错

3
我正在尝试从我的Android应用程序中以编程方式发送电子邮件,使用我从这里获取的一些已知方法。该方法在使用gmail帐户时工作正常,但是当我尝试使用不同的提供商帐户(例如hotmail、yahoo、outlook)时,它会引发以下错误:
07-04 13:18:34.736: I/System.out(32140): ERROR SENDING MESSAGE: javax.mail.MessagingException: Could not connect to SMTP host: smtp-mail.outlook.com, port: 587;
07-04 13:18:34.736: I/System.out(32140):   nested exception is:
07-04 13:18:34.736: I/System.out(32140):    javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7912c980: Failure in SSL library, usually a protocol error
07-04 13:18:34.736: I/System.out(32140): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x7387f7d0:0x00000000)

我使用以下凭据发送邮件:

用户:@hotmail.com 主机:smtp-mail.outlook.com 端口:587

我尝试使用不同的帐户(所有帐户都不是gmail帐户),但每次都遇到相同的错误。

我的MailSender类如下:

public class MailSender{

public synchronized static void sendMail(String[] dest, String org, String pass, String body, String port, String host){ 

    String[] to = dest;
    final String user = org;
    final String password = pass;
    MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
    mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
    mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
    mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
    mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
    mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
    CommandMap.setDefaultCommandMap(mc);  

    Properties props = new Properties();
    props.put("mail.smtp.user", user);
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", port);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user,password);
        }
    });

    try{ 
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));     
        InternetAddress[] addressTo = new InternetAddress[to.length]; 
        for (int i = 0; i < to.length; i++) { 
            addressTo[i] = new InternetAddress(to[i]); 
        } 
        message.setRecipients(MimeMessage.RecipientType.TO, addressTo);
        message.setSubject("Arcas Ollé Alarm Message");   
        BodyPart messageBody = new MimeBodyPart();
        messageBody.setText(body);  
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBody);
        message.setContent(multipart ); 
        Transport.send(message); 
        System.out.println("MESSAGE SENT...");
    }catch (MessagingException ex) {
        System.out.println("ERROR SENDING MESSAGE: "+ ex);
    }
  }
}

我会将方法调用如下:

MailSender.sendMail(dest, org, pass, body, port, host);

就像我之前说的,该方法在使用gmail账号时运行良好,因此我不认为这是数据解析问题,而且我确定我尝试的所有凭据都是正确的。

可能是一些属性的问题。

因此如果有人能帮我解决这个问题,我会非常感激。

提前感谢您的帮助,

margabro。


1
您的主机 smtp-mail.outlook.com 端口:587 无法访问。 - Mehul Joisar
我也能理解这个问题。但我不知道问题出在哪里。告诉我另一个可达的主机,我可以尝试一下...我几乎在所有地方都有账户。除了 Gmail 之外,没有一个可用的。 - kodartcha
3个回答

2

以下是参考信息:

Gmail- 主机名:smtp.gmail.com,端口号:465

Hotmail- 主机名:smtp.live.com,端口号:587

Yahoo- 主机名:smtp.mail.yahoo.com,端口号:465


谢谢,它也可以与雅虎一起使用,但不能与Hotmail一起使用。有什么想法为什么? - kodartcha
更改端口:将端口从587更改为25。 - Farouk Touzi
很奇怪的是,雅虎和Gmail主机都可以ping通,但Hotmail不能。 - Simas

0

我终于找到了一个解决方案(经过大量的研究和测试)。我将其发布为答案,以防其他人遇到相同的问题。

在邮件发送器类中,我对属性代码进行了以下更改:

Properties props = new Properties();
props.put("mail.smtp.user", user);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "587");
//props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

为所有主机添加端口587作为套接字工厂端口,并注释掉该行:

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

我测试和使用的主机配置如下:

-- HOTMAIL / OUTLOOK --

主机:smtp.live.com
端口:25

-- GMAIL --

主机:smtp.gmail.com
端口:587

-- YAHOO! --

主机:smtp.mail.yahoo.com
端口:587

希望能对您有所帮助 :)


你的答案对我有用,但是在使用雅虎发送邮件时,我遇到了这个异常:“10-09 11:23:26.891:I/System.out(11507):ERROR SENDING MESSAGE:com.sun.mail.smtp.SMTPSendFailedException:554 Transaction failed:Cannot send message due to possible abuse;请访问http://postmaster.yahoo.com/abuse_smtp.html获取更多信息。” 你有什么想法吗? - Satyen Udeshi

0

在此网站上创建您的帐户以进行测试

http://www.serversmtp.com/en/smtp-configuration

你试过使用POP邮件服务器进行这些设置吗?

Hotmail/MSN

传入邮件服务器:pop3.live.com

安全类型=SSL

服务器端口995


发件邮件服务器:smtp.live.com

需要登录 = 是

安全类型 = TLS

服务器端口587


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接