我正在使用本教程向用户注册确认电子邮件,但我找不到emailservice
课程。
我正在使用带有Identity 2.2的MVC 5.
答案 0 :(得分:0)
据我所知,没有这样的.net类EmailService
通常你使用SmtpClient
类来发送电子邮件:
SmtpClient client = new SmtpClient("server.address.com");
MailAddress from = new MailAddress(fromAddress, fromName);
MailMessage msg = new MailMessage();
msg.From = from;
foreach(string addr in to)
msg.To.Add(addr);
msg.Body = content;
msg.Subject = subject;
client.Send(msg);
答案 1 :(得分:0)
谢谢,但我已经找到了解决方案
我已创建了类电子邮件服务及其
选择来自usermanager的电子邮件,如此
userManager.EmailService = new EmailService();
答案 2 :(得分:0)
我有同样的问题,并使用以下解决方案。首先,在项目中创建一个名为services的新文件夹,并使用教程中的以下代码创建一个新类:
namespace YourProject.Services
{
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
await configSendGridasync(message);
}
// Use NuGet to install SendGrid (Basic C# client lib)
private async Task configSendGridasync(IdentityMessage message)
{
var myMessage = new SendGridMessage();
myMessage.AddTo(message.Destination);
myMessage.From = new System.Net.Mail.MailAddress("no-reply@your-domain.com", "Customer Service");
myMessage.Subject = message.Subject;
myMessage.Text = message.Body;
myMessage.Html = message.Body;
var credentials = new NetworkCredential(
ConfigurationManager.AppSettings["SendGridUsername"],
ConfigurationManager.AppSettings["SendGridPassword"]
);
// Create a Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email.
if (transportWeb != null)
{
await transportWeb.DeliverAsync(myMessage);
}
else
{
Trace.TraceError("Failed to create Web transport.");
await Task.FromResult(0);
}
}
}
}
然后,我所做的是将控制器中UserManager对象的EmailService属性设置为EmailService类的新实例(向右滚动以查看它,对不起):
namespace YourProject.Controllers
{
public class UserManagementController : Controller
{
private UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())){EmailService = new Services.EmailService()};
// GET: UserManagement
然后,当你打电话
UserManager.SendEmailAsync(user.Id, subject, body);
它将使用您在上面的EmailService类中配置的SendGrid accound发送电子邮件。