使用 javax.mail 通过 ssl 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26548059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:45:35  来源:igfitidea点击:

sending email with ssl using javax.mail

javaemail

提问by pila

i want to send an email using gmail as smtp server.

我想使用 gmail 作为 smtp 服务器发送电子邮件。

this is my code, and i do not get it to work... after running testSettings() i get the debug output and then it just stops. no timeout, no error, nothing....

这是我的代码,我没有让它工作......运行 testSettings() 后,我得到调试输出,然后它就停止了。没有超时,没有错误,没有......

public void testSettings() {
    final String username = Settings.get("benutzername");
    final String password = Settings.get("passwort");

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtps");

        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.port", Settings.get("port"));
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");

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

    props.put("mail.smtp.host", Settings.get("server"));
    props.put("mail.smtp.port", Settings.get("port"));
    props.put("mail.smtp.timeout", "10000");

    props.put("mail.smtp.ssl.checkserveridentity", "false");
    props.put("mail.smtp.ssl.trust", "*");
    props.put("mail.smtp.connectiontimeout", "10000");

    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.socketFactory.fallback", "false");
    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
    session.setDebug(true);
    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("[email protected]"));
        message.setSubject("Testing Subject");
        message.setText("test");
        Transport transport = session.getTransport("smtps");
        transport.send(message);
        // Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        // throw new RuntimeException(e);
    }
}


DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning     javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false

The following error occurs: http://pastie.org/private/rkoknss6ppiufjd9swqta

出现以下错误:http: //pastie.org/private/rkoknss6ppiufjd9swqta

采纳答案by Anar Orujov

Instead of

代替

props.put("mail.transport.protocol", "smtps");

Transport transport = session.getTransport("smtps");

props.put("mail.transport.protocol", "smtps");

传输传输 = session.getTransport("smtps");

Use

props.put("mail.transport.protocol", "smtp");

Transport transport =session.getTransport("smtp");

props.put("mail.transport.protocol", "smtp");

传输传输 =session.getTransport("smtp");

Use smtp, not smtps

使用smtp,而不是 smtps

I used JDK 8, Netbeans 8, JavaMail 1.5.2 and this example works fine:

我使用了 JDK 8、Netbeans 8、JavaMail 1.5.2,这个例子工作正常

public static void main(String[] args) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465"); 
    Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
                            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]","password");
            }
        });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("[email protected]"));
        message.setSubject("Testing Subject");
        message.setText("Test Mail");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

If you are not able connect with port 465, try port 587

如果您无法连接端口 465,请尝试端口 587

回答by Hibbem

From reading this: http://www.oracle.com/technetwork/java/javamail/faq-135477.html#commonmistakes

从阅读这个:http: //www.oracle.com/technetwork/java/javamail/faq-135477.html#commonmistakes

The use of

指某东西的用途

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

and

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

is kind of outdated. To simplify the code, use:

有点过时了。为了简化代码,请使用:

properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", "true");

回答by Nick Vaculich

If you using Windows and have any Antivirus or firewall disable it and try.

如果您使用 Windows 并且有任何防病毒或防火墙禁用它并尝试。

回答by Anton

My problem was in computer name, it was contain cyrillic symbols. For understanding reason of your problem add

我的问题是计算机名称,它包含西里尔符号。为了理解您的问题的原因,请添加

props.put("mail.debug", "true");

Also see work code here

另请参阅此处的工作代码