3

How can I send mails from R via Outlook?

I was told to use the sendmailR package, but I could not figure out how to specify certain control settings (such as port, username and password). I was also redirected to this post, but it did not help.

I switched to the mailR package. I can send mails from other servers, such as smtp.gmail.com, but I do not know the Outlook server details. What are the protocol, server and port details required to send mails via Outlook using mailR?

Arturo Sbr
  • 5,567
  • 4
  • 38
  • 76
  • Doest this question refers to (a) sending email message to any SMTP server, to (b) the Microsoft Outlook service on the Internet or just to (c) inserting a message into a Windows application's (MS Outlook) queue (possibly PST/OST file)? – cineS. May 17 '22 at 12:17

2 Answers2

2

This took me a while to figure out. Try this:

send.mail(from = "username@custom.org",
          to = c("recipient1@custom.org", "recipient2@custom.org"),
          subject = "Title",
          body = "Hello from R.",
          authenticate = TRUE,
          smtp = list(host.name = "smtp.office365.com",
                  port = 587,
                  user.name = "username@custom.org",
                  passwd = "Pa55w0rd",
                  tls = TRUE))

It is a common misconception that the port is 25 or 447. I believe port 25 can only be used whenauthenticate = FALSE.

Many sources claim that the correct server is smtp-mail.outlook.com. Perhaps you could try this in the event that the code does not work. Moreover, do not use ssl = TRUE. It has to be tls = TRUE.

Shoutout to Rahul Premraj's answer to this archived 2014 question.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Arturo Sbr
  • 5,567
  • 4
  • 38
  • 76
2

Or you can use DescTools::SendOutlookMail()

library(DescTools)
SendOutlookMail(to = c("me@microsoft.com", "you@rstudio.com"), 
                subject = "Some Info", 
                body = "Hi all\r Find the files attached\r Regards, Dude", 
                attachment = c("C:/temp/fileA.txt", 
                               "C:/temp/fileB.txt"))
Andri Signorell
  • 1,279
  • 12
  • 23
  • I tried this but I get the following error: 80020009 No support for InterfaceSupportsErrorInfo checkErrorInfo -2147352567 Error: Exception occurred. In addition: Warning message: In RDCOMClient::getCOMInstance(app) : creating a new instance of Outlook.Application rather than connecting to existing instance. – NBE May 17 '20 at 14:16
  • 1
    Did you check, that all the attachments exist? Try with a working email address, subject and body only and report. – Andri Signorell May 18 '20 at 13:57