nguyen.hoang
nguyen.hoang

Reputation: 23

How to resolve an issue with sending message to SMTP Server (mailR+Outlook)

I have been setting the mailR package to send emails from my Outlook account, but so far I have been unable to make it work smoothly, I got the result sometime send email successfully and sometime is failed.

The code is as following (with emails and passwords replaced with 'aaa' and 'bbb' for privacy/security):

  send.mail(from = "[email protected]",
            to = "[email protected]",
            subject = subject,
            body = 'Test Successful',
            encoding = 'utf-8',
            html = TRUE,
            inline = TRUE,
            attach.files = filenamex,
            smtp = list(host.name = 'smtp.office365.com',
                        port = 587,
                        user.name="[email protected]",
                        passwd= "abc",
                        tls = TRUE                       
            ),
            authenticate = TRUE,
            send = TRUE)

When I run the code with schedule every 5 minutes, I get the following result

2. send email at 2021-10-04 20:55:01 +07 failed !
1. send email at 2021-10-04 21:00:01 +07 successful !
2. send email at 2021-10-04 21:05:01 +07 failed !
2. send email at 2021-10-04 21:10:02 +07 failed !
2. send email at 2021-10-04 21:15:01 +07 failed !
1. send email at 2021-10-04 23:03:09 +07 successful !
2. send email at 2021-10-04 23:05:01 +07 failed !
1. send email at 2021-10-04 23:10:01 +07 successful !
2. send email at 2021-10-04 23:15:01 +07 failed !
1. send email at 2021-10-04 23:20:01 +07 successful !
1. send email at 2021-10-04 23:25:01 +07 successful !
1. send email at 2021-10-04 23:30:02 +07 successful !

Any idea what is going wrong?

Upvotes: 1

Views: 395

Answers (1)

manro
manro

Reputation: 3677

Example with RDCOMClient:

install.packages("RDCOMClient", repos = "http://www.omegahat.net/R")
#or devtools::install_github("omegahat/RDCOMClient")

# Load the DCOM library
library (RDCOMClient)

# Open Outlook
Outlook <- COMCreate("Outlook.Application")

# Create a new message
Email = Outlook$CreateItem(0)

# Set the recipient, subject, and body
Email[["to"]] = "[email protected]; [email protected]; 
[email protected]"
Email[["cc"]] = ""
Email[["bcc"]] = ""
Email[["subject"]] = "Quarterly Sales Analysis Updated"
Email[["body"]] = 
"The quarterly sales analysis has been updated.  
You can find it at: D:\\Reports\\Sales Analysis.xlsx"

# Send the message
Email$Send()

# Close Outlook, clear the message
rm(Outlook, Email)

See more there

Example with emayili

library(emayili)

#making an email
email <- envelope() %>%
from("[email protected]") %>%
to(c("Recipient 1 <[email protected]>", "Recipient 2 
<[email protected]>")) %>%
cc("[email protected]") %>%
bcc("[email protected]") %>%
reply("[email protected]") %>%
subject("Test email subject") %>%
body("Test email body")

#configuring the SMTP
smtp <- server(host = "smtp.mailtrap.io",
        port = 25,
        username = "********",
        password = "*********")

#sending    
smtp(email, verbose = TRUE)

See more there

Upvotes: 1

Related Questions