gpt4 book ai didi

c# - 如何在 ASP.Net IIdentityMessageService 中使用 SMTP 或 Office 365 帐户?

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:18 25 4
gpt4 key购买 nike

我曾经使用 web.config 配置我的 SMTP 帐户,以便能够使用发送电子邮件功能,但 ASP.Net Identity 要求电子邮件服务。如何在以下区域使用 SMTP 或 Office 365 电子邮件:

注册.aspx.cs

IdentityResult result = manager.Create(user, Password.Text );

if (result.Succeeded)
{
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
string code = manager.GenerateEmailConfirmationToken(user.Id);
string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");

signInManager.SignIn( user, isPersistent: false, rememberBrowser: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}

IdentityConfig.cs

public Task SendAsync(IdentityMessage message)
{


// Credentials:
var credentialUserName = "test@test.com";
var sentFrom = "test@test.com";
var pwd = "testpassword";

// Configure the client:
System.Net.Mail.SmtpClient client =
new System.Net.Mail.SmtpClient("smtp-mail.outlook.com");

client.Port = 587;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;

// Create the credentials:
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(credentialUserName, pwd);

client.EnableSsl = true;
client.Credentials = credentials;

// Create the message:
var mail =
new System.Net.Mail.MailMessage(sentFrom, message.Destination);

mail.Subject = message.Subject;
mail.Body = message.Body;

// Send:
return client.SendMailAsync(mail);


// Plug in your email service here to send an email.
//return Task.FromResult(0);
}

Web.Config

 <system.net>
<mailSettings>
<smtp>
<network defaultCredentials="true" enableSsl="true" host="smtp.office365.com" port="587" password="password" userName="username" />
</smtp>
</mailSettings>
</system.net>

感谢您为解决我的问题所做的努力。

最佳答案

EmailService.SendTaskAsync 中,有一条注释告诉您在何处插入您的电子邮件代码。这就是您放置 SMTP 代码、调用 Office 365 API 或类似 SendGrid 的 Web 服务的位置。 .这是使用 SMTP 的示例。

public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
//create a client, it should pick up the settings from web.config
var client = new SmtpClient();

//now construct a MailMessage object from the message
var email = new MailMessage("donotreply@mysite.com", message.Destination, message.Subject, message.Body);

//send the email asynchronously
await client.SendMailAsync(email);

return Task.FromResult(0);
}
}

显然,您可能希望将您的实际电子邮件代码移至另一个类,以便您可以将其重新用于其他与电子邮件相关的任务。

关于c# - 如何在 ASP.Net IIdentityMessageService 中使用 SMTP 或 Office 365 帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31678221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com