【问题标题】:Error sending email programmatically from non gmail account从非 gmail 帐户以编程方式发送电子邮件时出错
【发布时间】:2014-08-25 17:36:57
【问题描述】:

我正在尝试使用我从这里获取的一些已知方法从我的 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 帐户,因此我认为这不是数据解析问题,我确信我尝试的所有凭据都是正确的。

这可能是礼仪问题。

如果有人能帮我解决这个问题,我将不胜感激。

提前致谢,

马加布罗。

【问题讨论】:

  • 您的主机smtp-mail.outlook.com Port: 587 无法访问
  • 我也能理解。但我不知道问题出在哪里。告诉我另一个我可以尝试的可达主机...我几乎到处都有帐户。除了 gmail 之外,没有一个工作。

标签: java android email authenticator


【解决方案1】:

供参考:

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

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

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

【讨论】:

  • 谢谢,它也适用于 Yahoo,但不适用于 Hotmail。知道为什么吗?
  • 更改为端口:25 而不是端口:587
  • 奇怪的是,Yahoo 和 Gmail 主机都可以 ping,但 Hotmail 却不能。
【解决方案2】:

在此站点中创建您的帐户用于测试目的

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

您是否使用 POP 邮件服务器尝试过此设置?

Hotmail/MSN

接收邮件服务器:pop3.live.com

安全类型 = SSL

服务器端口 995


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

需要登录 = 是

安全类型 = TLS

服务器端口 587

【讨论】:

    【解决方案3】:

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

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

    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 / 展望--

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

    -- GMAIL --

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

    -- 雅虎! --

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

    希望对你有帮助:)

    【讨论】:

    • 您的答案对我有用,但在使用 yahoo 发送邮件时,我收到此异常“10-09 11:23:26.891: I/System.out(11507): ERROR SENDING MESSAGE: com.sun .mail.smtp.SMTPSendFailedException: 554 Transaction failed : 由于可能的滥用而无法发送消息;请访问postmaster.yahoo.com/abuse_smtp.html 了解更多信息”对此有任何想法吗??
    猜你喜欢
    • 2012-03-16
    • 2013-03-24
    • 2010-11-22
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 2011-12-15
    • 2012-03-30
    • 1970-01-01
    相关资源
    最近更新 更多