我在使用“system.net.mail”发送测试电子邮件时遇到问题。每次按下提交按钮时,都会出现错误消息“邮箱名称不允许。服务器响应为:5.3.0 Relaying denied check mail first”。先感谢您。
代码隐藏:
public class MailHelper
{
public static void SendMailMessage(string from, string to, string bcc,
string cc, string subject, string body)
{
MailMessage mMailMessage = new MailMessage();
mMailMessage.From = new MailAddress(from);
mMailMessage.To.Add(new MailAddress(to));
if ((bcc != null) && (bcc != string.Empty))
{
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
if ((cc != null) && (cc != string.Empty))
{
mMailMessage.CC.Add(new MailAddress(cc));
}
mMailMessage.Subject = subject;
mMailMessage.Body = body;
mMailMessage.IsBodyHtml = true;
mMailMessage.Priority = MailPriority.Normal;
string smtpServer = "";
//Read the Web.config to get configuration setting
smtpServer=System.Web.Configuration.WebConfigurationManager.AppSettings["smtp"];
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient(smtpServer);
// Send the mail message
mSmtpClient.Send(mMailMessage);
}
}
网络配置:
<appSettings>
<add key="smtp" value="smtp.mydomain.com"/>
</appSettings>
<system.net>
<mailSettings>
<smtp from="defaultEmail@mydomain.com">
<network host="smtp.mydomain.com" port="25" userName="yourUserName" password="yourPassword"/>
</smtp>
</mailSettings>
</system.net>
Sumbit_Click:
protected void Sumbit_Click(object sender, EventArgs e)
{
MailHelper.SendMailMessage("from@email.com", "to@email.com", null, null,
"subject", "textEnquiry.Text");
}