===============
app.config
<?xml version="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtp from="liufjay@163.com">
<network host="smtp.163.com" port="25" userName="test" password="test"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
发送邮件的方法
/// <summary>
/// 发送邮件方法
/// </summary>
/// <param name="from">Sender address</param>
/// <param name="to">Recepient address</param>
/// <param name="bcc">Bcc recepient</param>
/// <param name="cc">Cc recepient</param>
/// <param name="subject">Subject of mail message</param>
/// <param name="body">Body of mail message</param>
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mMailMessage.To.Add(new MailAddress(to));
// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
// Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
} // Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Send(mMailMessage);
}
调用的方法
private void button1_Click(object sender, EventArgs e)
{
//SendMailMessage("test@163.com", "test@qq.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "mack", "mackmailinfo");
SendMailMessage("testy@163.com", "test@qq.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "mack", "mackmailinfo");
}
===================================================================================
第二种方法(包含附件方法)
/// <summary>
/// C#发送邮件函数
/// </summary>
/// <param name="from">发送者邮箱</param>
/// <param name="fromer">发送人</param>
/// <param name="to">接受者邮箱</param>
/// <param name="toer">收件人</param>
/// <param name="Subject">主题</param>
/// <param name="Body">内容</param>
/// <param name="file">附件</param>
/// <param name="SMTPHost">smtp服务器</param>
/// <param name="SMTPuser">邮箱</param>
/// <param name="SMTPpass">密码</param>
/// <returns></returns>
public bool sendmail(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody, string sfile, string sSMTPHost, string sSMTPuser, string sSMTPpass)
{
//设置from和to地址
MailAddress from = new MailAddress(sfrom, sfromer);
MailAddress to = new MailAddress(sto, stoer);
//创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to);
// 添加附件
if (sfile != "")
{
oMail.Attachments.Add(new Attachment(sfile));
}
//邮件标题
oMail.Subject = sSubject;
//邮件内容
oMail.Body = sBody;
//邮件格式
oMail.IsBodyHtml = false;
//邮件采用的编码
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
//设置邮件的优先级为高
oMail.Priority = MailPriority.High;
//发送邮件
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Host = sSMTPHost;
client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(oMail);
return true;
}
catch (Exception err)
{
MessageBox.Show( err.Message.ToString());
return false;
}
finally
{
//释放资源
oMail.Dispose();
}
}
方法调用
//发送邮件
string from = "test@163.com"; //发送者邮箱
string fromer = "tt"; //发送人
string to = "totest@163.com"; //接受者邮箱
string toer = "ss"; //收件人
string Subject = "test_mail"; //主题
string file = System.AppDomain.CurrentDomain.BaseDirectory + "test.xlsx"; //附件
string Body = "发送表格邮件 请查收"; //内容
string SMTPHost = "smtp.163.com"; //smtp服务器
string SMTPuser = "test@163.com"; //邮箱
string SMTPpass = "testpw"; //密码
sendmail(from, fromer, to, toer, Subject, Body, file, SMTPHost, SMTPuser, SMTPpass);