4

I would like to make my android app using my mail server to send the mail, my server is using port 25, so my app need to send the mail without the appearance of ssl. I have tried the way mentioned in most of the similar questions here, but none of it works, therefore I open a new question to ask. Here are some links I have read:
Send email in android using JavaMail API with smtp but without SSL
Sending emails over SMTP with TSL
Java Mail: SSLHandshakeException when sending email on port 25 without SSL
Javamail: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException when sending mail from VPS
Using JavaMail with TLS
http://java.sun.com/products/javamail/javamail-1.4.2/SSLNOTES142.txt

Below is my mail sending class:

public class MailSender extends Authenticator { 
    private String user; 
    private String password; 

    private String [] to; 
    private String from; 

    private String port; 
    private String sport; 

    private String host; 

    private String subject; 
    private String body; 

    private boolean auth; 
    private boolean debuggable; 

    private Multipart multi; 

    public MailSender(){ 
        host = "me.myserver.com"; 
        port = "25"; 
        sport = "25"; 

        user = ""; 
        password = ""; 
        from = ""; 
        subject = ""; 
        body = ""; 

        debuggable = true; 
        auth = true; 

        multi = new MimeMultipart(); 

        // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. 
        MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();  
        mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");  
        mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");  
        mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
        mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");  
        CommandMap.setDefaultCommandMap(mc);  
    } 

    public MailSender(String user, String password){ 
        this();       
        this.user = user; 
        this.password = password;    
    } 

    public boolean send() throws Exception { 
        Properties props = setProperties(); 

        try{ 
            Session session = Session.getInstance(props, this); 
            session.setDebug(true); 

            MimeMessage msg = new MimeMessage(session); 

            msg.setFrom(new InternetAddress(from)); 

            InternetAddress[] addressTo = new InternetAddress[to.length]; 
            for(int i=0; i<to.length; i++){ 
                addressTo[i] = new InternetAddress(to[i]); 
            } 

            msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 
            msg.setSubject(subject); 
            msg.setSentDate(new Date()); 

            BodyPart messageBodyPart = new MimeBodyPart(); 
            messageBodyPart.setText(body); 
            multi.addBodyPart(messageBodyPart); 

            msg.setContent(multi); 

            Transport transport = session.getTransport("smtps"); 
            transport.connect(host, 25, user, password); 
            transport.sendMessage(msg, msg.getAllRecipients()); 
            transport.close(); 
            return true; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            return false; 
        } 
    } 

    public void addAttachment(String filename) throws Exception { 
        BodyPart messageBodyPart = new MimeBodyPart(); 
        DataSource source = new FileDataSource(filename); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(filename); 

        multi.addBodyPart(messageBodyPart); 
    } 

    @Override  
      public PasswordAuthentication getPasswordAuthentication() {  
        return new PasswordAuthentication(user, password);  
      } 

    private Properties setProperties() { 
        Properties props = new Properties(); 

        props.put("mail.smtp.host", host); 

        if(debuggable) { 
            props.put("mail.debug", "true"); 
        } 

        if(auth) { 
            props.put("mail.smtp.auth", "true"); 
        } 

        //props.put("mail.smtp.ssl.enable", "false");
        //props.put("mail.smtp.ssl.trust", "*");

        props.put("mail.smtp.port", port); 
        props.put("mail.smtp.socketFactory.port", sport); 
        //props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
        //props.put("mail.smtp.socketFactory.fallback", "true"); 
        props.setProperty("mail.smtp.ssl.enable", "true");
        props.setProperty("mail.smtp.ssl.socketFactory.class",
                        "DummySSLSocketFactory");
        props.setProperty("mail.smtp.ssl.socketFactory.fallback", "false");

        //props.put("mail.smtp.starttls.enable", "false");

        return props; 
    } 

    public void setTo(String[] toAddress) { 
        this.to = toAddress; 
    } 

    public void setFrom(String fromAddress) { 
        this.from = fromAddress; 
    } 

    public void setSubject(String subject) { 
        this.subject = subject; 
    } 

    public void setBody(String body) {  
        this.body = body;  
    } 
}

DummySSLSocketFactory.java:

public class DummySSLSocketFactory extends SSLSocketFactory {
    private SSLSocketFactory factory;

