3

I've looked throughout the site but have not been able to find an answer.

I need to use R to send emails via my works outlook email. It has to be from Outlook, not anywhere else.

Problem is, the computer I'm using is OSX so RDCOMClient won't work.

EDIT: Tried this and it wouldn't work.

sender<-"myemail@outlook.com"
recipients<-c("myemail@outlook.com")
send.mail(from = sender, to = recipients,
          subject = "Test",
          body = BodyOfMessage,
          smtp = list(host.name = "smtp-mail.outlook.com"),
          authenticate = FALSE,
          html = TRUE,
          send = TRUE)Does anyone have a workaround? 

And it resulted in this error:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp-mail.outlook.com:25 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(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at RJavaTools.invokeMethod(RJavaTools.java:386) Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [BN6PR19CA0117.namprd19.prod.outlook.com]

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2202) at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1693) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1194) at javax.mail.Transport.send0(Transport.jaNULL va:254) at javax.mail.Transport.send(Transport.java:124) at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400) ... 6 more Error: EmailException (Java): Sending the email to the following server failed : smtp-mail.outlook.com:25

2 Answers2

1

So, you wouldn't necessarily be sending this through your Outlook client, which is all that Outlook is. You would want to allow the R script you write and the libraries employed to be an email client. I use mailR with a lot of success. Some people like sendmailR for sending messages. They both have their advantages. Your email administrator might allow unauthenticated sending if you run a lot of scripting from a host. Or you can authenticate in your script. For example:

library(mailR)
#################
# Generate Spam #
#################
BodyOfMessage <- paste("<html><body><p>Hello,</p><p>This is an email message.</p>
                      <hr>
                      <p>The second table is a list of users that need to be toggled in the system, by adding them to the correct securitygroup.</p>
                      <p>", toggle.these.people, "</p>
                      <p>Scott</p></body></html>")
#mailR
    sender<-"fromwho@fromyou.org"
    recipients<-c("emailtosendto@email.com")
    send.mail(from = sender, to = recipients,
    subject = paste("Blah. Created: today.", sep = ""),
    body = BodyOfMessage,
    smtp = list(host.name = "smtp.exchangeserver.org"),
    authenticate = FALSE,
    html = TRUE,
    attach.files = CSVFileNameIs,    
    send = TRUE)
Scottieie
  • 304
  • 8
  • 14
  • Thank you. What do you mean by `You would want to allow the R script you write and the libraries employed to be an email client`? – Karl Marxdown Apr 04 '18 at 00:59
  • No problem. Instead of using a mail client such as Outlook, let the R script and the library be the client. The mailR or sendmailR package (others exist too) negotiate with the server, acting as the client. Your script supplies the correct parameters, such as the smtp address, message, subject, from, to, credentials..... – Scottieie Apr 04 '18 at 01:03
  • Thanks. I just posted some changes in my original post using what you gave me. I take it I just need to get my authentication permissions changed? – Karl Marxdown Apr 04 '18 at 16:42
  • https://github.com/rpremraj/mailR has a few examples of authenticated sending. Based on this message, authentication is the issue, so either authenticate in your script, or get the mail admin to allow unauthenticated sending from certain hosts. – Scottieie Apr 04 '18 at 17:47
0

This is what I use and it works fine for me.

library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email 
outMail = OutApp$CreateItem(0)
## configure  email parameter 
outMail[["To"]] = "ryanshuell@gmail.com"
outMail[["subject"]] = "some subject"
outMail[["body"]] = "some body"
## send it                     
outMail$Send()
ASH
  • 20,759
  • 19
  • 87
  • 200