使用Commons-Email向Gmail发送电子邮件

时间:2009-11-23 15:10:23

标签: java email apache-commons-email

Email email = new SimpleEmail();
String authuser = "......@gmail.com";
String authpwd = "*******";
// Very Important, Don't use email.setAuthentication()
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true); // true if you want to debug
email.setHostName("smtp.gmail.com");

email.getMailSession().getProperties().put("mail.smtp.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtp.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("........@gmail.com", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo(".............@gmail.com", "ToName");
email.send();

并且它提供以下异常

SEVERE: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465

5 个答案:

答案 0 :(得分:16)

Commons Email用户指南中有一个使用SSL的Gmail示例。

https://commons.apache.org/proper/commons-email/userguide.html

SSL / TLS(端口465) - > email.setSSLOnConnect(真);

Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

STARTTLS(端口587) - > email.setStartTLSEnabled(真);

Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setStartTLSEnabled(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

答案 1 :(得分:14)

这对我有用

Email email = new SimpleEmail();
String authuser = "...@gmail.com";
String authpwd = "xxxx";
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true);
email.setHostName("smtp.gmail.com");
email.getMailSession().getProperties().put("mail.smtps.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtps.port", "587");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("........@gmail.com", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("xxxx@gmail.com", "ToName");
email.setTLS(true);
email.send();

答案 2 :(得分:10)

您需要tell Commons Email that you're sending a TLS email

email.setTLS(true);

在您致电email.send()?

之前

我不确定这是否会解决您的问题,因为我不确定您是否遇到连接到smtp.gmail.com:465或成功发送到它的问题(错误消息/异常是你提出它时模糊不清),但到目前为止,这绝对是我所不知道的。

答案 3 :(得分:0)

如果要使用TLS,请使用端口587和starttls:

@extends('layouts.app')

@section('content')
   <h1>{{$title}}</h1>
   @if(count($pakman) > 0)
   <ul>
        @foreach($pakmans as $pakman)
        <li>{{$pakmans}}</li>
        @endforeach
    </ul>
  @endif
@endsection

如果要使用端口465,请不要将其设置为true,而是调用以下函数:

email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
// or call this:
email.setStartTLSEnabled().
// and set the port to 587
email.setSmtpPort(smtpPort);

答案 4 :(得分:0)

不要忘记启用谷歌 Less secure app access