如何配置 SMTP 设置
本文关键字:SMTP 设置 配置 何配置 | 更新日期: 2023-09-27 18:37:10
我想在 asp.net 中使用密码恢复控件恢复密码,但是当我单击提交按钮时。我收到这条消息:
命令顺序错误。服务器响应为:此邮件服务器在尝试发送到非本地电子邮件地址时需要身份验证。请检查您的邮件客户端设置或与管理员联系以验证是否为此服务器定义了域或地址.这是我的 HTML 代码:
<asp:PasswordRecovery ID="PasswordRecovery1" runat="server"
style="font-family: Tahoma; font-size: small"
onsendingmail="PasswordRecovery1_SendingMail" >
<MailDefinition BodyFileName="../User Pages/RecoveryPass.htm" From="info@shirinfam.com"
IsBodyHtml="True" Subject="بازیابی کلمه ی عبور" Priority="High"></MailDefinition>
<UserNameTemplate>
<table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
<tr>
<td>
<table cellpadding="0">
<tr>
<td align="center" colspan="2">
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Label ID="Label1" runat="server"
Text="<%$ Resources:resource, passrecovery %>"
style="font-size: small; font-family: Tahoma"></asp:Label></td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"></asp:Label>
</td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
ControlToValidate="UserName" ErrorMessage="User Name is required."
ToolTip="User Name is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color:Red;">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="SubmitButton" runat="server" CommandName="Submit" Text="<%$ Resources:resource, submit %>"
ValidationGroup="PasswordRecovery1" Font-Names="tahoma"
Font-Size="X-Small" Width="80px" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</UserNameTemplate>
</asp:PasswordRecovery>
这是背后的代码:
protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
SmtpClient smtp = new SmtpClient();
smtp.Send(e.Message);
}
这是 web.config 中的 SMTP 设置:
<system.net>
<mailSettings >
<smtp >
<network host="mail.domainname.com" userName="info@dominname.com" password ="password" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
为您的电子邮件帐户设置credentials
,假设您正在使用您的Gmail
帐户发送电子邮件,因此,您必须将credentials (username and password)
设置为您的smtpClient object
。
下面是示例:
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
MailMessage帮助我们创建邮件,SmtpClient允许我们将邮件发送给收件人。
注意:以下代码配置为从GMAIL帐户发送邮件。
m.From = new MailAddress("from@gmail.com", "Display name");
m.To.Add(new MailAddress("to@domain.com", "Display name To"));
m.Subject = "Test";
m.Body = "This is a Test Mail";
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new System.Net.NetworkCredential("from@gmail.com", "password of from");
sc.EnableSsl = true; // runtime encrypt the SMTP communications using SSL
sc.Send(m);
DefaultCredentials 应设置为 true,并应将凭据显式添加到客户端。
SmtpClient client = new SmtpClient(server, port);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;