    public DummySSLSocketFactory() {
    try {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null,
                 new TrustManager[] { new DummyTrustManager()},
                 null);
        factory = (SSLSocketFactory)sslcontext.getSocketFactory();
    } catch(Exception ex) {
        // ignore
    }
    }

    public static SocketFactory getDefault() {
    return new DummySSLSocketFactory();
    }

    public Socket createSocket() throws IOException {
    return factory.createSocket();
    }

    public Socket createSocket(Socket socket, String s, int i, boolean flag)
                throws IOException {
    return factory.createSocket(socket, s, i, flag);
    }

    public Socket createSocket(InetAddress inaddr, int i,
                InetAddress inaddr1, int j) throws IOException {
    return factory.createSocket(inaddr, i, inaddr1, j);
    }

    public Socket createSocket(InetAddress inaddr, int i)
                throws IOException {
    return factory.createSocket(inaddr, i);
    }

    public Socket createSocket(String s, int i, InetAddress inaddr, int j)
                throws IOException {
    return factory.createSocket(s, i, inaddr, j);
    }

    public Socket createSocket(String s, int i) throws IOException {
    return factory.createSocket(s, i);
    }

    public String[] getDefaultCipherSuites() {
    return factory.getDefaultCipherSuites();
    }

    public String[] getSupportedCipherSuites() {
    return factory.getSupportedCipherSuites();
    }
}

DummyTrustManager.java:

public class DummyTrustManager implements X509TrustManager {
    public void checkClientTrusted(X509Certificate[] cert, String authType) {
    // everything is trusted
    }

    public void checkServerTrusted(X509Certificate[] cert, String authType) {
    // everything is trusted
    }

    public X509Certificate[] getAcceptedIssuers() {
    return new X509Certificate[0];
    }
}

Error messages in log:

08-15 16:08:59.338: W/System.err(16163): javax.mail.MessagingException: Could not connect to SMTP host: me.myserver.com, port: 25;
08-15 16:08:59.338: W/System.err(16163):   nested exception is:
08-15 16:08:59.338: W/System.err(16163):    javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5bfaf480: Failure in SSL library, usually a protocol error
08-15 16:08:59.338: W/System.err(16163): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x580b87de:0x00000000)
08-15 16:08:59.338: W/System.err(16163):    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
08-15 16:08:59.338: W/System.err(16163):    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
08-15 16:08:59.346: W/System.err(16163):    at javax.mail.Service.connect(Service.java:288)
08-15 16:08:59.346: W/System.err(16163):    at Android.sender.MailSender.send(MailSender.java:100)
08-15 16:08:59.346: W/System.err(16163):    at Android.sender.Sender$SendMail.doInBackground(Sender.java:73)
08-15 16:08:59.346: W/System.err(16163):    at Android.sender.Sender$SendMail.doInBackground(Sender.java:1)
08-15 16:08:59.354: W/System.err(16163):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-15 16:08:59.354: W/System.err(16163):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 16:08:59.354: W/System.err(16163):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 16:08:59.354: W/System.err(16163):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-15 16:08:59.354: W/System.err(16163):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-15 16:08:59.354: W/System.err(16163):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-15 16:08:59.354: W/System.err(16163):    at java.lang.Thread.run(Thread.java:856)
08-15 16:08:59.354: W/System.err(16163): Caused by: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5bfaf480: Failure in SSL library, usually a protocol error
08-15 16:08:59.354: W/System.err(16163): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x580b87de:0x00000000)
08-15 16:08:59.362: W/System.err(16163):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:436)
08-15 16:08:59.362: W/System.err(16163):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:647)
08-15 16:08:59.362: W/System.err(16163):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:618)
08-15 16:08:59.362: W/System.err(16163):    at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:1449)
08-15 16:08:59.362: W/System.err(16163):    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1366)
08-15 16:08:59.362: W/System.err(16163):    ... 12 more
08-15 16:08:59.362: W/System.err(16163): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5bfaf480: Failure in SSL library, usually a protocol error
08-15 16:08:59.362: W/System.err(16163): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x580b87de:0x00000000)
08-15 16:08:59.362: W/System.err(16163):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
08-15 16:08:59.362: W/System.err(16163):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
08-15 16:08:59.362: W/System.err(16163):    ... 16 more

Here I have changed "Transport transport = session.getTransport("smtps");" to "Transport transport = session.getTransport("smtps");", and messages in error log has changed.

