我正在开发一个Asp.net Web应用程序。在我的应用程序中,我正在设置用户电子邮件确认和密码重置功能。我正在使用内置身份系统的Asp.net。根据Visual Studio中提到的链接 - https://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity,可以启用这些功能。
但要遵循它,此链接已被破坏 - https://azure.microsoft.com/en-us/gallery/store/sendgrid/sendgrid-azure/。但没关系,我只想知道asp.net身份系统中的一件事。那是发送电子邮件。根据Visual Studio中的注释行,我可以发送如下所示的重置密码电子邮件。
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
该行简单易读。但问题是我在哪里可以配置发件人电子邮件凭据?它用于发送电子邮件的设置是什么?如何更改发件人电子邮件?我也无法关注该链接,因为Azure链接已损坏。我在哪里可以设置和更改这些设置?
我尝试在web.config中添加此设置
<system.net>
<mailSettings>
<smtp from="testing@gmai.com">
<network host="smtp.gmail.com" password="testing" port="587" userName="testing" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
但现在发送电子邮件。
答案 0 :(得分:4)
最后我找到了解决方案。
我在web.config中添加了电子邮件设置,如此
<system.net>
<mailSettings>
<smtp from="testing@gmai.com">
<network host="smtp.gmail.com" password="testing" port="587" userName="testing" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
然后我更新了
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
return Task.FromResult(0);
}
}
在App_Start文件夹中的IdentityConfig.cs中到此
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
SmtpClient client = new SmtpClient();
return client.SendMailAsync("email from web.config here",
message.Destination,
message.Subject,
message.Body);
}
}
当我发送电子邮件时,它会自动使用web.config中的设置。
答案 1 :(得分:0)
APP_Start / IdentityConfig.cs
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function GetDeviceCaps Lib "Gdi32" _
(ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" _
(ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Declare Function SetWindowPos Lib "user32" ( _
ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const LOGPIXELSX = 88
Private Const LOGPIXELSY = 90
Private Type POINTAPI
x As Long
y As Long
End Type
Private Const HWND_TOP = 0
Private Const SWP_NOSIZE = &H1
Private Sub CommandButton1_Click()
RepositionForm UserForm2, CommandButton1
End Sub
Public Sub RepositionForm(f As Object, c As Object)
Dim P As POINTAPI
Dim meHwnd As Long, hwnd As Long
meHwnd = FindWindow(vbNullString, Me.Caption)
P.x = (c.Left - (f.Width / 4)) / PointsPerPixelX
P.y = (c.Top + c.Height) / PointsPerPixelY
'~~> The ClientToScreen function converts the client coordinates
'~~> of a specified point to screen coordinates.
ClientToScreen meHwnd, P
UserForm2.Show vbModeless
'~~> Get Handle of Userform2
hwnd = FindWindow("ThunderDFrame", "UserForm2")
'~~> Move the form to relevant location
SetWindowPos hwnd, HWND_TOP, P.x, P.y, 0, 0, SWP_NOSIZE
End Sub
Private Function PointsPerPixelX() As Double
Dim hDC As Long
hDC = GetDC(0)
PointsPerPixelX = 72 / GetDeviceCaps(hDC, LOGPIXELSX)
ReleaseDC 0, hDC
End Function
Public Function PointsPerPixelY() As Double
Dim hDC As Long
hDC = GetDC(0)
PointsPerPixelY = 72 / GetDeviceCaps(hDC, LOGPIXELSY)
ReleaseDC 0, hDC
End Function
web.config
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
//SmtpClient client = new SmtpClient();
//return client.SendMailAsync("email from web.config here",
// message.Destination,
// message.Subject,
// message.Body);
var smtp = new SmtpClient();
var mail = new MailMessage();
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string username = smtpSection.Network.UserName;
mail.IsBodyHtml = true;
mail.From = new MailAddress(username);
mail.To.Add(message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
smtp.Timeout = 1000;
var t = Task.Run(() => smtp.SendAsync(mail, null));
return t;
}
}