从AWS Linux AMI发送时出现javax.mail.AuthenticationFailedException

时间:2016-03-13 08:20:47

标签: java linux email amazon-web-services

  

我已将gmail设置设为安全性较低的应用。我的应用程序托管在AWS Linux AMI上。相同的应用程序在windows

中的localhost上工作正常
public MailServiceImpl() {
                java.util.Properties properties=new java.util.Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.socketFactory.port", "465");
            properties.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.port", "465");

        session = Session.getInstance(properties, new GmailAuthenticator(
                    "user@gmail.com",
                    "password"){
                public PasswordAuthentication getPasswordAuthentication(){
                    return new PasswordAuthentication("user@gmail.com", 
                            "password");
                }
            });

}

private void send(InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, String subject, String text, InternetAddress from,
            InternetAddress[] replyTo,String[] attachFiles) throws NoValidRecipientsException {
        try {
            // make sure that there are recipients
            if ((to == null) && (cc == null) & (bcc == null)) {
                log.warn("no valid recipient email address(es) specified");
                throw new NoValidRecipientsException();
            }
            // create message
            MimeMessage message = new MimeMessage(session);
            // set recipients
            message.setRecipients(Message.RecipientType.TO, to);
            message.setRecipients(Message.RecipientType.CC, cc);
            message.setRecipients(Message.RecipientType.BCC, bcc);
            // set replyto
            if (replyTo != null) {
                message.setReplyTo(replyTo);
            }
            // set from
            if (from != null) {
                message.setFrom(from);
            } else {
                log.warn("no valid 'from' email address specified");
            }
            // set subject
            message.setSubject(subject);
            // set content type as text for now.
            // message.setText(text);

            /** Attachment Starts **/
            if(attachFiles !=null){
            // creates message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(text, "text/html");

            // creates multi-part
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);

            // adds attachments
            if (attachFiles != null && attachFiles.length > 0) {
                for (String filePath : attachFiles) {
                    MimeBodyPart attachPart = new MimeBodyPart();

                    try {
                        attachPart.attachFile(filePath);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }

                    multipart.addBodyPart(attachPart);
                }
            }

            // sets the multi-part as e-mail's content
            message.setContent(multipart);
            }else{
                message.setContent(text, "text/html");
            }
            /*** Attachment ends **/
            // send the message
            log.info("before Transport send");
            Transport.send(message);
        } catch (MessagingException e) {
            log.error("error sending email", e);
            throw new MailServiceException(e);
        }
    }

公共类GmailAuthenticator扩展了Authenticator {

    String user;
    String pw;
    public  GmailAuthenticator (String username, String password)
    {
       super();
       this.user = username;
       this.pw = password;
    }
   public PasswordAuthentication getPasswordAuthentication()
   {
      return new PasswordAuthentication(user, pw);
   }

}

我得到以下错误

 08:07:52,876 ERROR MailServiceImpl:280 - error sending email
javax.mail.AuthenticationFailedException
        at javax.mail.Service.connect(Service.java:306)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)
        at javax.mail.Transport.send0(Transport.java:168)
        at javax.mail.Transport.send(Transport.java:98)
        at increed.service.mail.MailServiceImpl.send(MailServiceImpl.java:278)
        at increed.service.mail.MailServiceImpl.send(MailServiceImpl.java:134)
        at increed.service.mail.Messenger.sendCustomerEmail(Messenger.java:113)
        at increed.com.user.action.CollectionNavigation.fetchCustomerMailDetails(CollectionNavigation.java:429)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
        at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionI:
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
        at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
        at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
 at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(Cha:
  at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
        at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)
increed.service.mail.MailServiceException: increed.service.mail.MailServiceException: javax.mail.AuthenticationFailedException
        at increed.service.mail.MailServiceImpl.send(MailServiceImpl.java:136)
        at increed.service.mail.Messenger.sendCustomerEmail(Messenger.java:113)
        at increed.com.user.action.CollectionNavigation.fetchCustomerMailDetails(CollectionNavigation.java:429)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

1 个答案:

答案 0 :(得分:0)

在EC2常见问题解答中

  

问:从EC2实例发送电子邮件有任何限制吗?

     

是。为了保持EC2地址的发送质量   电子邮件,我们对可以发送的电子邮件数量实施默认限制   从EC2帐户发送。如果您希望发送大量电子邮件   从EC2,您可以申请从您的帐户中删除这些限制   填写此form

亚马逊宁愿使用他们的简单电子邮件服务,请参阅https://aws.amazon.com/ses/。这确实提供了一个SMTP接口