繁体   English   中英

使用SSL连接通过Gmail SMTP服务器发送电子邮件通知

[英]Send an email notification via Gmail SMTP server using SSL connection

我正在尝试使用SSL连接通过Gmail SMTP服务器发送电子邮件通知。 但没有电子邮件。 甚至没有报告错误。我的方法send()在下面给出。请帮助。我正在使用JSF2。

public void send()
{
    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() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("userName","password");
        }
    });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("nid@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("arun@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler," +
                "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

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

这段代码过于复杂。 JavaMail FAQ包含使用Gmail的示例代码 它还有调试问题的提示

一个特殊问题是您应该使用Session.getInstance而不是Session.getDefaultInstance。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM