**它只是說「發送郵件失敗。」 不確定它的代碼或SMTP服務器的問題?請幫助這裏是我的代碼,變量通過形式發送,我已經確認了所有的變量都OK在C#中使用SMTP發送電子郵件#
SMTP設置是在Web.config中**
<?xml version="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="mail.abc.com" port="25" userName="[email protected]" password="abc#[email protected]"/>
</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設置? – Sachin 2013-04-03 19:19:39
你實例化了'SmtpClient'然後...什麼都沒有。你只是試圖用它來發送。 – 2013-04-03 19:20:04
@Grant,只要設置存在於應用程序或Web配置中,它們將被拾取。 – 2013-04-03 19:20:44