Sent an email from asp.net through 365 with a "from" email that is different than the username

44 views Asked by At

I hope someone can help me out here:

Users generate emails through my asp.net website. Emails are to be sent out through the 365 server using an account that I have there with my domain (e.g. [email protected]). I want the message "from" field to be the email of my user (e.g., [email protected]) so that the receivers of the email will see his email as the sender and will reply to him directly.

But when I try this I get an error message (see below).

I get the same error message even if I try to send an email when the "from" field is another existing mailbox in my domain (e.g., [email protected])

Apparently someone in the way (not sure if the asp.net or the 365 server) blocks emails if the "from" is not identical to the username.

Is there any way to address this>

here is my code

protected void btnSend_Click(object sender, System.EventArgs e)
{
    AuditLog.Info("here");

    try
    {
        string EmailContent = "test";
        MailMessage msg = new MailMessage();
        msg.IsBodyHtml = true;
        msg.From = new MailAddress("[email protected]");
        msg.Bcc.Add(msg.From);
        string email = "[email protected]";
        msg.To.Add(email);
        msg.Subject = "TEst 365";

        System.Net.Mail.AlternateView plainTextView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(EmailContent);
        System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(EmailContent);
        msg.AlternateViews.Add(plainTextView);
        msg.AlternateViews.Add(htmlView);
        System.Net.Mail.SmtpClient client = new SmtpClient();

        client.Host = "smtp.office365.com";
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = "[email protected]", "MyPassword");
            client.Port = 587;
            client.EnableSsl = true;

            if (client.Host.Trim() != "")
                client.Send(msg);
   }
   catch (Exception ex)
   {
         AuditLog.Info(string.Format("Failed to send mail . Error={0} ",  ex.Message));
   }
   finally
   {
   AuditLog.Info("end");
   }

}


And this is the error:


{"Transaction failed. The server response was: 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 0.35250:0A00A280, 1.36674:0A000000, 1.61250:00000000, 1.45378:02000000, 1.44866:FD1E0000, 1.36674:0E000000, 1.61250:00000000, 1.45378:021F0000, 1.44866:14030000, 16.55847:AD0F0000, 17.43559:0000000004020000000000000000000000000000, 20.52176:140F2A8A0A00101043050000, 20.50032:140F2A8A7A17000000000000, 0.35180:48050000, 255.23226:0A00A780, 255.27962:0A000000, 255.27962:0E000000, 255.31418:0A00A880, 0.35250:0A000000, 1.36674:0A000000, 1.61250:00000000, 1.45378:02000000, 1.44866:20000000, 1.36674:32000000, 1.61250:00000000, 1.45378:25000000, 1.44866:01000000, 16.55847:8C000000, 17.43559:0000000030030000000000007B00000000000000, 20.52176:140F2A8A0A0070200A00AD80, 20.50032:140F2A8A7A1710106B050000, 0.35180:0A00AE80, 255.23226:4800D13D, 255.27962:0A000000, 255.27962:32000000, 255.17082:DC040000, 0.27745:75050000, 4.21921:DC040000, 255.27962..."}

1

There are 1 answers

1
Stephen Wuebker On

It's right there in the error message: Exception:SendAsDeniedException.MapiExceptionSendAsDenied;

You will need to grant your users SendAs permissions for [email protected]. That's probably not what you really want to do though, as those users would then consume an office 365 license and if they were using a license, they could just send as themselves.

The real solution is to not use office 365 as an SMTP relay. You should sign up with another SMTP provider that is specifically set up to do what you are trying to do. We use SendGrid, but there are others out there if you search.