UserManager SendEmailAsync没有发送电子邮件

我使用以下代码尝试异步发送电子邮件,但没有发送电子邮件,我不确定是什么做错了。 我还在web.config中为电子邮件协议添加了第二段代码。

SendEmailAsync代码

await UserManager.SendEmailAsync(username.Id, "MTSS-B: Forgot Password", "Here is your new password. Please go back to the MTSS-B web tool and sign in. You will be prompted to create your own password.

" + tmpPass + "

MTSS-B Administrator");

Web.config代码

        

**** ****更新

我测试了是否可以使用常规方法发送电子邮件,并且可以使用以下代码发送电子邮件。

 MailMessage m = new MailMessage(new MailAddress(ConfigurationManager.AppSettings["SupportEmailAddr"]), new MailAddress(model.Email)); m.Subject = "MTSS-B: Forgot Password"; m.Body = string.Format("Here is your new password. Please go back to the MTSS-B web tool and sign in. You will be prompted to create your own password.

Password: " + tmpPass + "

MTSS-B Administrator"); m.IsBodyHtml = true; SmtpClient smtp = new SmtpClient("smtp2.airws.org"); smtp.Send(m);

在您的应用程序中,您可能在App_Start文件夹中有一个名为IdentityConfig.cs的文件。 该文件可能具有以下类似的function:

 public class EmailService : IIdentityMessageService { public Task SendAsync(IdentityMessage message) { // Plug in your email service here to send an email. return Task.FromResult(0); } } 

将其更改为:

 public class EmailService : IIdentityMessageService { public Task SendAsync(IdentityMessage message) { SmtpClient client = new SmtpClient(); return client.SendMailAsync(ConfigurationManager.AppSettings["SupportEmailAddr"], message.Destination, message.Subject, message.Body); } } 

根据自己的喜好自定义发送代码。

乔的解决方案引导了我很多,谢谢你! 但要为此做出贡献,您必须包含以下命名空间:

 using System.Configuration; using System.Net.Mail; 

我做了很多尝试后,我已经改变了他的解决方案,我已经达到了一些有效的代码(它仍然必须被重新修改,但它不会改变这个想法),这就是我的SendAsync方法的样子:

 public Task SendAsync(IdentityMessage message) { //SmtpClient client = new SmtpClient(); //return client.SendMailAsync(ConfigurationManager.AppSettings["SupportEmailAddr"], // message.Destination, // message.Subject, // message.Body); SmtpClient client = new SmtpClient(); client.Port = 587; client.Host = "smtp.gmail.com"; client.EnableSsl = true; //client.Timeout = 10000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("mailName@gmail.com", "mailPassword"); return client.SendMailAsync("mailName@gmail.com", message.Destination, message.Subject, message.Body); } 

您可以看到Joe的解决方案在顶部进行了评论,然后,对SmtpClient进行了大量配置(由于我正在进行的一些测试,超时被评论,如果它符合您的需要,您可以取消注释)。

之后,邮件以异步方式发送,请注意发件人(Joe从AppSettings变量获取)与凭证中指定的相同(您必须创建一个gmail [或您想要的wathever邮件服务]帐户并使用它用于创建凭据的名称和密码)。

这应该够了吧! 请记住,在尝试以这种方式连接到您的新邮件帐户时,gmail可能会使您的生活变得复杂,要解决此问题,您必须登录该帐户并转到您的帐户配置并激活“安全性较低的应用程序访问”(或者类似于,我说西class牙语,所以我的牵引可能不那么好……)。

编辑22/04/16:似乎这个解决方案在处理代理服务器时无法正常工作,应该有一种方法来配置它。 在我的情况下,我发现禁用代理并继续下去会更便宜,但对于那些负担不起的人来说,期望在实现这一点时遇到这个障碍。

我认为您正在使用Macrosoft ASP.NET身份和SMTP电子邮件客户端服务器。 然后你的完整配置如下:

Web.config文件

        

创建一个SmtpEmailService.cs类

 public class SmtpEmailService : IIdentityMessageService { readonly ConcurrentQueue _clients = new ConcurrentQueue(); public async Task SendAsync(IdentityMessage message) { var client = GetOrCreateSmtpClient(); try { MailMessage mailMessage = new MailMessage(); mailMessage.To.Add(new MailAddress(message.Destination)); mailMessage.Subject = message.Subject; mailMessage.Body = message.Body; mailMessage.BodyEncoding = Encoding.UTF8; mailMessage.SubjectEncoding = Encoding.UTF8; mailMessage.IsBodyHtml = true; // there can only ever be one-1 concurrent call to SendMailAsync await client.SendMailAsync(mailMessage); } finally { _clients.Enqueue(client); } } private SmtpClient GetOrCreateSmtpClient() { SmtpClient client = null; if (_clients.TryDequeue(out client)) { return client; } client = new SmtpClient(); return client; } } 

IdentityConfig.cs

 // Configure the application user manager used in this application. //UserManager is defined in ASP.NET Identity and is used by the application. public class ApplicationUserManager : UserManager { public ApplicationUserManager(IUserStore store, IIdentityMessageService emailService) : base(store) { this.EmailService = emailService; } public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context) { var manager = new ApplicationUserManager(new UserStore(context.Get()), new SmtpEmailService()); . . . . return manager; } } 

如果您正在使用dependency injection(DI),那么配置它。 我正在使用UnityContainer( UnityConfig.cs ),所以我的配置是:

 container.RegisterType(); 

最后从您的控制器使用它:

 public async Task TestSmtpMail() { var subject = "Your subject"; var body = "Your email body it can be html also"; var user = await UserManager.FindByEmailAsync("xxx@gmail.com"); await UserManager.SendEmailAsync(user.Id, subject, body); return Ok(); } 

你可以得到一个类似的错误:

SMTP服务器需要安全连接或客户端未经过身份validation。

然后允许更少的安全应用和允许gmail帐户使其他应用可以访问

有关详细信息和SendGrid客户端,请访问此处

大。 谢谢。 我有错误,因为ConfigurationManager.AppSettings["SupportEmailAddr"]为null。 您必须在web.config文件中设置它。 您有一个名为: . 这也是ConfigurationManager.AppSettings所指的内容。 ["SupportEmailAddr"]正在查看名为SupportEmailAddr的特定设置。 在你的web.config中它看起来像这样: