Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29745

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

$
0
0

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. out@mydomain.com). I want the message "from" field to be the email of my user (e.g., myuser@anotherdomain.com) 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., myname@mydomain.com)

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("myuser@anotherdomain.com");
        msg.Bcc.Add(msg.From);
        string email = "receiver@gmail.com";
        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 = "out@mydomain.com", "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..."}


Viewing all articles
Browse latest Browse all 29745

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>