0

I think I have the exact same problem as this question. I mean literally, it might be the exact same. But it seems like that never got an answer so I'll post mine.

I'm trying to get R to send an email. (Ultimately it'll be part of trycatch and will email me when R errors, but babysteps).

The code is running from my work computer which is behind all sorts of firewall/microsoft protection/vpn that might be causing issues.

I've successfully got it to work using this code (notable because it means it CAN send emails):

library(Microsoft365R)
my_outlook <- get_personal_outlook()

my_email <- my_outlook$create_email(content_type = "html")$
set_body("<p>Testing to see if R can email</strong>.</p>")$
 set_subject("Did R email you?")$
set_recipients(to = c("Person@mywork.org", "Person2@mywork.org"))$
  send()

The problem is that this pops open a chrome window that asks me to login elsewhere. I'm hoping to get this R code running on a virtual machine that wont be actively watched. I.e. a script will run the R code daily at 1pm, and nobody will be there to watch it and click around in a browser window if it needs to log in.

So instead of a Microsoft365R solution, I'm trying to use this:

library(mailR)
sender<-"MyPersonal@hotmail.com"
recipients<-c("Mywork@myWork.org")

email<-send.mail(from=sender, to=recipients, subject="Test Email", body="Testing to see if R can email", smtp = list(host.name = "smtp.live.com", port = 587,user.name = "MyPersonal@hotmail", passwd = "MyPassword", tls = TRUE), authenticate=FALSE, send=TRUE)

But I get this error:

EmailException (Java): Sending the email to the following server failed : smtp.live.com:587

I've also tried my gmail instead of my hotmail:

send.mail(from = "mypersonal@gmail.com",
          to = "mypersonal@gmail.com",
          subject = "test",
          body = "test",
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "myUsername", passwd = "MyPassword", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

And I get this error:

EmailException (Java): Sending the email to the following server failed : smtp.gmail.com:465[1] "Java-Object{org.apache.commons.mail.SimpleEmail@496bc455}"

Any ideas? The errors are so vague I'm not sure what to change.

Joe Crozier
  • 944
  • 8
  • 20
  • 2
    This is likely not an R issue, but something to do with your OS. You need to have an SMTP server running on your computer. I have no idea how to do this in Windows. If you run R in a Linux container or VM, it will work if you install the sendmail package. – BigFinger May 26 '23 at 15:00
  • Seconds after I posted this I got it to work with library(RDCOMClient) instead. I figured I'd leave it in case others had this issue, but if its not an R issue as you said, I may delete it. – Joe Crozier May 26 '23 at 15:02
  • 1
    I suggest you answer that other question (or this one) with that solution, and then we can link that as a duplicate. – Axeman May 26 '23 at 15:06
  • I'll answer this one, as I'm not sure my solution would work perfectly for that one. – Joe Crozier May 26 '23 at 15:08

1 Answers1

0

I was able to get this email to send by using the RDCOMClient package instead:

library(RDCOMClient)
Outlook <- COMCreate("Outlook.Application")
Email = Outlook$CreateItem(0)
Email[["to"]] = "workperson@work.org"
Email[["subject"]] = "Test email subject"
Email[["body"]] = "Test email body"
Joe Crozier
  • 944
  • 8
  • 20