J.Sabree
J.Sabree

Reputation: 2536

Outlook suppresses authentication with mailR in R for certain email addresses

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 = "[email protected]",
to = c("[email protected]"),
subject = "Test Email",
body = "Hi Coworker! Did this email send?",
authenticate = TRUE,
smtp = list(host.name = "smtp.office365.com",
port = 587,
user.name = "[email protected]",
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!

Upvotes: 0

Views: 1408

Answers (1)

mixedbag99
mixedbag99

Reputation: 529

Try this:

send.mail(from = "[email protected]",
to = c("[email protected]"),
subject = "Test Email",
body = "Hi Coworker! Did this email send?",
smtp = list(host.name = "companydomain.mail.protection.outlook.com", port = 25, user.name   = "[email protected]", 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.

Upvotes: 0

Related Questions