none
Error sending emails via Outlook.com with custom app RRS feed

  • Question

  • Hi,

    I'm developing a custom web app with ASP.NET MVC 5. I'm trying to get my app to send e-mail notifications via an Outlook.com email account. The settings I use is the standard Outlook.com SMTP settings below:

    SMTP Server:   smtp-mail.outlook.com

    Port:   587

    Encryption:   TLS

    I tested my code with Gmail as well as another SMTP account, and it works 100%. But when I tried this with my Outlook Mail account and password it throws the Exception below. I also tried using an app password that I created in Outllok.com.

    I keep getting the following Exception:

    ============================================>

    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. 16.55847:6C0E0000, 17.43559:0000000094000000000000000000000000000000, 20.52176:140F38840A004010F1030000, 20.50032:140F38847A174010F1030000, 0.35180:0A00BE81, 255.23226:F1030000, 255.27962:0A000000, 255.27962:0E000000, 255.31418:1B100000, 16.55847:8A000000, 17.43559:0000000068010000000000000000000000000000, 20.52176:140F38840A004021140F3884, 20.50032:140F38847A1700100A00F836, 0.35180:9E010000, 255.23226:0A000000, 255.27962:0A000000, 255.27962:32000000, 255.17082:DC040000, 0.27745:00000000, 4.21921:DC040000, 255.27962:FA000000, 255.1494:B6010000, 0.37692:0F010480, 0.37948:A71B0100, 5.33852:00000000534D545000393332, 4.56248:DC040000, 7.40748:010000000000010B31303A44, 7.57132:00000000000000003A653834, 1.63016:32000000, 4.39640:DC040000, 8.45434:A71B01002F4C2CCF000000000000000000000480, 5.10786:0000000031352E32302E313038302E3031303A444236...

    ============================================>

    So it seems that the error I'm getting is only with Outlook.com.

    Can anyone shed some light on this error?

    Below are the code I have for C#. I use the Appsettings section in Web.Config to store my SMTP server settings:

    The HomeController:

    using System;
    using System.Web.Mvc;
    using PrepaidServicesWebsite.ViewModels;

    namespace PrepaidServicesWebsite.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }

            public ActionResult About()
            {
                ViewBag.Message = "About Us";
                return View();
            }


            [HttpGet]
            public ActionResult Contact()
            {
                ViewBag.Message = "";
                return View();
            }

    [HttpGet] public ActionResult Contact() { ViewBag.Message = ""; return View(); } [HttpPost] public ActionResult Contact(ContactViewModel vm) { if (ModelState.IsValid) { try { var fromName = vm.Name; var fromAddress = vm.Email; var fromTelNo = vm.TelNo; var mailSubject = vm.Subject; string mailMessage = ""; mailMessage = "<br/>Name :" + vm.Name; mailMessage = mailMessage + "<br/>Email: " + vm.Email; mailMessage = mailMessage + "<br/>Phone: " + vm.TelNo; mailMessage = mailMessage + "<br/>Message: " + vm.Message; string mailBody = "Hi, <br/><br/> A new enquiry was received from prepaid-services.co.za. Detail is as follows:<br/><br/> " + mailMessage + "<br/><br/>Thanks"; bool result = false; var eMail = new MailSystemController(); result = eMail.SendEmail(fromName, fromAddress, mailSubject, mailBody); if (result == true) { ModelState.Clear(); ViewBag.Message = "Your message have been sent successfuly!"; } else { ModelState.Clear(); ViewBag.Message = "Your message could not be sent at this time. Please try again later."; } } catch (Exception ex) { ModelState.Clear(); ViewBag.Message = $" Sorry we are having a problem here {ex.Message}"; } } return View(); }

    The MailSystemController that will be called by the rest of the app, and handles simple e-mails:

    using System;
    using System.Net;
    using System.Net.Mail;
    using System.Text;
    using System.Web.Mvc;
    
    
    namespace PrepaidServicesWebsite.Controllers
    {
        public class MailSystemController : Controller
        {
    
            public bool SendEmail(string fromName, string fromAddress, string subject, string message)
            {
    
                try
                {
                    string mailEnableSsl = System.Configuration.ConfigurationManager.AppSettings["smtpEnableSsl"].ToString();
                    string mailServer = System.Configuration.ConfigurationManager.AppSettings["smtpServer"].ToString();
                    string mailServerPort = System.Configuration.ConfigurationManager.AppSettings["smtpPort"].ToString();
                    string mailAccount = System.Configuration.ConfigurationManager.AppSettings["smtpUserName"].ToString();
                    string mailPassword = System.Configuration.ConfigurationManager.AppSettings["smtpPassword"].ToString();
    
    
                    MailMessage eMailMessage = new MailMessage();
                    eMailMessage.From = new MailAddress(fromAddress);
                    eMailMessage.To.Add(new MailAddress(mailAccount, "Jaco Ferreira", System.Text.Encoding.UTF8));
                    eMailMessage.Subject = subject;
                    eMailMessage.Body = message;
                    eMailMessage.IsBodyHtml = true;
                    eMailMessage.BodyEncoding = UTF8Encoding.UTF8;
    
                    try
                    {
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = mailServer;
                        smtp.Port = Int32.Parse(mailServerPort);
                        smtp.EnableSsl = bool.Parse(mailEnableSsl);
                        smtp.Timeout = 100000;
                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential(mailAccount, mailPassword);
    
                        smtp.Send(eMailMessage);
                    }
    
                    finally
                    {
                        eMailMessage.Dispose();
                    }
    
                    return true;
                }
    
                catch (SmtpFailedRecipientsException SFRex)
                {
                    foreach (SmtpFailedRecipientException t in SFRex.InnerExceptions)
                    {
                        var status = t.StatusCode;
    
                        if (status == SmtpStatusCode.MailboxBusy)
                        {
                            Response.Write("Mail delivery failed - mailbox busy.");
                        }
                        else if (status == SmtpStatusCode.MailboxUnavailable)
                        {
                            Response.Write("Mail delivery failed - mailbox unavailable.");
                        }
                        else if (status == SmtpStatusCode.Ok)
                        {
                            Response.Write("Mail delivery succesful.");
                        }
                        else
                        {
                            Response.Write("An unknown error has ocuured. Please contact the administrator.");
                        }
                    }
                    Response.Write(SFRex);
                    return false;
                }
    
                catch (SmtpException Sex)
                {
                    var SmtpErrorMsg = Sex;
                    Response.Write(Sex);
                    return false;
                }
    
    
                catch (Exception ex)
                {
                    ViewBag.GeneralError = ex;
                    return false;
                }
            }
    
        }
    }


    The ContactViewModel:

    using System.ComponentModel.DataAnnotations;
    
    namespace PrepaidServicesWebsite.ViewModels
    {
        public class ContactViewModel
        {
            [Required]
            [StringLength(100, MinimumLength = 2)]
            [Display(Name = "Full Name:")]
            public string Name { get; set; }
            [Required]
            [EmailAddress]
            [Display(Name = "E-mail Address:")]
            public string Email { get; set; }
            [Required]
            [Phone]
            [Display(Name = "Contact Number:")]
            public string TelNo { get; set; }
            [Required]
            [Display(Name = "Subject:")]
            public string Subject { get; set; }
            [Required]
            [Display(Name = "Message:")]
            public string Message { get; set; }
        }
    }

    The Contact Viewl:

    @model PrepaidServicesWebsite.ViewModels.ContactViewModel
    @{
        ViewBag.Title = "Contact";
    }
    
    
    <div class="container">
    
        <h2>Contact</h2>
    
    </div>
    
    
    <div class="container col-md-12">
    
        <div class="container col-md-6">
    
            <div class="panel panel-primary">
                <div class="panel-heading" style="font-size: 14pt">
                    Contact Details
                </div>
                <div class="panel-body">
                    <table style="width: 100%; text-align: left" cellspacing="5" cellpadding="5">
    	                <tr>
    		                <td style="width: 30%">
    			                <b>Contact Person:</b>
    		                </td>
    		                <td style="width: 70%">
    			                Chris Strydom
    		                </td>
    	                </tr>
    	                <tr>
    		                <td style="width: 30%">
    			                <b>Contact Number:</b>
    		                </td>
    		                <td style="width: 70%">
    			                083 445 2000
    		                </td>
    	                </tr>
    	                <tr>
    		                <td style="width: 30%">
    			                <b>VoIP Number:</b>
    		                </td>
    		                <td style="width: 70%">
    			                0100-350-250
    		                </td>
    	                </tr>
    	                <tr>
    		                <td style="width: 30%">
    			                &nbsp;
    		                </td>
    		                <td style="width: 70%">
    			                &nbsp;
    		                </td>
    	                </tr>
    	                <tr>
    		                <td style="width: 30%">
    			                <b>E-Mail:</b>
    		                </td>
    		                <td style="width: 70%">
    			                <a href="mailto:info@prepaid-services.co.za">info@prepaid-services.co.za</a>
    		                </td>
    	                </tr>
    	                <tr>
    		                <td style="width: 30%">
    			                <b>Facebook:</b>
    		                </td>
    		                <td style="width: 70%">
    			                <a href="http://www.facebook.com/chrisstrydom680418" target="_blank">Visit me on Facebook</a>
    		                </td>
    	                </tr>
                    </table>
                </div>
            </div>
    
            <div class="panel panel-primary">
                <div class="panel-heading" style="font-size: 14pt">
                    Contact Form
                </div>
                <div class="panel-body">
                    @using (Html.BeginForm("Contact","Home"))
                    {
                        <div class="form-group">
                            @Html.LabelFor(m => m.Name)
                            @Html.TextBoxFor(m => m.Name, new { @class = "form-control"} )
                            @Html.ValidationMessageFor(m => m.Name)
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(m => m.Email)
                            @Html.TextBoxFor(m => m.Email, new { @class = "form-control"} )
                            @Html.ValidationMessageFor(m => m.Email)
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(m => m.TelNo)
                            @Html.TextBoxFor(m => m.TelNo, new { @class = "form-control"} )
                            @Html.ValidationMessageFor(m => m.TelNo)
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(m => m.Subject)
                            @Html.TextBoxFor(m => m.Subject, new { @class = "form-control"} )
                            @Html.ValidationMessageFor(m => m.Subject)
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(m => m.Message)
                            @Html.TextAreaFor(m => m.Message, new { @class = "form-control", @rows = 6} )
                            @Html.ValidationMessageFor(m => m.Message)
                        </div>
                        <button tyoe="submit" class="btn btn-primary">Submit</button>
                        <p>
                            @if (String.IsNullOrEmpty(ViewBag.Message))
                            {
                                //Do Nothing
                            }
                            else
                            {
                                <br/>
                                <H4>@ViewBag.Message</H4>
                            }
                        </p>
                    }
                </div>
            </div>
    
        </div>
    
        <div class="container col-md-6">
            <div class="panel panel-primary">
            <div class="panel-heading" style="font-size: 14pt">
                Like us on Facebook
            </div>
            <div class="panel-body">
                <table style="width: 100%; text-align: center" cellspacing="5" cellpadding="5">
                    <tr>
                        <td>
                            <div id="fb-root"></div>
                                <script>(function(d, s, id) {
                                    var js, fjs = d.getElementsByTagName(s)[0];
                                    if (d.getElementById(id)) return;
                                    js = d.createElement(s); js.id = id;
                                    js.src = 'https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v3.1';
                                    fjs.parentNode.insertBefore(js, fjs);
                                }(document, 'script', 'facebook-jssdk'));</script>
                            </div>
                            <div class="fb-page" data-href="https://www.facebook.com/PrepaidServices/" data-tabs="timeline, events, messages" data-small-header="false" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="true">
                                <blockquote cite="https://www.facebook.com/PrepaidServices/" class="fb-xfbml-parse-ignore"><a href="https://www.facebook.com/PrepaidServices/">Prepaid-Services</a></blockquote>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    
    </div>
    
    @section Scripts{}
    
    
    


    Tuesday, August 28, 2018 7:53 AM

