C# 发送邮件 .net SendEmail 源码
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace EZhi.COMP
{
/// <summary>
/// 发送邮件类
/// </summary>
public class SendEmail
{
/// <summary>
/// [SMTP服务器域名或IP地址]
/// </summary>
public string SmtpServer = "";
/// <summary>
/// [发件人Email地址]
/// </summary>
public string From= "";
/// <summary>
/// [发件人Email密码]
/// </summary>
public string FromPass= "";
/// <summary>
/// [收件人Email地址]
/// </summary>
public string To= "";
/// <summary>
/// 邮件主题
/// </summary>
public string Subject= "";
/// <summary>
/// 邮件内容
/// </summary>
public string Body= "";
public SendEmail()
{
this.SmtpServer = "smtp.163.com";
this.From = "ezhioladmin@163.com";
this.FromPass = "sulincn";
}
public SendEmail(string strTo, string strSubject, string strBody)
{
this.SmtpServer = "smtp.163.com";
this.From = "ezhioladmin@163.com";
this.FromPass = "sulincn";
this.To = strTo;
this.Subject = strSubject;
this.Body = strBody;
}
public SendEmail(string strSmtpServer, string strFrom, string strFromPass, string strTo, string strSubject, string strBody)
{
this.SmtpServer = strSmtpServer;
this.From = strFrom;
this.FromPass = strFromPass;
this.To = strTo;
this.Subject = strSubject;
this.Body = strBody;
}
public void Send()
{
SmtpClient client = new SmtpClient(SmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(From, FromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage message = new MailMessage(From, To);
//message.CC.Add(new MailAddress(To));//抄送
//message.Bcc.Add(new MailAddress(To)); //暗抄
message.Subject = Subject;//主题
message.Body = Body;//内容
message.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
message.IsBodyHtml = true;//设置为HTML格式
message.Priority = MailPriority.High;//优先级
client.Send(message);
}
public void Send(string to,string body,string subject)
{
string from = From;//"123@163.com"; //发送方邮箱
MailMessage newEmail = new MailMessage();
newEmail.From = new MailAddress(from, from);
newEmail.To.Add(new MailAddress(to)); //接收方邮箱一
//newEmail.To.Add(new MailAddress("132@hotmail.com")); //接收方邮箱二
newEmail.Subject = subject; //标题
newEmail.Body = body; //内容
newEmail.IsBodyHtml = true; //是否支持html
newEmail.Priority = MailPriority.Normal; //优先级
//发送方服务器信息
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential(from, FromPass);
smtpClient.Host = SmtpServer;//"smtp.163.com"; //主机
smtpClient.Port = 25;
//smtpClient.Timeout = 0;
smtpClient.Send(newEmail); //同步发送,程序将被阻塞
}
}
}
以上内容为本人业余时间整理,部分内容来源网络,如有错误欢迎各位批评指正。
*/