Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't configure office365 SMTP on play mailer 5.0.0-M1 #98

Open
HirenPatel2791 opened this issue Mar 30, 2016 · 16 comments
Open

Can't configure office365 SMTP on play mailer 5.0.0-M1 #98

HirenPatel2791 opened this issue Mar 30, 2016 · 16 comments

Comments

@HirenPatel2791
Copy link

I recently tried this plug in.. and was able to send mails using google smtp.. Now in the same application I'm trying to integrate my office365 mailbox and send emails out. for that I followed this

application.conf:

play.mailer {
  host = smtp.office365.com
  port = 587
  ssl = no
  tls = yes
  username = "hiren.***@*****.com"
  password = "******"
  debug = no
  timeout = 600
  connectiontimeout = 600
  mock = no
}

but throws an exception:

[error] application - Email Sending Exception :
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.office365.com:587
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1421)
    at org.apache.commons.mail.Email.send(Email.java:1448)
    at play.api.libs.mailer.SMTPMailer$$anon$2.send(MailerPlugin.scala:100)
    at play.api.libs.mailer.CommonsMailer.send(MailerPlugin.scala:130)
    at play.api.libs.mailer.SMTPMailer.send(MailerPlugin.scala:110)
    at play.api.libs.mailer.SMTPDynamicMailer.send(MailerPlugin.scala:117)
    at play.api.libs.mailer.MailerClient$class.send(MailerPlugin.scala:56)
    at play.api.libs.mailer.SMTPDynamicMailer.send(MailerPlugin.scala:114)
    at controllers.EmailHelper.sendEmail(EmailHelper.java:17)
    at controllers.HomeController.addUser(HomeController.java:61)
Caused by: javax.mail.AuthenticationFailedException: 
    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:887)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
    at javax.mail.Service.connect(Service.java:386)
    at javax.mail.Service.connect(Service.java:245)
    at javax.mail.Service.connect(Service.java:194)
    at javax.mail.Transport.send0(Transport.java:253)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1411)
    at org.apache.commons.mail.Email.send(Email.java:1448)
Caused by: javax.mail.MessagingException: Exception reading response
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2304)
    at com.sun.mail.smtp.SMTPTransport.simpleCommand(SMTPTransport.java:2234)
    at com.sun.mail.smtp.SMTPTransport$LoginAuthenticator.doAuth(SMTPTransport.java:928)
    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:870)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
    at javax.mail.Service.connect(Service.java:386)
    at javax.mail.Service.connect(Service.java:245)
    at javax.mail.Service.connect(Service.java:194)
    at javax.mail.Transport.send0(Transport.java:253)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:930)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)

The problem I could suspect is tls version which should be >=1.0 for office365. But don't know how to check it.
I tried hard to find solution but couldn't.
any hint will be helpful.. thanks

@ggrossetie
Copy link
Member

Have you tried to enable ssl ? I think the root cause is AuthenticationFailedException, are you sure about your credential ?

@HirenPatel2791
Copy link
Author

Thanks for getting back to me. If you see to office365 SMTP submission documentation, It is only tls enabled. so there is no way ssl gonna work. But I'll give it a try. well I implemented mailing service using javax.mail with same settings and its working fine so there is no error in my configurations.

@mkurz
Copy link
Member

mkurz commented Mar 31, 2016

@HirenPatel2791 Did it work with enabling ssl?
If not - could you please provide your code which you use to send the mail via javax.mail?

@ggrossetie
Copy link
Member

On a side note, I think it could be great to add the configuration for common mail provider like Gmail or Office 365 in the user manual: https://github.com/playframework/play-mailer/blob/master/user-manual.adoc

@HirenPatel2791
Copy link
Author

@mkurz no it didn't work with ssl as expected. I'll share my code:

play.Configuration cfg = Play.application().configuration();
String from = cfg.getString("myEmail.from");
Properties props = System.getProperties();
props.setProperty("mail.smtp.from",from);
props.setProperty("mail.smtp.host", cfg.getString("myEmail.host"));
props.setProperty("mail.smtp.port", cfg.getString("myEmail.port"));
props.setProperty("mail.smtp.auth", cfg.getString("myEmail.auth"));
props.setProperty("mail.smtp.starttls.enable", cfg.getString("myEmail.tls"));
Session session = Session.getInstance(props, new Authenticator() {
  protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
    return new javax.mail.PasswordAuthentication(cfg.getString("myEmail.user"),cfg.getString("myEmail.password") );
  }
});
try {
  // Create a default MimeMessage object.
  MimeMessage message = new MimeMessage(session);
  // Set From: header field of the header.
  message.setFrom(new InternetAddress(from));
  // Set To: header field of the header.
  message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
  // Set Subject: header field
  message.setSubject(emailSubject);
  // Now set the actual message
  message.setContent("Hi "+ fname + "," + emailBodyStringEnd, "text/html");
  // Send message
  Transport.send(message);
  System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
  mex.printStackTrace();
}

this is config:

#mail config
myEmail.password = "***"
myEmail.user = "hiren.***@****.com"
myEmail.from = "****@*****.com"
myEmail.host = "smtp.office365.com"
myEmail.port = "587"
myEmail.auth = "true"
myEmail.tls = "true"

I'm using shared mailbox so user and from is different.

@ggrossetie
Copy link
Member

Thanks for sharing. I will try to find the root cause.

@ggrossetie
Copy link
Member

Sorry but I can't reproduce this issue.
Here's a working example with Play! 2.5.1 and Play mailer 5.0.0-M1:

val email = Email(
  "Hello",
  "guillaume@abc.fr",
  Seq("ggrossetie@xyz.com"),
  bodyText = Some("Hi Guillaume")
)
mailerClient.send(email)
play.mailer {
  host=smtp.office365.com
  port=587
  tls=true
  ssl=false
  user="guillaume@abc.fr"
  password="********"
  from="guillaume@abc.fr"
}

I also tried to use this configuration (similar to what you are using):

play.mailer {
  host=smtp.office365.com
  port=587
  tls=yes
  ssl=no
  debug=no
  mock=no
  user="guillaume@abc.fr"
  password="********"
  from="guillaume@abc.fr"
}

NOTE: timeout and connectiontimeout are in milliseconds. 0.6 second is a really short timeout and you should increase this timeout or use the default (ie. 60 seconds = 60000 milliseconds)

Your code is working as well, here's the Scala version:

val props = new Properties()
props.setProperty("mail.smtp.from", "guillaume@abc.fr")
props.setProperty("mail.smtp.host", "smtp.office365.com")
props.setProperty("mail.smtp.port","587")
props.setProperty("mail.smtp.auth", "true")
props.setProperty("mail.smtp.starttls.enable", "true")

val session = javax.mail.Session.getInstance(props, new Authenticator {
    override def getPasswordAuthentication = new javax.mail.PasswordAuthentication("guillaume@abc.fr","********")
})

val message = new MimeMessage(session)
message.setFrom(new InternetAddress("guillaume@abc.fr"))
message.addRecipient(Message.RecipientType.TO, new InternetAddress("ggrossetie@xyz.com"))
message.setSubject("Hello")
message.setContent("Hi Guillaume", "text/html")
Transport.send(message)

I did try to use a different address between user and from but I'm not using a shared mailbox. In this case I get the following error: 550 5.7.60 SMTP; Client does not have permissions to send as this sender.

I would be curious to understand why this is not working. Could someone give it a try ? @mkurz are you using Office 365 ?

@mkurz
Copy link
Member

mkurz commented Apr 12, 2016

@Mogztter No I am not using Office 365 right now. But I am working on a project for a client of mine and I think they use Office 365 AFAIK - so maybe I have to send out emails with this provider as well in the coming weeks. If I find out something I will let you know of course.

@ggrossetie
Copy link
Member

@mkurz Ok great, thanks 😄

@ggrossetie
Copy link
Member

Reading again this issue, I'm pretty sure the problem was a short timeout and connectiontimeout because the root cause is Caused by: java.net.SocketTimeoutException: Read timed out.

@HirenPatel2791 Do you find a solution ? Is it working now ?

@adis-me
Copy link

adis-me commented Feb 14, 2017

I can confirm it is working with Office365.com.

@HirenPatel2791
Copy link
Author

@adis-me can you share your solution.

@mkurz
Copy link
Member

mkurz commented Feb 27, 2017

@HirenPatel2791 Is your antivirus blocking smtp requests? See #115

@adis-me
Copy link

adis-me commented Feb 27, 2017

@HirenPatel2791 My configuration looks like:

smtpConfiguration = new SMTPConfiguration(
                "smtp.office365.com",                               // host
                587,                                                // port
                Boolean.FALSE,                                      // ssl
                Boolean.TRUE,                                       // tls
                Option.apply("email@domain.com"),                   // user
                Option.apply("VeryHardPassword"),                   // password
                Boolean.FALSE,                                      // debug
                Option.apply(60000),                                // timeout in ms
                Option.apply(60000),                                // connection timeout in ms
                Boolean.FALSE                                       // mock
        );

And then I send the mail:

Email email = new Email()
        .setSubject("Some subject")
        .setFrom("email@mydomain.com")
        .addTo("adis@live.nl")
        .setBodyHtml(errorBuilder.toString());

// send mail
new SMTPMailer(smtpConfiguration).send(email);

@mkurz
Copy link
Member

mkurz commented Feb 27, 2017

Thanks for sharing this @adis-me

@malviyamukul
Copy link

update spf records of that domain according to office365

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants