We are testing the new Office 365 beta, and i have a mail account on the Exchange Online service. Now I\'m trying to connect a LOB application that can send smtp emails from
In year of 2020, these code seems to return exception as
System.Net.Mail.SmtpStatusCode.MustIssueStartTlsFirst or The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
This code is working for me.
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("alias@fulldomain.com", "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("alias@fulldomain.com"), // 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);
}
}
Fixed a few typos in the working code above:
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("someone@somedomain.com", "SomeOne"));
msg.From = new MailAddress("you@yourdomain.com", "You");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("your user name", "your password");
client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
client.Send(msg);
lblText.Text = "Message Sent Succesfully";
}
catch (Exception ex)
{
lblText.Text = ex.ToString();
}
I have two web applications using the above code and both work fine without any trouble.
Here is a side note for some that may be searching this thread for an answer to this problem. (Be sure to read cautions at the bottom before implementing this solution.) I was having trouble sending emails for a client to which my MS Office 365 subscription did not have a user or domain for. I was trying to SMTP through my Me@MyDomain.com 365 account but the .NET mail message was addressed from Client@ClientDomain.com. This is when the "5.7.1 Client does not have permissions" error popped up for me. To remedy, the MailMessage class needed to have the Sender property set to an email address that my supplied SMTP credentials had permission in O365 to "Send As". I chose to use my main account email (Me@MyDomain.com) as seen in the code below. Keep in mind I could have used ANY email address my O365 account had permission to "send as" (i.e. Support@MyDomain.com, no-reply@MyDomain.com, etc.)
using System;
using System.Net.Mail;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (
MailMessage message = new MailMessage
{
To = { new MailAddress("Recipient1@Recipient1Domain.com", "Recipient 1") },
Sender = new MailAddress("Me@MyDomain.com", "Me"),
From = new MailAddress("Client@ClientDomain.com", "Client"),
Subject=".net Testing"
Body="Testing .net emailing",
IsBodyHtml=true,
}
)
{
using (
SmtpClient smtp = new SmtpClient
{
Host = "smtp.office365.com",
Port = 587,
Credentials = new System.Net.NetworkCredential("Me@MyDomain.com", "Pa55w0rd"),
EnableSsl = true
}
)
{
try { smtp.Send(message); }
catch (Exception excp)
{
Console.Write(excp.Message);
Console.ReadKey();
}
}
}
}
}
}
Please note SmtpClient is only disposable and able to use the Using block in .NET Framework 4
Users of .NET Framework 2 through 3.5 should use SmtpClient as such...
SmtpClient smtp = new SmtpClient
{
Host = "smtp.office365.com",
Port = 587,
Credentials = new System.Net.NetworkCredential("Me@MyDomain.com", "Pa55w0rd"),
EnableSsl = true
};
try { smtp.Send(message); }
catch (Exception excp)
{
Console.Write(excp.Message);
Console.ReadKey();
}
The resulting email's header will look something like this:
Authentication-Results: spf=none (sender IP is )
smtp.mailfrom=Me@MyDomain.com;
Received: from MyPC (192.168.1.1) by
BLUPR13MB0036.namprd13.prod.outlook.com (10.161.123.150) with Microsoft SMTP
Server (TLS) id 15.1.318.9; Mon, 9 Nov 2015 16:06:58 +0000
MIME-Version: 1.0
From: Client <Client@ClientDomain.com>
Sender: Me <Me@MyDomain.com>
To: Recipient 1 <Recipient1@Recipient1Domain.com>
-- Be Cautious --
Be aware some mail clients may display the Sender address as a note. For example Outlook will display something along these lines in the Reading Pane's header:
Me <Me@MyDomain.com> on behalf of Client <Client@ClientDomain.com>
However, so long as the email client the recipient uses isn't total garbage, this shouldn't effect the Reply To address. Reply To should still use the From address. To cover all your bases, you can also utilize the MailMessage.ReplyToList property to afford every opportunity to the client to use the correct reply address.
Also, be aware that some email servers may flat out reject any emails that are Sent On Behalf of another company siting Domain Owner Policy Restrictions. Be sure to test thoroughly and look for any bounce backs. I can tell you that my personal Hotmail (mail.live.com) email account is one that will reject messages I send on behalf of a certain client of mine but others clients go through fine. Although I suspect that it has something to do with my client's domain TXT "spf1" records, I do not have an answer as to why it will reject emails sent on behalf of one domain versus another. Maybe someone who knows can shed some light on the subject?
I've ported c# code used to work against smtp.google.com:587 to work via office365 without success. Tried all combinations of Credential before/after using Ssl and almost every recommendation made on the Internet - w/o success (got 5.7.1 .... error).
Finally got this line from somewhere as last resort, just before .Send(message)
smtpClient.TargetName = "STARTTLS/smtp.office365.com";
Since then - every send() is big success.
Finally, Works!
Put smtpClient.UseDefaultCredentials = false;
after smtpClient.Credentials = credentials;
then problem resolved!
SmtpClient smtpClient = new SmtpClient(smtpServerName);
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(smtpUName, smtpUNamePwd);
smtpClient.Credentials = credentials;
smtpClient.UseDefaultCredentials = false; <-- Set This Line After Credentials
smtpClient.Send(mailMsg);
smtpClient = null;
mailMsg.Dispose();
Have you seen this? Sending email using Smtp.mail.microsoftonline.com
Setting the UseDefaultCredentials after setting the Credentials would be resetting your Credentials property.