08-15 17:18:13.986: W/System.err(21399): javax.mail.MessagingException: Could not convert socket to TLS;
08-15 17:18:13.986: W/System.err(21399):   nested exception is:
08-15 17:18:13.986: W/System.err(21399):    javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
08-15 17:18:13.986: W/System.err(21399):    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1339)
08-15 17:18:13.986: W/System.err(21399):    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:420)
08-15 17:18:13.986: W/System.err(21399):    at javax.mail.Service.connect(Service.java:288)
08-15 17:18:13.986: W/System.err(21399):    at Android.sender.MailSender.send(MailSender.java:100)
08-15 17:18:13.986: W/System.err(21399):    at Android.sender.Sender$SendMail.doInBackground(Sender.java:73)
08-15 17:18:13.986: W/System.err(21399):    at Android.sender.Sender$SendMail.doInBackground(Sender.java:1)
08-15 17:18:13.986: W/System.err(21399):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-15 17:18:13.986: W/System.err(21399):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 17:18:13.986: W/System.err(21399):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 17:18:13.986: W/System.err(21399):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-15 17:18:13.986: W/System.err(21399):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-15 17:18:13.986: W/System.err(21399):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-15 17:18:13.986: W/System.err(21399):    at java.lang.Thread.run(Thread.java:856)
08-15 17:18:13.986: W/System.err(21399): Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
08-15 17:18:13.986: W/System.err(21399):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:398)
08-15 17:18:13.986: W/System.err(21399):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:647)
08-15 17:18:13.986: W/System.err(21399):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:618)
08-15 17:18:13.986: W/System.err(21399):    at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:1449)
08-15 17:18:13.986: W/System.err(21399):    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1336)
08-15 17:18:13.986: W/System.err(21399):    ... 12 more
08-15 17:18:13.986: W/System.err(21399): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
08-15 17:18:13.986: W/System.err(21399):    at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:192)
08-15 17:18:13.994: W/System.err(21399):    at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:163)
08-15 17:18:13.994: W/System.err(21399):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:597)
08-15 17:18:13.994: W/System.err(21399):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
08-15 17:18:13.994: W/System.err(21399):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
08-15 17:18:13.994: W/System.err(21399):    ... 16 more
08-15 17:18:13.994: W/System.err(21399): Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
08-15 17:18:13.994: W/System.err(21399):    ... 21 more
Community
  • 1
  • 1
Conrad
  • 933
  • 4
  • 19
  • 34
  • You do realise your mail.smtp.starttls.enable property is set to false when it should be true? – JamesB Aug 15 '12 at 08:41
  • so I hide the line, isn't it default to true? And also I want to ask, is my problem need a self-celtificate? Because I have read this page, but not quite understand how to use the block of code in my program. http://stackoverflow.com/questions/1217141/self-signed-ssl-acceptance-android – Conrad Aug 15 '12 at 08:48
  • I have added back these two lines, but still, same error occurs, I was fail to disable ssl. props.put("mail.smtp.ssl.enable", "false"); props.put("mail.smtp.starttls.enable", "true"); – Conrad Aug 15 '12 at 08:56
  • Have you tried what was suggested here: http://stackoverflow.com/questions/5592112/javamail-smtp-properties-for-starttls – JamesB Aug 15 '12 at 09:03
  • I think the one shown there is exactly the same to mine after I have added back the two lines, however, it does not work. As I mention,I have no idea why props.put("mail.smtp.ssl.enable", "false"); cannot disable ssl. – Conrad Aug 15 '12 at 09:09
  • I have updated the question little bit, see if this can help you recognize my problem easily. – Conrad Aug 15 '12 at 09:20

1 Answers1

1

This seems like a duplicate of your other thread: SSLException in using JavaMail API, although over there it seemed like you did want to use SSL.

If you don't want to use SSL, why do you have DummySSLSocketFactory and DummyTrustManager? Get rid of them entirely and use the "smtp" protocol, not the "smtps" protocol. And, as I explained in your other thread, you don't need any socket factory properties at all, even if you were using SSL.

You didn't say whether you wanted to use STARTTLS, that is, whether you want to connect to your mail server using a plain text (non-SSL) connection and then switch the connection to SSL. If you want to do that, set the mail.smtp.starttls.enable property to true.

Community
  • 1
  • 1
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • ya, I think in somewhere they are repeated. As one is the old one, which is using current one. Because I am a beginner, so I am just trying different method mentioned on the Internet, because what I would like to do in fact is to connect to my mail server at port 25 only. And at last I have tried out the answer, which is exactly same as what you said, it should be "smtp" instead of "smtps" – Conrad Aug 16 '12 at 01:37
  • Have you found the JavaMail FAQ, and all the sample code included with JavaMail? – Bill Shannon Aug 16 '12 at 06:44
  • I just read those sample codes posted in other questions or website mentioned by other people, so it causes trouble to me when I need to debug such kind of small scale error. – Conrad Aug 16 '12 at 07:04
  • I'm not talking about the random, often incorrect, code that people post here or on other web sites. I'm talking about the sample code that's included in the JavaMail zip file when you download JavaMail. – Bill Shannon Aug 16 '12 at 17:24
  • I have not read it, as I had not downloaded JavaMail as well. I understand this will probably cause a lot of troubles to me, so I will try to change this bad habit, thank you for your advice! – Conrad Aug 17 '12 at 01:23
  • In the link to the post above I've just put the working examples of my mail client implementation. You can check if it helped for you. – Lukasz Frankowski Aug 24 '12 at 05:48