0
votes

I use Amazon SES service to send email in tomcat6. It can only send email success for several hours and then it will send email failed. If I reboot the tomcat6, it will send email success again for several hours and then failed. This problem has been bothering me for a several days.
Does any body has encountered this problem? please help me, thanks.

//Send email code:

public class MyAuthenticator extends Authenticator {
    String userName = null;
     String password = null;
     public MyAuthenticator() {
     }
     
     public MyAuthenticator(String username, String password) {
        this.userName = username;
        this.password = password;
     }
     
     protected PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(userName, password);
     }
}


//send email
public class SimpleMailSender {
    public final static String HOST = "email-smtp.us-east-1.amazonaws.com";
    public final static int PORT = 25;
    public final static String FROM_ADDRESS = "[email protected]";
    public final static String TO_ADDRESS = "[email protected]";
    public final static String USER = "xx";
    public final static String PASSWORD = "xxx";
    
    public static void sendTestEmail()
    {
        try
        {
            Properties p = new Properties();
            p.put("mail.transport.protocol", "smtp");
            p.put("mail.smtp.host", HOST);
            p.put("mail.smtp.port", PORT);
            p.put("mail.smtp.auth", "true");
            p.put("mail.smtp.starttls.enable", "true");
            p.put("mail.smtp.starttls.required", "true");
            p.put("mail.debug", "true");

            MyAuthenticator authenticator = new MyAuthenticator(USER, PASSWORD);
            Session sendMailSession = Session.getDefaultInstance(p, authenticator);
              
            Message mailMessage = new MimeMessage(sendMailSession);
            Address from = new InternetAddress(FROM_ADDRESS);
            mailMessage.setFrom(from);
            Address to = new InternetAddress(TO_ADDRESS);
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            mailMessage.setSubject("Test");
            mailMessage.setSentDate(new Date());
            
            Multipart mainPart = new MimeMultipart();
            BodyPart html = new MimeBodyPart();
            html.setContent("Test", "text/html; charset=utf-8");    
            mainPart.addBodyPart(html);
            mailMessage.setContent(mainPart);

            Transport.send(mailMessage);
        }catch(Exception ex)
        {
          ex.printStackTrace();
        }
    }   
}
          


Error information when send email failed:
-----------------------------------------
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 "email-smtp.us-east-1.amazonaws.com", port 25, isSSL false
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-1488591277 A0o81GaPHjNAz0NyRkfI
DEBUG SMTP: connected to host "email-smtp.us-east-1.amazonaws.com", port: 25

EHLO ip-172-31-15-212.ec2.internal
250-email-smtp.amazonaws.com
250-8BITMIME
250-SIZE 10485760
250-STARTTLS
250-AUTH PLAIN LOGIN
250 Ok
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10485760"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "Ok", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed

javax.mail.AuthenticationFailedException: 220 Ready to start TLS

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at mail.SimpleMailSender.sendHtmlMail(SimpleMailSender.java:97)
at cla.communication.sendmail(communication.java:2577)
at cla.communication.getpasswd(communication.java:1821)
at cla.communication.doPost(communication.java:95)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:745)

1
You should be defining this mail connection as a Resource in your Context.xml. That will save you a lot of code and give you an external place for all these configuration items, including the password. No Authenticator requried.user207421

1 Answers

1
votes

It looks like it's ignoring your starttls properties. Try fixing the common JavaMail mistakes and see if that helps.

What version of JavaMail are you using?