0

I am trying to send mail from R to outlook via mailR package, but I keep getting error whenever I try to run it. Here's my code :

library(mailR)

send.mail(
    from = "me@companydomain.com",
    to = c("coworker@companydomain.com"),
    subject = "Test Email",
    body = "testing",
    authenticate = TRUE,
    smtp = list(
      host.name = "smtp.office365.com",
      port = ` 587,
      user.name = "me@companydomain.com",
      passwd = "Password",
      tls = TRUE
    ),
  authenticate = TRUE,
  send = TRUE
)

The error is get is

    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(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.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout 60000;
  nested exception is:
    java.net.UnknownHostException: smtp.office365.com
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2053)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
    at javax.mail.Service.connect(Service.java:386)
    at javax.mail.Service.connect(Service.java:245)
    at jaNULL
vax.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
Caused by: java.net.UnknownHostException: smtp.office365.com
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:310)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2019)
    ... 13 more
Error: EmailException (Java): Sending the email to the following server failed : smtp.office365.com:587
Till
  • 3,845
  • 1
  • 11
  • 18
retrx22
  • 61
  • 1
  • 9
  • The error references gmail's SMTP server (smtp.gmail.com). That should not be the case if you are sending the mail via a company outlook installation. – Till Aug 31 '21 at 16:13
  • i have changed the error I was getting – retrx22 Aug 31 '21 at 16:26
  • Looks like the issue is not specfic to office365, as you had the same issue with gmail!? Can you send emails through them or any other SMTP from your machine with a mail client like thunderbird? Also, in your code you seem to have a misplaced tick "port =` 587". – Till Aug 31 '21 at 16:48
  • I have only tried on gmail and outlook so far and it doesn't work. I changed the tick on port and nothing happened – retrx22 Aug 31 '21 at 16:55
  • Could be a network configuration thing. Especially if you are in a company network or behind a VPN. You could try different SMTP servers and also different machines, maybe a machine that is in a different network. – Till Aug 31 '21 at 16:58
  • I tried port = 465 and ssl = true instead of port = 587 and tls = true but no luck – retrx22 Aug 31 '21 at 17:00
  • What is the exact error as returned by the SMTP server? – Eugene Astafiev Aug 31 '21 at 17:54
  • Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout 60000; nested exception is: java.net.UnknownHostException: smtp.office365.com – retrx22 Aug 31 '21 at 18:51
  • I remember mailR was difficult for me as well. Try `library(emayili)` – Coldest Aug 31 '21 at 21:28

0 Answers0