Error sending emails via Outlook.com with custom app

  • Thread starter Thread starter Jaco Ferreira
  • Start date Start date
J

Jaco Ferreira

Guest
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="Chris Strydom" 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{}

Continue reading...
 
Back
Top