Send authenticated mails via Outlook through R using mailR package - r

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?

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"))

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.

Related

What `control` settings are valid to send automated emails from Outlook using `sendmailR`?

EDIT: I found a solution. You can find it here. I had to resort to using mailR. I could never get sendmailR to work with the Office 365 services.
I am trying to send a single email using library(sendmailR) to later send daily automated emails to about 500 people. Unfortunately, I'm suck in step 1.
It is vital that I send these emails from my institution's Outlook account. I have no idea what control settings to use in order to successfully send mails from Outlook. I checked a couple of questions but they are either using the gmail SMPT server or they do not specify the control = ... settings they used. For example:
Use sendmailR with Windows
Sending an email from R
This is what I'm working with:
from <- "<myaccount#institution.org>"
to <- "<boss#institution.org>"
subject <- "Hello from R"
body <- list("It's working.")
sendmail(from, to, subject, body,
control = list(smptServer = "oultook.office.365", port = 443))
And I'm getting the following error message:
Error in wait_for(code) :
SMTP Error: 5.7.57 SMTP; Client was not authenticated to send anonymous mail
during MAIL FROM [SN4PR0501CA0061.namprd05.prod.outlook.com]
I believe I'm not specifying the port correctly. The sendmaiLR documentation is not specific enough, but something tells me I should be writing the port as "port 443"or something along those lines.
Does anyone know what controlsettings I should be using?

Why emails are getting stuck in pick-up folder when sending through SMTP Service and CDO?

I'm programming Classic Asp with IIS8.5, so for sending email I use the SMTP Service (not the SMTP Server) with the CDO component. I want to send email using the pickup directory option:
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1
So my code is:
Set objConfig = Server.CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
With Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp-relay.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "The-pwd-123"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") ="C:\inetpub\correos\"
.Update
End With
Set objMessage = Server.CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
With objMessage
.BodyPart.Charset = "unicode-1-1-utf-8"
.To = "YYY <yyy#gmail.com>"
.From = "XXX <xxx#gmail.com>"
.Subject = "This is a subject"
.TextBody = "This is the body of the message"
.Send
End With
As you may know IIS8.5 come without SMTP Server, so there is no folder for the pickup directory (or I haven't found it yet), you must created, the problem I'm having is that the emails are getting stuck into the folder I created for that purpose.
https://support.google.com/a/answer/176600?hl=es
When you use the cdoSendUsingPickup option, you are simply writing the email message to a file which will be queued up for delivery asyncronously from your code. Yes, this is dramatically faster as your code does not have to wait for the SMTP server to receive the message. So, when using this option, you do not need to specify the destination SMTP server, the port, the username, password, etc.
Hopefully you are now asking yourself how does the server know where to send the message to if I don't specify a forwarding SMTP server. Well, that's because you just have to configure the Microsoft SMTP Service to let it know what server to hand off the messages to. There are plenty of articles online to configure it so you should not have any trouble finding that.

Unable to send email from web application

I've looked this up and it seems other people have had the same issue mainly because Gmail was blocking the application's access, however I've turned on the "allow less secure apps to connect" option and the problem remains.
Here's the complete error message I get from ASP.net when I try to connect.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
I've tried connecting using port 465 and I get a time-out (I assumed it was the Explicit/Implicit SSL problem)
Here's the code that I'm using to send the emails.
Dim Mail As New MailMessage
Dim EmailToMail As New MailAddress(EmailTo, EmailToName)
Mail.To.Add(EmailToMail)
Dim FromE As New MailAddress(EmailFrom, "Something")
Mail.From = FromE
Mail.Body = Body
Mail.Subject = Title
Dim SmtpClient As New SmtpClient("smtp.gmail.com", 587)
SmtpClient.UseDefaultCredentials = False
Dim SmtpCredentials As New System.Net.NetworkCredential("MyEmail", "MyPassword")
SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpClient.EnableSsl = True
SmtpClient.Send(Mail)
I've also tried setting UseDefaultCredentials to True, which didn't work.
your credentials are stored only within variable not within smtp instance.
set
SmtpClient.Credentials = SmtpCredentials
also make sure you use System.Net.Mail
This can happen if your password no longer meets Gmail's complexity criteria. Try logging in and changing it.
There can be a few issues.
Your firewall could be blocking it... (bypass it)
The port is blocked... (you need to allow it)
If you have two-step verification enabled on your email account...
Your credentials you are creating are never applied to the server credentials.
I have already answered this same question before here and will answer all of your problems. It also includes a link for sending the emails that I have answered as well.

Non-interactive passwords in R

I'd like to revisit an old SO question about passwords. Like the OP, I don't want to enter my password interactively, but I am confused about how to store the password securely on my machine and share scripts with colleagues (and I'd add specifically, push scripts to GitHub). The accepted answer involves storing the password in .Rprofile, but one commenter suggests that this is not a good idea.
In my specific use case, I have a script that runs every day on a virtual machine that I want other members of my team to access. At the end of the run, it sends an email with the mailR package. This code looks for my gmail password. I've set up 2-step authentication, so mypassword is a third-party password, not my actual gmail password. Still, I am hesitant to share this with others. I'd like to be able to push the script to a private git repo.
send.mail(from = "myemail#gmail.com",
to = tolist,
subject = "my subject",
body = "my message",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = "myusername",
passwd = "mypassword",
ssl = TRUE),
authenticate = TRUE,
html = FALSE,
send = TRUE)
How would you store mypassword?
Storing it in .Rprofile seems to be an option, but I don't know if there are downsides like the one mentioned in response to the accepted answer in the question I linked to.
I could store it in another file like auth.R and run source('auth.R') before send.mail, but this would put the password in the global environment.
Other ideas?

ASP.NET fails to send email through Gmail SMTP, but Outlook 2007 succeeds

Hi all
a couple weeks ago, my ASP.NET website stopped sending user comments which I implemented by emailing those comments to my email account through Gmail SMTP server (smtp.gmail.com). I opened the project on my development machine and again it fails to send the email after a couple minutes with the following exception message:
Failure sending mail
Unable to read data from the transport connection: net_io_connectionclosed.
I'm using Gmail's SMTP with port 465, and MailClient.EnableSSL = True.
The weird thing is my Office Outlook 2007 is using the same settings and it doesn't have any problems sending mail using the same Gmail account.
Any thoughts?
This code works for me. Note the port number is different to the 465 you are trying. I'm almost certain I also tried 465 first with no luck.
var msg = new MailMessage(new MailAddress(recipient, "SCS Web Site"), new MailAddress(toAddress))
{
Body = BuildMailBody(),
Subject = "SCS Web Site Message"
};
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(myGmailUser, myGmailPassword)
};
smtp.Send(msg);
You wouldn't happen to have a firewall disallowing outbound connections here then, say, specifically allowed outlook to punch through . . .
One oddity I found with formmail going through gmail (on Windows server) was that the port number was the problem. My code was similar to yours, but it only worked when I changed the port to 25 rather than the 587 or 465. That might be what you need here.

Resources