public void TestMailMessage() { var message = new MailMessage(); Assert.AreEqual(0, message.Attachments.Count, "初始化MailAttachments"); Assert.AreEqual(MailFormat.Text, message.BodyFormat, "邮件格式"); Assert.AreEqual("GB2312", message.Charset, "缺省的字符集"); }
public void TestSendMail() { SmtpMail.SmtpServer = "smtp.126.com"; var mail = new MailMessage {From = "*****@*****.**", FromName = "陈春伟"}; mail.AddRecipients("*****@*****.**"); mail.Subject = "主题:测试邮件"; mail.BodyFormat = MailFormat.Text; mail.Body = "测试的内容."; //mail.Attachments.Add("c:\\test.txt"); SmtpMail.Send(mail, "*****@*****.**", "");//请填写自己的测试邮件帐号 }
public static bool Send(MailMessage mailMessage, string username, string password) { var helper = new SmtpServerHelper(); return helper.SendEmail(SmtpServer, 25, username, password, mailMessage); }
/// <summary> /// 发送电子邮件,SMTP服务器需要身份验证 /// </summary> /// <param name="smtpServer"></param> /// <param name="port"></param> /// <param name="username"></param> /// <param name="password"></param> /// <param name="mailMessage"></param> /// <returns></returns> public bool SendEmail(string smtpServer, int port, string username, string password, MailMessage mailMessage) { return SendEmail(smtpServer, port, false, username, password, mailMessage); }
/// <summary> /// 发送电子邮件,SMTP服务器不需要身份验证 /// </summary> /// <param name="smtpServer"></param> /// <param name="port"></param> /// <param name="mailMessage"></param> /// <returns></returns> public bool SendEmail(string smtpServer, int port, MailMessage mailMessage) { return SendEmail(smtpServer, port, false, "", "", mailMessage); }
private bool SendEmail(string smtpServer, int port, bool ESmtp, string username, string password, MailMessage mailMessage) { if (Connect(smtpServer, port) == false)//测试连接服务器是否成功 return false; string priority = GetPriorityString(mailMessage.Priority); bool html = (mailMessage.BodyFormat == MailFormat.Html); string[] sendBuffer; string sendBufferstr; //进行SMTP验证,现在大部分SMTP服务器都要认证 if (ESmtp) { sendBuffer = new String[4]; sendBuffer[0] = "EHLO " + smtpServer + CRLF; sendBuffer[1] = "AUTH LOGIN" + CRLF; sendBuffer[2] = Base64Encode(username) + CRLF; sendBuffer[3] = Base64Encode(password) + CRLF; if (!Dialog(sendBuffer, "SMTP服务器验证失败,请核对用户名和密码。")) return false; } else {//不需要身份认证 sendBufferstr = "HELO " + smtpServer + CRLF; if (!Dialog(sendBufferstr, "")) return false; } //发件人地址 sendBufferstr = "MAIL FROM:<" + mailMessage.From + ">" + CRLF; if (!Dialog(sendBufferstr, "发件人地址错误,或不能为空")) return false; //收件人地址 sendBuffer = new string[mailMessage.Recipients.Count]; for (var i = 0; i < mailMessage.Recipients.Count; i++) { sendBuffer[i] = "RCPT TO:<" + (string)mailMessage.Recipients[i] + ">" + CRLF; } if (!Dialog(sendBuffer, "收件人地址有误")) return false; /* SendBuffer=new string[10]; for(int i=0;i<RecipientBCC.Count;i++) { SendBuffer[i]="RCPT TO:<" + RecipientBCC[i].ToString() +">" + CRLF; } if(!Dialog(SendBuffer,"密件收件人地址有误")) return false; */ sendBufferstr = "DATA" + CRLF; if (!Dialog(sendBufferstr, "")) return false; //发件人姓名 sendBufferstr = "From:" + mailMessage.FromName + "<" + mailMessage.From + ">" + CRLF; //if(ReplyTo.Trim()!="") //{ // SendBufferstr+="Reply-To: " + ReplyTo + CRLF; //} //SendBufferstr+="To:" + ToName + "<" + Recipient[0] +">" +CRLF; //至少要有一个收件人 if (mailMessage.Recipients.Count == 0) { return false; } else { sendBufferstr += "To:=?" + mailMessage.Charset.ToUpper() + "?B?" + Base64Encode((string)mailMessage.Recipients[0]) + "?=" + "<" + (string)mailMessage.Recipients[0] + ">" + CRLF; } //SendBufferstr+="CC:"; //for(int i=0;i<Recipient.Count;i++) //{ // SendBufferstr+=Recipient[i].ToString() + "<" + Recipient[i].ToString() +">,"; //} //SendBufferstr+=CRLF; sendBufferstr += (string.IsNullOrEmpty(mailMessage.Subject) ? "Subject:" : ((mailMessage.Charset == "") ? ("Subject:" + mailMessage.Subject) : ("Subject:" + "=?" + mailMessage.Charset.ToUpper() + "?B?" + Base64Encode(mailMessage.Subject) + "?="))) + CRLF; sendBufferstr += "X-Priority:" + priority + CRLF; sendBufferstr += "X-MSMail-Priority:" + priority + CRLF; sendBufferstr += "Importance:" + priority + CRLF; sendBufferstr += "X-Mailer: Lion.Web.Mail.SmtpMail Pubclass [cn]" + CRLF; sendBufferstr += "MIME-Version: 1.0" + CRLF; if (mailMessage.Attachments.Count != 0) { sendBufferstr += "Content-Type: multipart/mixed;" + CRLF; sendBufferstr += " boundary=\"=====" + (html ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====\"" + CRLF + CRLF; } if (html) { if (mailMessage.Attachments.Count == 0) { sendBufferstr += "Content-Type: multipart/alternative;" + CRLF;//内容格式和分隔符 sendBufferstr += " boundary=\"=====003_Dragon520636771063_=====\"" + CRLF + CRLF; sendBufferstr += "This is a multi-part message in MIME format." + CRLF + CRLF; } else { sendBufferstr += "This is a multi-part message in MIME format." + CRLF + CRLF; sendBufferstr += "--=====001_Dragon520636771063_=====" + CRLF; sendBufferstr += "Content-Type: multipart/alternative;" + CRLF;//内容格式和分隔符 sendBufferstr += " boundary=\"=====003_Dragon520636771063_=====\"" + CRLF + CRLF; } sendBufferstr += "--=====003_Dragon520636771063_=====" + CRLF; sendBufferstr += "Content-Type: text/plain;" + CRLF; sendBufferstr += ((mailMessage.Charset == "") ? (" charset=\"iso-8859-1\"") : (" charset=\"" + mailMessage.Charset.ToLower() + "\"")) + CRLF; sendBufferstr += "Content-Transfer-Encoding: base64" + CRLF + CRLF; sendBufferstr += Base64Encode("邮件内容为HTML格式,请选择HTML方式查看") + CRLF + CRLF; sendBufferstr += "--=====003_Dragon520636771063_=====" + CRLF; sendBufferstr += "Content-Type: text/html;" + CRLF; sendBufferstr += ((mailMessage.Charset == "") ? (" charset=\"iso-8859-1\"") : (" charset=\"" + mailMessage.Charset.ToLower() + "\"")) + CRLF; sendBufferstr += "Content-Transfer-Encoding: base64" + CRLF + CRLF; sendBufferstr += Base64Encode(mailMessage.Body) + CRLF + CRLF; sendBufferstr += "--=====003_Dragon520636771063_=====--" + CRLF; } else { if (mailMessage.Attachments.Count != 0) { sendBufferstr += "--=====001_Dragon303406132050_=====" + CRLF; } sendBufferstr += "Content-Type: text/plain;" + CRLF; sendBufferstr += ((mailMessage.Charset == "") ? (" charset=\"iso-8859-1\"") : (" charset=\"" + mailMessage.Charset.ToLower() + "\"")) + CRLF; sendBufferstr += "Content-Transfer-Encoding: base64" + CRLF + CRLF; sendBufferstr += Base64Encode(mailMessage.Body) + CRLF; } //SendBufferstr += "Content-Transfer-Encoding: base64"+CRLF; if (mailMessage.Attachments.Count != 0) { for (int i = 0; i < mailMessage.Attachments.Count; i++) { string filepath = (string)mailMessage.Attachments[i]; sendBufferstr += "--=====" + (html ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====" + CRLF; //SendBufferstr += "Content-Type: application/octet-stream"+CRLF; sendBufferstr += "Content-Type: text/plain;" + CRLF; sendBufferstr += " name=\"=?" + mailMessage.Charset.ToUpper() + "?B?" + Base64Encode(filepath.Substring(filepath.LastIndexOf("\\") + 1)) + "?=\"" + CRLF; sendBufferstr += "Content-Transfer-Encoding: base64" + CRLF; sendBufferstr += "Content-Disposition: attachment;" + CRLF; sendBufferstr += " filename=\"=?" + mailMessage.Charset.ToUpper() + "?B?" + Base64Encode(filepath.Substring(filepath.LastIndexOf("\\") + 1)) + "?=\"" + CRLF + CRLF; sendBufferstr += GetStream(filepath) + CRLF + CRLF; } sendBufferstr += "--=====" + (html ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====--" + CRLF + CRLF; } sendBufferstr += CRLF + "." + CRLF;//内容结束 if (!Dialog(sendBufferstr, "错误信件信息")) return false; sendBufferstr = "QUIT" + CRLF; if (!Dialog(sendBufferstr, "断开连接时错误")) return false; _networkStream.Close(); _tcpClient.Close(); return true; }