0

I'm trying to send the mail by enabling SSL using JavaMail with office365 as host. When I'm using the host as smtp.gmail.com then it's working fine but not working for smtp.office365.com Below is my approach:

public class SendMail
{

     public static void main(String args[]){
        String username = "abc@xyz.com";
        String password = "abc123";
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.office365.com");
        properties.put("mail.smtp.socketFactory.port",995);
        properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");

        Session session = null;

        if (authenticationRequired) {
          session = Session.getInstance(properties,
              new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(userName,password);
                }
              });
        }else{
             session = Session.getDefaultInstance(properties);  
       }
       try{
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setSubject("Test mail");
            message.setContent("Test Mail", "text/html");
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(john@gmail.com));

            Transport transport = session.getTransport("smtp");
            transport.connect();
            transport.sendMessage(message,message.getAllRecipients());
       }catch(Exception e){
          e.printStack();
       }
     }
  }

It is throwing exception as - Could not connect to SMTP host: smtp.office365.com, port: 995 response: -1. I have also tried the port as 993 but that too also not working. Kindly help me out. Other approaches are also welcome

Samraan
  • 204
  • 1
  • 2
  • 14
  • I haven't an answer for your problem, but I used this library [https://commons.apache.org/proper/commons-email/](https://commons.apache.org/proper/commons-email/) for sending mails....and worked well. Try it! Is built on top of the Java Mail API. – appersiano Jun 10 '15 at 13:37
  • What's the output if you start your program with `-Djavax.net.debug=ssl`? – wallenborn Jun 10 '15 at 14:46

1 Answers1

0

As you said, works with gmail did you import the gmail´s certificate to your keystore?

Raphael Milani
  • 151
  • 2
  • 14
  • Try to add this: props.put("mail.smtp.ssl.trust", "smtpserver"); – Raphael Milani Jun 10 '15 at 20:02
  • I tried that one also but getting same error....and I didn't import the Certificate – Samraan Jun 11 '15 at 08:09
  • Try to use port 587 or take a look this thread: http://stackoverflow.com/questions/6244694/send-smtp-email-using-system-net-mail-via-exchange-online-office-365 – Raphael Milani Jun 15 '15 at 13:43
  • I tried with port 587 also, but I got the error like - `Failed to connect host: office365 and Port 25` though we are using the port `587` – Samraan Jun 15 '15 at 14:00