一:发邮件
1. //首先创建一个邮件对象
Mail_Message m = Creat_PlainText_Html_Attachment_Image(addInfos, from_email, from_name, vTopic, vContent, vContent, attchInfos, true,dt);
MemoryStream ms = new MemoryStream();
MIME_Encoding_EncodedWord mee = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8);
m.ToStream(ms, mee, Encoding.UTF8);
ms.Position = 0;
//smtp连接
SMTP_Client smtp = new SMTP_Client();
smtp.Connect(smtpServer, smtpPort, smtpUseSsl);
smtp.EhloHelo(smtpServer);
var auth = new AUTH_SASL_Client_Plain(from_email, password);
smtp.Auth(auth);
smtp.MailFrom(from_email, -1);
foreach (MailaddsInfo ma in addInfos)
{//多个收件人时循环,单个收件人可省略循环步骤
smtp.RcptTo(ma.MailAdd);
}
//发送邮件
smtp.SendMessage(ms);
2.创建邮件实体类的方法
private static Mail_Message Creat_PlainText_Html_Attachment_Image(List<MailaddsInfo> addInfos, string sendAdd, string sendName, string subject, string body, string txtBody, List<MailattachmentInfos> attachInfos, bool notify,DateTime dt)
{
Mail_Message m = new Mail_Message();
m.MimeVersion = "1.0";
m.MessageID = MIME_Utils.CreateMessageID();//id
m.Date = dt;//时间
m.Subject = subject;//主题
m.From = new Mail_t_MailboxList();
//sendName:diaplayName,sendAdd:address
m.From.Add(new Mail_t_Mailbox(sendName, sendAdd));//发件人
m.To = new Mail_t_AddressList();//收件人
m.Cc = new Mail_t_AddressList();
m.Bcc = new Mail_t_AddressList();
foreach (MailaddsInfo addinfo in addInfos)
{
switch (addinfo.addType)
{
case "1":
m.To.Add(new Mail_t_Mailbox(addinfo.MailName, addinfo.MailAdd));
break;
case "2":
m.Cc.Add(new Mail_t_Mailbox(addinfo.MailName, addinfo.MailAdd));
break;
case "3":
m.Bcc.Add(new Mail_t_Mailbox(addinfo.MailName, addinfo.MailAdd));
break;
default:
break;
}
}
//--- multipart/mixed -----------------------------------
MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);
contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed);
m.Body = multipartMixed;
//--- multipart/alternative -----------------------------
MIME_Entity entity_multipartAlternative = new MIME_Entity();
MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative);
contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative);
entity_multipartAlternative.Body = multipartAlternative;
multipartMixed.BodyParts.Add(entity_multipartAlternative);
//--- text/plain ----------------------------------------
MIME_Entity entity_text_plain = new MIME_Entity();
MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain);
entity_text_plain.Body = text_plain;
//普通文本邮件内容,如果对方的收件客户端不支持HTML,这是必需的
string plainTextBody = txtBody;
text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, plainTextBody);
multipartAlternative.BodyParts.Add(entity_text_plain);
//--- text/html -----------------------------------------
//内容
string htmlText = body;//"<html>这是一份测试邮件,<img src=\"cid:test.jpg\">来自<font color=red><b>LumiSoft.Net</b></font></html>";
MIME_Entity entity_text_html = new MIME_Entity();
MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html);
entity_text_html.Body = text_html;
text_html.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, htmlText);
multipartAlternative.BodyParts.Add(entity_text_html);
//附件
//--- application/octet-stream -------------------------
if (attachInfos.Count > 0) {
WebClient client = new WebClient();
foreach (MailattachmentInfos attachInfo in attachInfos)
{
byte[] bytes = client.DownloadData(attachInfo.FilePath);
MemoryStream stream = new MemoryStream(bytes);
string fileName = attachInfo.FileName;
MIME_Entity entity_attach = new MIME_Entity();
entity_attach = Mail_Message.CreateAttachment(stream, fileName);
entity_attach.ContentDisposition = new MIME_h_ContentDisposition(MIME_DispositionTypes.Attachment);
entity_attach.ContentDisposition.Param_FileName = fileName;
entity_attach.ContentType.Param_Name = fileName;
entity_attach.ContentDisposition.Parameters.EncodeRfc2047 = true;
entity_attach.ContentType.Parameters.EncodeRfc2047 = true;
multipartMixed.BodyParts.Add(entity_attach);
}
}
//设置回执通知
if (notify)
{
m.DispositionNotificationTo = new Mail_t_MailboxList();
m.DispositionNotificationTo.Add(new Mail_t_Mailbox(sendName, sendAdd));
}
return m;
}