App.Config文件的配置
<add key="SmtpServer" value="smtp.gmail.com"/><!--服务器地址-->
<add key="MailFrom" value="**@gmail.com"/><!--发件人邮箱-->
<add key="PassWord" value="******"/><!--发件人密码-->
<add key="MailTo" value="******@qq.com"/><!--收件人邮箱-->
<add key="MailSubject" value="邮件的标题"/><!--邮件标题-->
<add key="EnableSsl" value="1"/><!--是否使用安全套接字层加密发送-->
<add key="ServerPort" value="587"/><!--端口-->
<add key="MailBody" value="Dear All,邮件的内容,请查收,谢谢。——***"/><!--邮件正文内容-->
<add key="CarbonCopy" value="****@qq.com"/><!-- 可抄送多人,每个邮箱地址使用";"隔开-->
/// <summary>
/// 发送邮件到用户
/// </summary>
public static void SendEmailToUser()
{
String strSmtpServe = ConfigurationManager.AppSettings["SmtpServer"].ToString();//服务器地址
string strFrom = ConfigurationManager.AppSettings["MailFrom"].ToString();//发件人地址
string strFromPass = ConfigurationManager.AppSettings["PassWord"].ToString();//发件人邮箱密码
string strTo = ConfigurationManager.AppSettings["MailTo"].ToString();//收件人地址
string strSubject = ConfigurationManager.AppSettings["MailSubject"].ToString();//标题
bool IsHtml = true;
bool boolEnableSsl = Convert.ToBoolean(int.Parse(ConfigurationManager.AppSettings["EnableSsl"].ToString()));
int iPort = int.Parse(ConfigurationManager.AppSettings["ServerPort"].ToString());
string strBody = ConfigurationManager.AppSettings["MailBody"].ToString();//内容
string carBonCoyList = ConfigurationManager.AppSettings["CarbonCopy"].ToString();//抄送
SendSMTPEmail(strSmtpServe, strFrom, strFromPass, strTo, strSubject, IsHtml, strBody, iPort, boolEnableSsl, carBonCoyList);//发送第一封邮件
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="strSmtpServer">邮件服务器地址</param>
/// <param name="strFrom">发件人地址</param>
/// <param name="strFromPass">密码</param>
/// <param name="strto">收件人</param>
/// <param name="strSubject">标题</param>
/// <param name="IsHtml">邮件格式是否为Html格式</param>
/// <param name="strBody">内容</param>
/// <param name="iPort"></param>
/// <param name="boolEnableSsl"></param>
public static void SendSMTPEmail(string strSmtpServer, String strFrom, String strFromPass, String strto, String strSubject, Boolean IsHtml, String strBody, int iPort, Boolean boolEnableSsl, string carBonCoyList)
{
try
{
char[] splitter = new char[] { ';' };
string[] CClist = carBonCoyList.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
SmtpClient client = new SmtpClient(strSmtpServer);
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;//邮件发送方式
client.Timeout = 120000;
client.Port = iPort;
client.EnableSsl = boolEnableSsl;
strSubject = strSubject + "(" + DateTime.Now.ToString("yyyyMMdd") + ")";
MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
message.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
foreach (string val in CClist)
{
MailAddress copy = new MailAddress(val);
message.CC.Add(copy);
}
string savepath = ConfigurationManager.AppSettings["savepath"].ToString();//附件
string attaches = Path.Combine(Path.GetDirectoryName(savepath), Path.GetFileName(savepath));
Attachment a1 = new Attachment(attaches);
message.Attachments.Add(a1);
message.IsBodyHtml = IsHtml;
client.Send(message);
}
catch (Exception ex)
{
String err = ex.Message;
}
}