vkyczh
vkyczh

Reputation: 1

I want to send email (send as) with C# to Office365 but I get an error

As I've written in the title, I get an error:

5.7.62 SMTP; Client does not have permissions to send on behalf of the from address

I want to use C# to send an email using account2, but as if it were account1. Which means that account2 will send the email, but when the recipient receives the email, they will see account1 as the sender.

But if I use another account (not from Office365) instead of account2 (real sender), there is no exception.

Or if I use another account (not from Office365) instead of account1 (display sender), there is no exception.

And if I send with foxmail client, everything is right.

I've tried to search the problem on the internet for a few days, but still cannot solve this problem. Can you give me some advice?

My code:

static public void Send(string from, string fromDisplay, string fromPwd, string to, string toDisplayName) 
{
    //account1: an  account from a office365 email server to (to display on the sender when received)
        var emailFrom = new MailAddress("EmailToDisplayToReceiver@<theserver>.com", "test");

        //account2:The sender is another account from an office365 email server(The real sender to send the email )
        var sender = new System.Net.Mail.MailAddress(from, fromDisplay); 
        System.Net.Mail.MailAddress EmailTo = new System.Net.Mail.MailAddress(to, toDisplayName);
        System.Net.Mail.MailMessage Email = new System.Net.Mail.MailMessage(emailFrom, EmailTo);
        Email.Sender = sender;
        Email.Body = "The content";
        Email.SubjectEncoding = System.Text.Encoding.Default;
        Email.BodyEncoding = System.Text.Encoding.Default;
        Email.IsBodyHtml = true;
        Email.Priority = System.Net.Mail.MailPriority.High;

        System.Net.Mail.SmtpClient SmtpPC = new System.Net.Mail.SmtpClient("smtp.office365.com", 587); 
        SmtpPC.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        SmtpPC.EnableSsl = true;
        SmtpPC.UseDefaultCredentials=false;

        //the real sender and password
        SmtpPC.Credentials = new System.Net.NetworkCredential(from, fromPwd);  
        ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
        try
        {
            SmtpPC.Send(Email);
        }
        catch (Exception E)
        {
        //Here throwed an exception which said:
        //Mailbox is unable to use.The server response is: 5.7.62 SMTP; Client does not have permissions to send on behalf of the from address
        //(邮箱不可用。 服务器响应为: 5.7.62 SMTP; Client does not have permissions to send on behalf of the from address)
            throw;
        }
    }

Upvotes: 0

Views: 2385

Answers (1)

Yaser
Yaser

Reputation: 5719

This usually happens when the from address is fake or is not defined in the directory in which you are trying to send email on behalf of. There a couple of things to try:

  • Try giving the authenticating account proper Send As / On Behalf Of permissions to the From address account.
  • Make sure the actual email header From and Sender are using the same address.
  • Giving the sender account permissions on each From user.

If you need more info you can check out this link, and read the Use shell to assign permissions section.

Upvotes: 1

Related Questions