Reputation: 293
Trying to send an email via MVC 5 C#. This newly created email address is on an office 365 server. Tried numerous solutions online but to no avail I get the following error message: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [LO3P265CA0018.GBRP265.PROD.OUTLOOK.COM]'. My code is as follows:
public void ConcernConfirmEmail(Appointments a, EmailConfig ec)
{
Dictionary<string, string> tokens = new Dictionary<string, string>();
tokens.Add("Name", a.sirenDetail.FirstName);
tokens.Add("Time", a.start.ToString("HH:mm"));
tokens.Add("Date", a.start.ToString("dd/MM/yyyy"));
tokens.Add("Location", a.site.SiteDescription);
using (SmtpClient client = new SmtpClient()
{
Host = "smtp.office365.com",
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(ec.EmailUser, ec.EmailPassword),
TargetName = "STARTTLS/smtp.office365.com",
EnableSsl = true
})
{
MailMessage message = new MailMessage()
{
From = new MailAddress("[email protected]"),
Subject = "Subject",
Sender = new MailAddress("[email protected]", "password"),
Body = PopulateTemplate(tokens, GetTemplate("ConfirmTemplate.html")),
IsBodyHtml = true,
BodyEncoding = System.Text.Encoding.UTF8,
SubjectEncoding = System.Text.Encoding.UTF8,
};
message.To.Add(a.sirenDetail.EmailAddress.ToString());
client.Send(message);
}
}
Upvotes: 1
Views: 2599
Reputation: 1030
According to oficcial microsoft documentation, SmtpClass is obsolete for a while now, microsoft encourages deveopers to use new open souce Smtp implementations, like MailKit.
When using MailKit with username and password authentication, you have to set the authentication mechanism to use NTLM.
Here is a working example:
public async Task Send(string emailTo, string subject, MimeMessage mimeMessage, MessagePriority messagePriority = MessagePriority.Urgent)
{
MimeMessage mailMessage = mimeMessage;
mailMessage.Subject = subject;
mailMessage.Priority = messagePriority;
if (emailTo.Contains(';'))
{
foreach (var address in emailTo.Split(';'))
{
mailMessage.To.Add(new MailboxAddress("", address));
}
}
else
{
mailMessage.To.Add(new MailboxAddress("", emailTo));
}
mailMessage.From.Add(new MailboxAddress("Sender", _smtpCredentials.SenderAddress));
using var smtpClient = new SmtpClient
{
SslProtocols = SslProtocols.Tls,
CheckCertificateRevocation = false,
ServerCertificateValidationCallback = (s, c, h, e) => true,
};
await smtpClient.ConnectAsync(_smtpCredentials.Server, _smtpCredentials.Port, SecureSocketOptions.StartTlsWhenAvailable);
await smtpClient.AuthenticateAsync(new SaslMechanismNtlm(new NetworkCredential(_smtpCredentials.User, _smtpCredentials.Password)));
try
{
await smtpClient.SendAsync(mailMessage);
}
catch (Exception ex)
{
}
finally
{
if (smtpClient.IsConnected) await smtpClient.DisconnectAsync(true);
}
}
The most important line here is
await smtpClient.AuthenticateAsync(new SaslMechanismNtlm(new NetworkCredential(_smtpCredentials.User, _smtpCredentials.Password)));
That is setting the NTLM as the authentication mechanism for the client to use.
But
If you are unable to change Smtp library right now, you can try change your code to look like this, as imcurrent using in older services and work fine:
using (var smtpClient = new SmtpClient(smtpServer)
{
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(user, password),
EnableSsl = false,
Port = 587
})
See that the change is on just on the EnableSsl set to false, and not needded to set the TargetName property.
Hope this helps
Upvotes: 1