C#中关于邮件的东西都放在了命名空间System.Net.Mail中,
其中MailMessage表示一封邮件,其地址MailAddress,然后邮件的发送由
SmtpClient来处理。你看C#还是很简单的。
其代码
//strFrom发件人的邮箱
//strTo收件人的邮箱
//strSubject邮箱主题
//strContent邮箱的文本内容
//strPwd邮箱密码
//strServerMail 为smtp服务器如Smtp.163.com
public string SendMail(string strFrom, string strTo, string strSubject, string strContent
, string strPwd, string strServerMail)
{
if (strFrom == "" || strTo == "" || strSubject == "" || strContent == "" || strPwd == "" || strServerMail == "")
return "各参数不应有空值";
//关于邮件的设置
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(strFrom);
mailMsg.To.Add(new MailAddress(strTo));
mailMsg.Subject = strSubject;
mailMsg.Body = strContent;
mailMsg.IsBodyHtml = false;
try
{
//用于发送的设置
SmtpClient mailClient = new SmtpClient();
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Host = strServerMail;
//安全签名,用于验证的信息
mailClient.Credentials = new NetworkCredential(strFrom, strPwd);
mailClient.Send(mailMsg);
}
catch(SmtpException eError)
{
return eError.Message;
}
return "";
}