I have an Office 365 Family subscription (thus using outlook.com) and are trying to send email from a C# application I'm working on. Does anybody know if this is even possible? From my research there seems to be a lot of people having issue with this approach but I'm trying hard to find out if it's supported
CodePudding user response:
There are different ways of doing the required job:
- Use standard .net mechanisms, the same question was posted here:
using (SmtpClient client = new SmtpClient()
{
Host = "smtp.office365.com",
Port = 587,
UseDefaultCredentials = false, // This require to be before setting Credentials property
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("[email protected]", "password"), // you must give a full email address for authentication
TargetName = "STARTTLS/smtp.office365.com", // Set to avoid MustIssueStartTlsFirst exception
EnableSsl = true // Set to avoid secure connection exception
})
{
MailMessage message = new MailMessage()
{
From = new MailAddress("[email protected]"), // sender must be a full email address
Subject = subject,
IsBodyHtml = true,
Body = "<h1>Hello World</h1>",
BodyEncoding = System.Text.Encoding.UTF8,
SubjectEncoding = System.Text.Encoding.UTF8,
};
var toAddresses = recipients.Split(',');
foreach (var to in toAddresses)
{
message.To.Add(to.Trim());
}
try
{
client.Send(message);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
The SmtpClient
is usable in .NET Core, but its use isn't recommended. Instead, consider using https://github.com/jstedfast/MailKit .
- Use Graph API.
CodePudding user response:
And for anybody else having problems with using smtp.office365.com and sending email through code. You need to add an "app password" in your Microsoft Account. The regular password you use to login to outlook.com will not work