【发布时间】:2013-04-03 19:17:54
【问题描述】:
**它只是说“发送邮件失败。” 不确定代码或 SMTP 服务器的问题?请帮助这是我的代码,变量是通过表单发送的,我已经确认所有变量都正常
SMTP 设置在 Web.config** 中
<?xml version="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtp from="newsletter@abc.com">
<network host="mail.abc.com" port="25" userName="newsletter@abc.com" password="abc#!@"/>
</smtp>
</mailSettings>
</system.net>
<system.web>
</system.web>
</configuration>
C# 中的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cmdSend_Click(object sender, EventArgs e)
{
MailMessage mMailMessage = new MailMessage();
// address of sender
mMailMessage.From = new MailAddress(txtFrom.Text);
// recipient address
mMailMessage.To.Add(new MailAddress(txtTo.Text));
// Check if the bcc value is empty
if (txtBcc.Text != string.Empty)
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(txtBcc.Text));
}
// Check if the cc value is empty
if (txtCc.Text != string.Empty)
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(txtCc.Text));
} // Set the subject of the mail message
mMailMessage.Subject = txtSubject.Text;
// Set the body of the mail message
mMailMessage.Body = txtBody.Text;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
try
{
mSmtpClient.Send(mMailMessage);
}
catch (Exception ex)
{
;//log error
lblMessage.Text = ex.Message;
}
finally
{
mMailMessage.Dispose();
}
}
}
【问题讨论】:
-
您的 SMTP 设置在哪里?
-
@Grant,只要设置存在于应用程序或网络配置中,它们就会被拾取。
-
@我已经添加了web.config
-
@MichaelPerrenoud 我收到“发送邮件失败”。留言
-
查看事件查看器,看看您是否收到任何与之相关的系统或应用程序错误。