.net jMail邮件发送(含抄送、密送、多发、日志记录)实例代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using jmail;
using System.IO;
namespace JmailDemo
{
public class MailAPI
{
///
/// 邮件发送
///
/// 主题
/// 正文
/// 收件人地址
/// 发件人地址,多个地址间用英文逗号或分号隔开
/// 抄送人地址,多个地址间用英文逗号或分号隔开
/// 密送人地址,多个地址间用英文逗号或分号隔开
/// 附件服务器目录,多个服务器端目录之间以英文逗号或分号隔开
public void Send(string zt, string zw, string getMail, string fromMail, string csMail, string msMail, string fjMail)
{
jmail.Message jmessage = new Message();
//Jmail系统配置
jmessage.Charset = "GB2312";
jmessage.Encoding = "base64";
//配置邮件信息
jmessage.Subject = zt.Trim();//邮件主题
jmessage.HTMLBody = zw;//邮件正文
//配置收件人信息
string[] sj = getMail.Split(',', ';');
if (sj.Length > 1)
{
for (int i = 0; i < sj.Length; i++)
{
jmessage.AddRecipient(sj[i], "", "");//收件人邮箱地址
}
}
else
{
jmessage.AddRecipient(sj[0], "", "");
}
//配置抄送人信息
string[] cs = csMail.Split(',', ';');
if (cs.Length > 1)
{
for (int i = 0; i < cs.Length; i++)
{
jmessage.AddRecipientCC(cs[i]);
}
}
else
{
jmessage.AddRecipient(cs[0], "", "");
}
//配置密送人信息
string[] ms = msMail.Split(',', ';');
if (ms.Length > 1)
{
for (int i = 0; i < ms.Length; i++)
{
jmessage.AddRecipientBCC(ms[i]);
}
}
else
{
jmessage.AddRecipient(ms[0], "", "");
}
//配置发件人信息
jmessage.ReplyTo = fromMail;//指定收件人回复的,所回复到的地址
jmessage.From = fromMail;//邮件发件人的邮箱地址
jmessage.FromName = "Test!";//邮件发件人显示的姓名
jmessage.MailServerUserName = fromMail;//用户名身份验证
jmessage.MailServerPassWord = "password";//发送邮件在服务器中的密码
//配置附件
string[] fj = fjMail.Split(',', ';');
if (fj.Length > 1)
{
for (int i = 0; i < fj.Length; i++)
{
jmessage.AddAttachment(fj[i],true,null);
}
}
else
{
jmessage.AddRecipient(sj[0], "", "");
}
//配置追加信息
jmessage.AppendHTML("此邮件发送者");
//发送邮件
for (int i = 0; i < sj.Length; i++)
{
jmessage.Send("smtp.qq.com", false);//发送邮件服务器
}
//配置jmail日志
jmessage.Logging = true;
using (StreamWriter sw = new StreamWriter("E:log.txt", true))
{
sw.Write(jmessage.Log);
sw.Close();
}
jmessage.Close();//邮件发送完毕,关闭邮件发送状态
}
}
}