使用smtpClient发送邮件时,经常跑出难以明白的错误。其实主要都和我们申请的免费邮箱有关系。有些邮箱不支持,比如163的以前申请的帐号可以用,新注册的帐号是发不了的。
不同的邮箱,要改的部分主要是“上面代码中高亮的部分”,有些邮箱地址是要加后缀“@xxx.com”,而有些邮箱只要用户名就好了 .
附带以前2003版的:
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="Sender">发件人邮箱地址</param>
/// <param name="Receiver">收件人邮箱地址</param>
/// <param name="Title">邮件标题</param>
/// <param name="Content">邮件内容</param>
/// <param name="SmtpServer">邮箱服务器名称</param>
/// <param name="Validate">服务器是否需要验证</param>
/// <param name="UserName">验证用户名</param>
/// <param name="Password">验证密码</param>
/// <returns>结果</returns>
public string SendMail(string from,string to,string title,string body,string enclosure,
string smtpServer,bool validate,string userName,string password)
{
MailMessage mail = new MailMessage();//实例化MailMessage
mail.From = from; //邮件发件人
mail.To = to;//邮件收件人
mail.Subject = title;//邮件标题
mail.Body = body; //邮件内容
mail.BodyFormat = MailFormat.Html; //邮件文本格式
mail.Priority = MailPriority.Normal;//邮件发送级别
if(enclosure != "")
{
//添加附件
MailAttachment ma = new MailAttachment(enclosure);
mail.Attachments.Add(ma);
}
//邮件服务器是否需要验证
if(validate)
{
if(userName != "" && password != "")
{
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
}
else
{
return "缺少验证用户名和密码";
}
}
try
{
SmtpMail.SmtpServer = smtpServer;//设置邮件服务器
//发送邮件
SmtpMail.Send(mail);
return "邮件发送成功!";
}
catch
{
return "邮件发送失败!";
}
}
/// <summary>
/// 功能:发送邮件,返回字符串:“发送成功”否则返回错误代码。
/// 创建:2007-08-23
/// </summary>
/// <param name="MailTo">MailTo为收信人地址</param>
/// <param name="Subject">Subject为标题</param>
/// <param name="Body">Body为信件内容</param>
/// <param name="BodyFormat">BodyFormat为信件内容格式:0为Text,1为Html</param>
/// <param name="Priority">Priority为优先级:0为低,1为中,2为高</param>
/// <param name="Attachments">Attachment为附件,为null则不发送</param>
public static string SendMail(System.Collections.ArrayList MailTo, string Subject, string Body, int BodyFormat, int Priority, string Attachments)
{
string result;
SmtpClient mail = new SmtpClient();
//发送方式
mail.DeliveryMethod = SmtpDeliveryMethod.Network;
//smtp服务器
mail.Host = "smtp.sina.com";
//用户名凭证
mail.Credentials = new System.Net.NetworkCredential("mail_user_name", "mail_user_password");
//邮件信息
MailMessage message = new MailMessage();
//发件人
message.From = new MailAddress("mail_user_name@sina.com");
//收件人
foreach (object item in MailTo)
{
message.To.Add(item.ToString());
}
//主题
message.Subject = Subject;
//内容
message.Body = Body;
//正文编码
message.BodyEncoding = System.Text.Encoding.UTF8;
//设置为HTML格式
message.IsBodyHtml = true;
//优先级
message.Priority = MailPriority.High;
try
{
mail.Send(message);
result = "发送成功";
return result;
}
catch (Exception e)
{
result = e.ToString();
}
return result;
}
/// 功能:发送邮件,返回字符串:“发送成功”否则返回错误代码。
/// 创建:2007-08-23
/// </summary>
/// <param name="MailTo">MailTo为收信人地址</param>
/// <param name="Subject">Subject为标题</param>
/// <param name="Body">Body为信件内容</param>
/// <param name="BodyFormat">BodyFormat为信件内容格式:0为Text,1为Html</param>
/// <param name="Priority">Priority为优先级:0为低,1为中,2为高</param>
/// <param name="Attachments">Attachment为附件,为null则不发送</param>
public static string SendMail(System.Collections.ArrayList MailTo, string Subject, string Body, int BodyFormat, int Priority, string Attachments)
{
string result;
SmtpClient mail = new SmtpClient();
//发送方式
mail.DeliveryMethod = SmtpDeliveryMethod.Network;
//smtp服务器
mail.Host = "smtp.sina.com";
//用户名凭证
mail.Credentials = new System.Net.NetworkCredential("mail_user_name", "mail_user_password");
//邮件信息
MailMessage message = new MailMessage();
//发件人
message.From = new MailAddress("mail_user_name@sina.com");
//收件人
foreach (object item in MailTo)
{
message.To.Add(item.ToString());
}
//主题
message.Subject = Subject;
//内容
message.Body = Body;
//正文编码
message.BodyEncoding = System.Text.Encoding.UTF8;
//设置为HTML格式
message.IsBodyHtml = true;
//优先级
message.Priority = MailPriority.High;
try
{
mail.Send(message);
result = "发送成功";
return result;
}
catch (Exception e)
{
result = e.ToString();
}
return result;
}
不同的邮箱,要改的部分主要是“上面代码中高亮的部分”,有些邮箱地址是要加后缀“@xxx.com”,而有些邮箱只要用户名就好了 .
附带以前2003版的:
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="Sender">发件人邮箱地址</param>
/// <param name="Receiver">收件人邮箱地址</param>
/// <param name="Title">邮件标题</param>
/// <param name="Content">邮件内容</param>
/// <param name="SmtpServer">邮箱服务器名称</param>
/// <param name="Validate">服务器是否需要验证</param>
/// <param name="UserName">验证用户名</param>
/// <param name="Password">验证密码</param>
/// <returns>结果</returns>
public string SendMail(string from,string to,string title,string body,string enclosure,
string smtpServer,bool validate,string userName,string password)
{
MailMessage mail = new MailMessage();//实例化MailMessage
mail.From = from; //邮件发件人
mail.To = to;//邮件收件人
mail.Subject = title;//邮件标题
mail.Body = body; //邮件内容
mail.BodyFormat = MailFormat.Html; //邮件文本格式
mail.Priority = MailPriority.Normal;//邮件发送级别
if(enclosure != "")
{
//添加附件
MailAttachment ma = new MailAttachment(enclosure);
mail.Attachments.Add(ma);
}
//邮件服务器是否需要验证
if(validate)
{
if(userName != "" && password != "")
{
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
}
else
{
return "缺少验证用户名和密码";
}
}
try
{
SmtpMail.SmtpServer = smtpServer;//设置邮件服务器
//发送邮件
SmtpMail.Send(mail);
return "邮件发送成功!";
}
catch
{
return "邮件发送失败!";
}
}