Send Email Error
private void btnSend_Click(object sender, EventArgs e)
{
IPAddress[] ipAddress = Dns.GetHostEntry(txtServerName.Text).AddressList;
string ipadd = "";
Ping ping= new Ping();
PingReply pingReply =null;
foreach (IPAddress ip in ipAddress)
{
pingReply = ping.Send(ip, Convert.ToInt32(txtPort.Text));
if (pingReply.Status ==IPStatus.Success)
{
ipadd = ip.ToString();
break;
}
}
SmtpClient sc = new SmtpClient(ipadd.ToString(), Convert.ToInt32(txtPort.Text));
sc.Credentials = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
//sc.Host = ipadd.ToString();
//sc.Port = Convert.ToInt32(txtPort.Text);
MailAddress fromMail = new MailAddress(txtFrom.Text, txtDisplay.Text);
MailAddress toMail = new MailAddress(txtTo.Text);
MailMessage ms = new MailMessage();
ms.Body = txtContent.Text;
ms.Subject = txtTitle.Text;
ms.ReplyTo = fromMail;
ms.From = fromMail;
ms.Sender = fromMail;
ms.To.Add(toMail);
sc.SendCompleted += new SendCompletedEventHandler(SendCompleted);
sc.Send(ms);//错误发生在这里 错误信息:SmtpException was unhandled
}
private void SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{ MessageBox.Show("发送已取消!"); }
if (e.Error != null)
{
MessageBox.Show(e.UserState.ToString() + "发送错误:" + e.Error.Message, "发送错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("邮件成功发出!", "恭喜!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}