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 [email protected] 365 account but the .NET mail message was addressed from [email protected]. 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 ([email protected]) 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. [email protected], [email protected], etc.)
using System;
using System.Net.Mail;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (
MailMessage message = new MailMessage
{
To = { new MailAddress("[email protected]", "Recipient 1") },
Sender = new MailAddress("[email protected]", "Me"),
From = new MailAddress("[email protected]", "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("[email protected]", "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("[email protected]", "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 )
[email protected];
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 <[email protected]>
Sender: Me <[email protected]>
To: Recipient 1 <[email protected]>
-- 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 <[email protected]> on behalf of Client <[email protected]>
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?