Skip to content

Commit

Permalink
使用 MailKit 来代替 System.Net.Mail.SmtpClient 发送邮件(快多了)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jijie Chen committed Nov 17, 2018
1 parent 51596e7 commit c3e4747
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/Discussion.Web/Discussion.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<DnClubExcludes>publish\**;logs\**;dotnetclub.db;uploaded\**;</DnClubExcludes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MailKit" Version="2.0.7" />
<PackageReference Include="Markdig" Version="0.15.2" />
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.3" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
using Microsoft.Extensions.Options;
using MimeKit;
using MimeKit.Text;

namespace Discussion.Web.Services.EmailConfirmation.Impl
{
Expand All @@ -15,34 +16,25 @@ public SmtpEmailSender(IOptions<EmailSendingOptions> emailSendingOptions)

public async Task SendEmailAsync(string emailTo, string subject, string message)
{
var smtpClient = new SmtpClient
{
DeliveryMethod = SmtpDeliveryMethod.Network,
Host = _emailSendingOptions.ServerHost,
Port = _emailSendingOptions.ServerSslPort,
EnableSsl = true
};
var mimeMessage = new MimeMessage ();
mimeMessage.From.Add (new MailboxAddress ("dotnet club", _emailSendingOptions.MailFrom));
mimeMessage.To.Add (new MailboxAddress (emailTo));
mimeMessage.Subject = subject;
mimeMessage.Body = new TextPart (TextFormat.Html) { Text = message };

if (!string.IsNullOrEmpty(_emailSendingOptions.LoginName)
&& !string.IsNullOrEmpty(_emailSendingOptions.Password))
{
smtpClient.Credentials = new System.Net.NetworkCredential(_emailSendingOptions.LoginName, _emailSendingOptions.Password);
}

var fromAddress = string.IsNullOrEmpty(_emailSendingOptions.MailFrom)
? _emailSendingOptions.LoginName
: _emailSendingOptions.MailFrom;
var mailMessage = new MailMessage(fromAddress, emailTo)
using (var client = new SmtpClient())
{
Subject = subject,
Body = message,
BodyEncoding = Encoding.UTF8,
IsBodyHtml = true,
Priority = MailPriority.Normal
};
await Task.Run(() => {
smtpClient.Send(mailMessage);
});
client.Connect(_emailSendingOptions.ServerHost, _emailSendingOptions.ServerSslPort, useSsl: true);
if (!string.IsNullOrEmpty(_emailSendingOptions.LoginName)
&& !string.IsNullOrEmpty(_emailSendingOptions.Password))
{
client.Authenticate(_emailSendingOptions.LoginName, _emailSendingOptions.Password);
}

await client.SendAsync(mimeMessage);
client.Disconnect(true);
}
}
}
}
3 changes: 2 additions & 1 deletion src/Discussion.Web/Views/User/Settings.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<div class="inner-square">
<img src="@AvatarService.GetUserAvatarUrl(Model)" title="点击更改头像" class="user-avatar"/>
</div>
<span>点击更改头像,请使用 240x240 方形图片</span>
<span class="avatar-note">点击更改头像,请使用 240x240 方形图片</span>
<canvas class="avatar-preview" width="240" height="240"></canvas>
</div>
<div>
Expand Down Expand Up @@ -230,6 +230,7 @@
if (data.hasSucceeded) {
$('.user-avatar')[0].src = data.result.publicUrl;
$('#@(nameof(Model.AvatarFileId))').val(data.result.fileId);
$('.avatar-note').text('请点击 保存 确认使用新头像');
}
},
error: function() {
Expand Down

0 comments on commit c3e4747

Please sign in to comment.