All replies

  • Hi,

    Use email from as "emailId@outlook.com" OR use SMTP server as "SMTPHOST"


    Thanks,
    Vivek Bansod
    Blog | MSDN | LinkedIn
     

    Tuesday, August 28, 2018 11:03 AM
  • Hi,

    I'm not sure what you mean. I use Web.Config to store my SMTP settings:

    <appSettings>
        <add key="smtpEnableSsl" value="true" />
        <add key="smtpServer" value="smtp-mail.outlook.com" />
        <add key="smtpPort" value="587" />
        <add key="smtpUserName" value="emailaccount@outlook.com" />
        <add key="smtpPassword" value="EmailAccountPassword" />
    </appSettings>

    Then I assign & use these values in my MailSystemController:

                try
                {
                    string mailEnableSsl = System.Configuration.ConfigurationManager.AppSettings["smtpEnableSsl"].ToString();
                    string mailServer = System.Configuration.ConfigurationManager.AppSettings["smtpServer"].ToString();
                    string mailServerPort = System.Configuration.ConfigurationManager.AppSettings["smtpPort"].ToString();
                    string mailAccount = System.Configuration.ConfigurationManager.AppSettings["smtpUserName"].ToString();
                    string mailPassword = System.Configuration.ConfigurationManager.AppSettings["smtpPassword"].ToString();
    
    
                    MailMessage eMailMessage = new MailMessage();
                    eMailMessage.From = new MailAddress(fromAddress);
                    eMailMessage.To.Add(new MailAddress(mailAccount, "Jaco Ferreira", System.Text.Encoding.UTF8));
                    eMailMessage.Subject = subject;
                    eMailMessage.Body = message;
                    eMailMessage.IsBodyHtml = true;
                    eMailMessage.BodyEncoding = UTF8Encoding.UTF8;
    
                    try
                    {
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = mailServer;
                        smtp.Port = Int32.Parse(mailServerPort);
                        smtp.EnableSsl = bool.Parse(mailEnableSsl);
                        smtp.Timeout = 100000;
                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential(mailAccount, mailPassword);
    
                        smtp.Send(eMailMessage);
                    }
    
                    finally
                    {
                        eMailMessage.Dispose();
                    }

    The problem I'm having is this: when I use my personal SMTP settings it works. I also get it to work with my Gmail account settings. But when I try use my Outlook.com settings, I get the above Exception.

    So it looks like Outlook.com denies my application the right to send email via the Outlook.com server with my account.

    Tuesday, August 28, 2018 12:08 PM
  • Hi,

    >>Transaction failed. The server response was: 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;

    The reason for your question could be without closing Clutter. To work around this issue, turn off Clutter for the user.

    Method : Use Outlook on the web

    1.In Outlook on the web, click Settings in the upper-right corner.

    2.Click My app settings, click Mail, and then click Clutter.

    3.Clear the Separate items identified as clutter check box, and then click Save.

    Regards,

    JianGuo



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 30, 2018 8:09 AM
  • hello, 

    I checked my outlook mail config and found this :

    those are the parameters I found on my standard outlook account, maybe there is an upgrade to use o365 servers!!!!

    (there is already threads about the same issue, it is more related about outlook config and it impact all client of smtp-mail.outlook.com , here is some links :
     "5-2-0-storedrv-deliver-exception-objectnotfoundexception-mapiexcep"

    5.2.0 STOREDRV Error

    Hope it help;


    Sunday, September 2, 2018 2:10 PM