1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-22 21:51:22 +01:00

smtp fixes

This commit is contained in:
Kyle Spearrin 2017-08-21 22:36:31 -04:00
parent 15e69a529e
commit 1ab815dbb8
2 changed files with 38 additions and 39 deletions

View File

@ -22,46 +22,45 @@ namespace Bit.Core.Services
public Task SendEmailAsync(Models.Mail.MailMessage message) public Task SendEmailAsync(Models.Mail.MailMessage message)
{ {
using(var client = new SmtpClient(_globalSettings.Mail.Smtp.Host, _globalSettings.Mail.Smtp.Port)) var client = new SmtpClient(_globalSettings.Mail.Smtp.Host, _globalSettings.Mail.Smtp.Port);
client.UseDefaultCredentials = false;
client.EnableSsl = _globalSettings.Mail.Smtp.Ssl;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential(_globalSettings.Mail.Smtp.Username,
_globalSettings.Mail.Smtp.Password);
var smtpMessage = new MailMessage();
smtpMessage.From = new MailAddress(_globalSettings.Mail.ReplyToEmail, _globalSettings.SiteName);
smtpMessage.Subject = message.Subject;
smtpMessage.SubjectEncoding = Encoding.UTF8;
smtpMessage.BodyEncoding = Encoding.UTF8;
smtpMessage.BodyTransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
foreach(var address in message.ToEmails)
{ {
client.UseDefaultCredentials = false; smtpMessage.To.Add(new MailAddress(address));
client.EnableSsl = _globalSettings.Mail.Smtp.Ssl;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential(_globalSettings.Mail.Smtp.Username,
_globalSettings.Mail.Smtp.Password);
var smtpMessage = new MailMessage();
smtpMessage.From = new MailAddress(_globalSettings.Mail.ReplyToEmail, _globalSettings.SiteName);
smtpMessage.Subject = message.Subject;
smtpMessage.SubjectEncoding = Encoding.UTF8;
smtpMessage.BodyEncoding = Encoding.UTF8;
smtpMessage.BodyTransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
foreach(var address in message.ToEmails)
{
smtpMessage.To.Add(new MailAddress(address));
}
if(string.IsNullOrWhiteSpace(message.TextContent))
{
smtpMessage.IsBodyHtml = true;
smtpMessage.Body = message.HtmlContent;
}
else
{
smtpMessage.Body = message.TextContent;
var htmlView = AlternateView.CreateAlternateViewFromString(message.HtmlContent);
htmlView.ContentType = new System.Net.Mime.ContentType("text/html");
smtpMessage.AlternateViews.Add(htmlView);
}
client.SendCompleted += (s, e) =>
{
smtpMessage.Dispose();
};
client.SendAsync(smtpMessage, null);
return Task.FromResult(0);
} }
if(string.IsNullOrWhiteSpace(message.TextContent))
{
smtpMessage.IsBodyHtml = true;
smtpMessage.Body = message.HtmlContent;
}
else
{
smtpMessage.Body = message.TextContent;
var htmlView = AlternateView.CreateAlternateViewFromString(message.HtmlContent);
htmlView.ContentType = new System.Net.Mime.ContentType("text/html");
smtpMessage.AlternateViews.Add(htmlView);
}
client.SendCompleted += (s, e) =>
{
smtpMessage.Dispose();
client.Dispose();
};
client.SendAsync(smtpMessage, null);
return Task.FromResult(0);
} }
} }
} }

View File

@ -397,7 +397,7 @@ globalSettings__mail__smtp__host=REPLACE
globalSettings__mail__smtp__username=REPLACE globalSettings__mail__smtp__username=REPLACE
globalSettings__mail__smtp__password=REPLACE globalSettings__mail__smtp__password=REPLACE
globalSettings__mail__smtp__ssl=true globalSettings__mail__smtp__ssl=true
globalSettings__mail__smtp__port=465"); globalSettings__mail__smtp__port=587");
if(!_push) if(!_push)
{ {