使用javax.mail发送带有ssl的电子邮件 [英] sending email with ssl using javax.mail

查看:1578
本文介绍了使用javax.mail发送带有ssl的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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....

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("myemail@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("myemail@gmail.com"));
        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

发生以下错误:
http://pastie.org/private/rkoknss6ppiufjd9swqta

推荐答案

而不是


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

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

运输运输= session.getTransport(smtps);

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

使用


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

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

运输transport = session.getTransport(smtp);

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

使用 smtp ,而不是smtps

Use smtp, not smtps

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

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

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("username@gmail.com","password");
            }
        });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("frommail@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("tomail@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Test Mail");

        Transport.send(message);

        System.out.println("Done");

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

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

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

这篇关于使用javax.mail发送带有ssl的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