C# UserManager SendEmailAsync未发送电子邮件
我正在使用下面的代码尝试异步发送电子邮件,但没有发送电子邮件,我不确定是什么做得不正确。我还在web.config中添加了电子邮件协议的第二段代码 发送电子邮件异步代码C# UserManager SendEmailAsync未发送电子邮件,c#,asp.net-mvc,C#,Asp.net Mvc,我正在使用下面的代码尝试异步发送电子邮件,但没有发送电子邮件,我不确定是什么做得不正确。我还在web.config中添加了电子邮件协议的第二段代码 发送电子邮件异步代码 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 p
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.<br/><br/>" + tmpPass + "<br/><br/>MTSS-B Administrator");
<system.net>
<mailSettings>
<smtp>
<network host="smtp1.airws.org" userName="" password="" />
</smtp>
</mailSettings>
</system.net>
等待UserManager.SendEmailAsync(username.Id,“MTSS-B:忘记密码”,“这是您的新密码。请返回MTSS-B web工具并登录。系统将提示您创建自己的密码。
“+tmpPass+”
MTSS-B管理员”);
Web.config代码
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.<br/><br/>" + tmpPass + "<br/><br/>MTSS-B Administrator");
<system.net>
<mailSettings>
<smtp>
<network host="smtp1.airws.org" userName="" password="" />
</smtp>
</mailSettings>
</system.net>
****更新****
我测试了是否可以使用常规方法发送电子邮件,是否可以使用以下代码发送电子邮件
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.<br/><br/>Password: " + tmpPass + "<br/><br/>MTSS-B Administrator");
m.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp2.airws.org");
smtp.Send(m);
MailMessage m=新邮件(新邮件地址(ConfigurationManager.AppSettings[“SupportEmailAddr]”)、新邮件地址(model.Email));
m、 主题=“MTSS-B:忘记密码”;
m、 Body=string.Format(“这是您的新密码。请返回MTSS-B web工具并登录。系统将提示您创建自己的密码。
密码:“+tmpPass+”
MTSS-B管理员”);
m、 IsBodyHtml=true;
SmtpClient smtp=新的SmtpClient(“smtp2.airws.org”);
smtp.Send(m);
在您的应用程序中,app\u Start
文件夹中可能有一个名为IdentityConfig.cs
的文件。该文件顶部可能有如下内容:
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 mail service]帐户,并使用其名称和密码创建凭据)
那就应该成功了!请记住,gmail可能会让你的生活变得复杂,而试图以这种方式连接到你的新邮件帐户,要解决这个问题,你必须登录该帐户,然后进入并激活“不太安全的应用程序访问”(或者类似的,我说西班牙语,所以我的交易可能不太好…)
2016年4月22日编辑:在代理服务器下工作时,此解决方案似乎无法正常工作,应该有办法对其进行配置。在我的例子中,我发现禁用代理并继续下去更便宜,但对于那些负担不起的人来说,在实现此功能时可能会遇到这种障碍。我认为您正在使用SMTP和电子邮件客户端服务器。那么您的完整配置如下所示:
Web.config
<system.net>
<mailSettings>
<smtp from="xyz@gmail.com">
<network host="smtp.gmail.com" userName="xyz" defaultCredentials="false" password="xyz" port="587" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
最后从控制器中使用它:
public async Task<IHttpActionResult> 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();
}
public异步任务TestSmtpMail()
{
var subject=“您的主题”;
var body=“您的电子邮件正文也可以是html”;
var user=await UserManager.findbyemailsync(“xxx@gmail.com");
等待UserManager.sendmailasync(user.Id、subject、body);
返回Ok();
}
您可能会出现如下错误:
SMTP服务器需要安全连接,或者客户端未连接
认证的
然后,
有关更多详细信息和客户访问太好了。非常感谢。我有错误,因为
ConfigurationManager.AppSettings[“SupportEmailAddr”]
为空。您必须在web.config文件中进行设置。
您有一个名为:
这也是ConfigurationManager.AppSettings所指的内容。
[“SupportEmailAddr”]
正在查看名为SupportEmailAddr的特定设置。
在您的web.config中,它看起来像这样:
<appSettings>
<add key="SupportEmailAddr" value="someone@example.com" />
</appSettings>
您需要在web.config文件中有一个有效的用户名和密码。访问我们的网络主机不需要用户名和密码。这在其他应用程序中也以同样的方式实现。您有端口号吗?如果端口号不是问题,请尝试使用非异步SendEmail函数测试它是否出错。好的,我将尝试非异步SendEmail函数。我可以使用system.net.mail发送电子邮件,因此我不确定为什么不能使用sendemailasync发送电子邮件。我没有机会测试此解决方案,但如果我再次遇到这个问题,我会尽力解决。谢谢完美的谢谢。对我来说是EmailService.csi,ConfigurationManager.AppSettings[“key”]
和ConfigurationManager.AppSettings.Get(“key”)
之间有什么区别吗?你能解释一下你在代理问题上做了什么吗?当我在本地机器上运行gmail解决方案时,它运行良好,但当我将代码发布到Azure时,我得到502-Web服务器在充当网关或代理服务器时收到无效响应。我想知道这是否与你的问题有关。谢谢恐怕不是,我的工作中有一个代理,这是一个制造麻烦的代理,在它启动时我从未收到邮件,直到我禁用它进行测试,但问题都是在发布到azure之前。遗憾的是,我不记得在部署azure时遇到过问题。[链接]那家伙似乎也有同样的问题,显然是因为连接字符串。我建议你从那里开始:/谢谢你回复我!我们将查看链接。
<appSettings>
<add key="SupportEmailAddr" value="someone@example.com" />
</appSettings>