0

I'm trying to automate sending emails in R using mailR (it would not let me tag mailR, so I tagged it with a similar package sendmailR) from my company email.

I used the code from this post (Send authenticated mails via Outlook through R using mailR package), and while it works for my school outlook email, it does not work from my company:

send.mail(from = "me@companydomain.com",
to = c("coworker@companydomain.com"),
subject = "Test Email",
body = "Hi Coworker! Did this email send?",
authenticate = TRUE,
smtp = list(host.name = "smtp.office365.com",
port = 587,
user.name = "me@companydomain.com",
passwd = "Password1",
sls = TRUE,
tls = TRUE),
debug = TRUE)

I am 100% sure that I am using the correct username, password, etc, but I receive this error message when using my company email only:

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:1410)
    at org.apache.commons.mail.Email.send(Email.java:1437)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at RJavaTools.invokeMethod(RJavaTools.java:386)
Caused by: javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [MN2PR05CA0017.namprd05.prod.outlook.com]

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:892)
    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:1400)
    ... 6 more
NULL
Error: EmailException (Java): Sending the email to the following server failed : smtp.office365.com:587

Last, because I was receiving this error, I turned on debug, and this is the report from it. From what I can gather, my email is being authenticated the first time and connecting to the server, but at the end, it's failing an authentication a second time. To my knowledge, we do not have 2 factor authentication unless something is happening under the hood.

DEBUG: JavaMail version 1.5.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
220 MN2PR05CA0017.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 31 May 2019 12:15:26 +0000
DEBUG SMTP: connected to host "smtp.office365.com", port: 587

EHLO WLPC0ER8VM.wks.companydomain.com
250-MN2PR05CA0017.outlook.office365.com Hello [50.205.156.92]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "157286400"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO WLPC0ER8VM.wks.companydomain.com
250-MN2PR05CA0017.outlook.office365.com Hello [50.205.156.92]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-AUTH LOGIN XOAUTH2
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "157286400"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN XOAUTH2"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", 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

I want to be able to send these automated emails while still maintaining security. Is this possible and if so how? I know the errors mention things in Java, which I do not know, so perhaps that is the key? Thank you!

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
J.Sabree
  • 2,280
  • 19
  • 48
  • I know that sometimes with gmail there is a connection problem with R and mailR because Google can block the connection with "less secure apps". It can be remove in the gmail settings. Could it be possible that your company email got a similar security setting? – Gainz May 31 '19 at 13:20
  • It could also be a firewall/port problem. Could someone from the IT at your job check which port are open, just to be sure? – Gainz May 31 '19 at 13:28
  • @Gainz, good idea, and I'm not sure. I'll reach out to someone on Monday and report back. As much as I want to increase efficiency, I want to maintain security as well. – J.Sabree May 31 '19 at 13:35
  • Yes security is also important. Tag me in the comments if you find your answer, I'm not using mailR often but at least I'll know if I have to use it again, – Gainz May 31 '19 at 13:41

1 Answers1

0

Try this:

send.mail(from = "me@companydomain.com",
to = c("coworker@companydomain.com"),
subject = "Test Email",
body = "Hi Coworker! Did this email send?",
smtp = list(host.name = "companydomain.mail.protection.outlook.com", port = 25, user.name   = "me@companydomain.com", passwd = "Password1"),
authenticate = TRUE,
debug = TRUE)

Note that I have changed the SMTP host name from smtp.office365.com to companydomain.mail.protection.outlook.com, and set port = 25.

I learnt this from Microsoft's Office 365 documentation pages.

mixedbag99
  • 529
  • 1
  • 4
  • 17