Java发送电子邮件:smtp.gmail.com error

时间:2017-06-09 19:23:32

标签: java gmail

我有以下java代码发送电子邮件:

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

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("erjobertolazo@gmail.com"));

Address[] toUser = InternetAddress 
.parse("Recipient1@gmail.com, Recipient2@hotmail.com, Recipient3@yahoo.com.br");  
message.setRecipients(Message.RecipientType.TO, toUser);
message.setSubject("TEST SEND EMAIL");//Assunto
message.setText("THIS IS JUST A TEST!");

Transport.send(message);

我收到以下错误消息:

  

javax.servlet.ServletException:您的控制器引发了异常       br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:103)根本原因

     

org.apache.commons.mail.EmailException:将电子邮件发送给   以下服务器失败:smtp.gmail.com:465       org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)       org.apache.commons.mail.Email.send(Email.java:1437)       br.com.corpcontrol.controller.UsuariosController.testeEmailB(UsuariosController.java:245)       sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)       sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       java.lang.reflect.Method.invoke(Method.java:606)       br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:61)       br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:61)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:48)       br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:67)       br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:83)       br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:61)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:69)       br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56)       br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)       br.com.caelum.vraptor.core.EnhancedRequestExecution.execute(EnhancedRequestExecution.java:44)       br.com.caelum.vraptor.VRaptor $ 1.insideRequest(VRaptor.java:93)       br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:58)       br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:99)根本原因

有人知道我该怎么做? 可以与其他电子邮件服务器。谢谢你的建议!

1 个答案:

答案 0 :(得分:3)

  1. 确保您未启用“双因素身份验证
  2. 导航至:“https://myaccount.google.com/lesssecureapps?pli=1”并启用“访问安全性较低的应用