阅读全文并下载实例:http://www.cckan.net/forum.php?mod=viewthread&tid=56
以前我也写过这样一个发送邮件的类
第一种方法
http://www.cnblogs.com/sufei/archive/2009/03/28/1423891.html
第二种方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Web.Mail;
using System.Collections;
/**/
/// <summary>
/// 支持批量(发送、抄送、密送)
/// 支持多语言、带多个附件
/// 支持smtp发送邮件服务器验证
/// </summary>
public class DotNetSendMail
{
public DotNetSendMail()
{ }
/**/
/// <summary>
/// 邮件发送结果。如果发送过程出现错误,该值为捕获到的异常提示;否则,该值为“OK”。
/// </summary>
public string SendMailResult
{
get
{
return this.m_SendMailResult;
}
set
{
this.m_SendMailResult = value;
}
}
private string m_SendMailResult;
/**/
/// <summary>
/// 抄送地址,多个以逗号隔开
/// </summary>
public string Cc
{
set
{
this.m_Cc = value;
}
}
private string m_Cc;
/**/
/// <summary>
/// 密送地址,多个以逗号隔开
/// </summary>
public string Bcc
{
set
{
this.m_Bcc = value;
}
}
private string m_Bcc;
/**/
/// <summary>
/// 邮件格式。可以赋值为“1”或“2”,“1”表示Html格式,“2”表示Text格式。
/// </summary>
public int BodyFormat
{
set
{
this.m_BodyFormat = value;
}
}
private int m_BodyFormat = 1;
/**/
/// <summary>
/// 邮件格式。可以赋值为“1”、“2”或“3”。
///“1”表示不紧急邮件,“2”表示普通邮件, “3”表示紧急邮件
/// </summary>
public int MailPriority
{
set
{
this.m_MailPriority = value;
}
}
private int m_MailPriority = 2;
/**/
/// <summary>
/// 附件地址列表,用该属性Add方法加入多个附件,附件地址必须是绝对路径
/// </summary>
public ArrayList AttachFiles
{
get
{
return this.m_AttachFiles;
}
set
{
this.m_AttachFiles = value;
}
}
ArrayList m_AttachFiles = new ArrayList();
/**/
/// <summary>
/// 发送邮件方法,方法中几个参数是发邮件时所必须的
/// </summary>
/// <param name="p_From">发件人</param>
/// <param name="p_To">邮件人,多个收件人逗号隔开</param>
/// <param name="p_Subject">邮件标题</param>
/// <param name="p_Body">正文</param>
/// <param name="p_SmtpServer">发送邮件服务器</param>
/// <param name="p_SmtpUsername">发件服务器登录名</param>
/// <param name="p_SmtpPassword">发件服务器登录密码</param>
/// <returns></returns>
public bool SendMail(string p_From, string p_To, string p_Subject, string p_Body, string p_SmtpServer, string p_SmtpUsername, string p_SmtpPassword)
{
try
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.From = p_From;
mail.To = p_To;
mail.Subject = p_Subject;
mail.Body = p_Body;
SmtpMail.SmtpServer = p_SmtpServer;
//邮件服务器验证,需要输入您在邮件发送服务器上注册的邮箱用户名和密码
if (p_SmtpUsername != "")
{
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", p_SmtpUsername);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", p_SmtpPassword);
}
//以上是发邮件时所必需的,下面是额外的一些功能设置
if (this.m_Cc != "")
{
mail.Cc = this.m_Cc;
}
if (this.m_Bcc != "")
{
mail.Bcc = this.m_Bcc;
}
switch (this.m_BodyFormat)
{
case 1:
mail.BodyFormat = MailFormat.Html;
break;
case 2:
mail.BodyFormat = MailFormat.Text;
break;
default:
break;
}
switch (this.m_MailPriority)
{
case 1:
mail.Priority = System.Web.Mail.MailPriority.Low;
break;
case 2:
mail.Priority = System.Web.Mail.MailPriority.Normal;
break;
case 3:
mail.Priority = System.Web.Mail.MailPriority.High;
break;
default:
break;
}
if (this.m_AttachFiles.Count > 0)
{
foreach (string file in m_AttachFiles)
{
if (file.Trim() != "")
{
mail.Attachments.Add(new MailAttachment(file.Trim()));
}
}
}
SmtpMail.Send(mail);
this.m_SendMailResult = "OK";
return true;
}
catch (Exception ex)
{
this.m_SendMailResult = ex.ToString();
return false;
}
}
}
但是现在做项目要用,不知道为什么不能用了, 之前还有几种方法,都要收费了真是不明白,发个邮件还要收费,
1.为什么收费
2.为什么第一种方法里只有06年以前163的邮箱才能发送
3.第二种方法是我找的园子里一个哥门儿的还可以用着,在这里谢谢了,大家不知道有没有更好的方案,贴出来玩玩吧。
第三种方法 最好用也最实用
直接上代码了
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace BLL.Function
{
public class EmailServices
{
///<summary>
/// 发送邮件
///</summary>
///<param name="sendEmailAddress">发件人邮箱</param>
///<param name="sendEmailPwd">发件人密码</param>
///<param name="msgToEmail">收件人邮箱地址</param>
///<param name="title">邮件标题</param>
///<param name="content">邮件内容</param>
///<returns>0:失败。1:成功!</returns>
public static int SendEmail(string sendEmailAddress, string sendEmailPwd, string[] msgToEmail, string title, string content)
{
//发件者邮箱地址
string fjrtxt = sendEmailAddress;
//发件者邮箱密码
string mmtxt = sendEmailPwd;
////收件人收箱地址
//string sjrtxt = msgToEmail;
//主题
string zttxt = title;
//附件
//string fjtxt = fj.Text;
//内容
string nrtxt = content;
string[] fasong = fjrtxt.Split('@');
string[] fs = fasong[1].Split('.');
//发送
//设置邮件协议
SmtpClient client = new SmtpClient("smtp." + fs[0].ToString().Trim() + ".com");
client.UseDefaultCredentials = false;
//通过网络发送到Smtp服务器
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//通过用户名和密码 认证
client.Credentials = new NetworkCredential(fasong[0].ToString(), mmtxt);
//发件人和收件人的邮箱地址
MailMessage mmsg = new MailMessage();
mmsg.From = new MailAddress(fjrtxt);
for (int i = 0; i < msgToEmail.Length; i++)
{
mmsg.To.Add(new MailAddress(msgToEmail[i]));
}
//邮件主题
mmsg.Subject = zttxt;
//主题编码
mmsg.SubjectEncoding = Encoding.UTF8;
//邮件正文
mmsg.Body = nrtxt;
//正文编码
mmsg.BodyEncoding = Encoding.UTF8;
//设置为HTML格式
mmsg.IsBodyHtml = true;
//优先级
mmsg.Priority = MailPriority.High;
//if (fj.Text.Trim() != "")
//{
////增加附件
// mmsg.Attachments.Add(new Attachment(fj.Text));
//}
try
{
client.Send(mmsg);
return 1;
}
catch
{
return 0;
}
}
}
}