Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error sending email programmatically from non gmail account

I'm trying to send and email programmatically from my Android app, using some known methods which I took from here.

The method is working fine with gmail account, but when I try with a different provider account like, hotmail, yahoo, outlook, it throws this error:

07-04 13:18:34.736: I/System.out(32140): ERROR SENDING MESSAGE: javax.mail.MessagingException: Could not connect to SMTP host: smtp-mail.outlook.com, port: 587;
07-04 13:18:34.736: I/System.out(32140):   nested exception is:
07-04 13:18:34.736: I/System.out(32140):    javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7912c980: Failure in SSL library, usually a protocol error
07-04 13:18:34.736: I/System.out(32140): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x7387f7d0:0x00000000)

I'm using the following credentials to send the mail:

User: @hotmail.com Host: smtp-mail.outlook.com Port: 587

Tried with different accounts (all of them non gmail) and I get the same error every time.

My MailSender class is:

public class MailSender{

public synchronized static void sendMail(String[] dest, String org, String pass, String body, String port, String host){ 

    String[] to = dest;
    final String user = org;
    final String password = pass;
    MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
    mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
    mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
    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);  

    Properties props = new Properties();
    props.put("mail.smtp.user", user);
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", port);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user,password);
        }
    });

    try{ 
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));     
        InternetAddress[] addressTo = new InternetAddress[to.length]; 
        for (int i = 0; i < to.length; i++) { 
            addressTo[i] = new InternetAddress(to[i]); 
        } 
        message.setRecipients(MimeMessage.RecipientType.TO, addressTo);
        message.setSubject("Arcas Ollé Alarm Message");   
        BodyPart messageBody = new MimeBodyPart();
        messageBody.setText(body);  
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBody);
        message.setContent(multipart ); 
        Transport.send(message); 
        System.out.println("MESSAGE SENT...");
    }catch (MessagingException ex) {
        System.out.println("ERROR SENDING MESSAGE: "+ ex);
    }
  }
}

And I call the method like:

MailSender.sendMail(dest, org, pass, body, port, host);

As I said before the method works fine with gmail account, so I don't think it is a data parsing problem, and I'm sure all the credentials I tried are correct.

It could be a proprieties problem.

So if anyone could help me solve this out, I would really appreciate it.

Thanks in advance,

margabro.

like image 621
kodartcha Avatar asked Aug 05 '26 16:08

kodartcha


1 Answers

For reference:

Gmail- Host: smtp.gmail.com , Port: 465

Hotmail- Host: smtp.live.com , Port: 587

Yahoo- Host: smtp.mail.yahoo.com , Port: 465

like image 186
Farouk Touzi Avatar answered Aug 08 '26 05:08

Farouk Touzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!