public bool SendEmail(string smtpServer, int port, string username, string password, MailMessage mailMessage) { return(this.SendEmail(smtpServer, port, true, username, password, mailMessage)); }
private bool SendEmail(string smtpServer, int port, bool ESmtp, string username, string password, MailMessage mailMessage) { if (!this.Connect(smtpServer, port)) { return(false); } string priorityString = this.GetPriorityString(mailMessage.Priority); bool flag = mailMessage.BodyFormat == MailFormat.HTML; if (ESmtp) { if (!this.Dialog(new string[4] { "EHLO " + smtpServer + this.CRLF, "AUTH LOGIN" + this.CRLF, this.Base64Encode(username) + this.CRLF, this.Base64Encode(password) + this.CRLF }, "SMTP服务器验证失败,请核对用户名和密码。")) { return(false); } } else if (!this.Dialog("HELO " + smtpServer + this.CRLF, "")) { return(false); } if (!this.Dialog("MAIL FROM:<" + username + ">" + this.CRLF, "发件人地址错误,或不能为空")) { return(false); } string[] str1 = new string[mailMessage.Recipients.Count]; for (int index = 0; index < mailMessage.Recipients.Count; ++index) { str1[index] = "RCPT TO:<" + (string)mailMessage.Recipients[index] + ">" + this.CRLF; } if (!this.Dialog(str1, "收件人地址有误") || !this.Dialog("DATA" + this.CRLF, "")) { return(false); } string str2 = "From:" + mailMessage.FromName + "<" + mailMessage.From + ">" + this.CRLF; if (mailMessage.Recipients.Count == 0) { return(false); } string str3 = str2 + "To:=?" + mailMessage.Charset.ToUpper() + "?B?" + this.Base64Encode((string)mailMessage.Recipients[0]) + "?=<" + (string)mailMessage.Recipients[0] + ">" + this.CRLF; string str4; if (!(mailMessage.Subject == string.Empty) && mailMessage.Subject != null) { if (!(mailMessage.Charset == "")) { str4 = "Subject:=?" + mailMessage.Charset.ToUpper() + "?B?" + this.Base64Encode(mailMessage.Subject) + "?="; } else { str4 = "Subject:" + mailMessage.Subject; } } else { str4 = "Subject:"; } string crlf = this.CRLF; string str5 = str3 + str4 + crlf + "X-Priority:" + priorityString + this.CRLF + "X-MSMail-Priority:" + priorityString + this.CRLF + "Importance:" + priorityString + this.CRLF + "X-Mailer: Lottery.Utils.Mail.SmtpMail Pubclass [cn]" + this.CRLF + "MIME-Version: 1.0" + this.CRLF; if (mailMessage.Attachments.Count != 0) { str5 = str5 + "Content-Type: multipart/mixed;" + this.CRLF + " boundary=\"=====" + (flag ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====\"" + this.CRLF + this.CRLF; } string str6; if (flag) { str6 = (mailMessage.Attachments.Count != 0 ? str5 + "This is a multi-part message in MIME format." + this.CRLF + this.CRLF + "--=====001_Dragon520636771063_=====" + this.CRLF + "Content-Type: multipart/alternative;" + this.CRLF + " boundary=\"=====003_Dragon520636771063_=====\"" + this.CRLF + this.CRLF : str5 + "Content-Type: multipart/alternative;" + this.CRLF + " boundary=\"=====003_Dragon520636771063_=====\"" + this.CRLF + this.CRLF + "This is a multi-part message in MIME format." + this.CRLF + this.CRLF) + "--=====003_Dragon520636771063_=====" + this.CRLF + "Content-Type: text/plain;" + this.CRLF + (mailMessage.Charset == "" ? " charset=\"iso-8859-1\"" : " charset=\"" + mailMessage.Charset.ToLower() + "\"") + this.CRLF + "Content-Transfer-Encoding: base64" + this.CRLF + this.CRLF + this.Base64Encode("邮件内容为HTML格式,请选择HTML方式查看") + this.CRLF + this.CRLF + "--=====003_Dragon520636771063_=====" + this.CRLF + "Content-Type: text/html;" + this.CRLF + (mailMessage.Charset == "" ? " charset=\"iso-8859-1\"" : " charset=\"" + mailMessage.Charset.ToLower() + "\"") + this.CRLF + "Content-Transfer-Encoding: base64" + this.CRLF + this.CRLF + this.Base64Encode(mailMessage.Body) + this.CRLF + this.CRLF + "--=====003_Dragon520636771063_=====--" + this.CRLF; } else { if (mailMessage.Attachments.Count != 0) { str5 = str5 + "--=====001_Dragon303406132050_=====" + this.CRLF; } str6 = str5 + "Content-Type: text/plain;" + this.CRLF + (mailMessage.Charset == "" ? " charset=\"iso-8859-1\"" : " charset=\"" + mailMessage.Charset.ToLower() + "\"") + this.CRLF + "Content-Transfer-Encoding: base64" + this.CRLF + this.CRLF + this.Base64Encode(mailMessage.Body) + this.CRLF; } if (mailMessage.Attachments.Count != 0) { for (int index = 0; index < mailMessage.Attachments.Count; ++index) { string attachment = mailMessage.Attachments[index]; str6 = str6 + "--=====" + (flag ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====" + this.CRLF + "Content-Type: text/plain;" + this.CRLF + " name=\"=?" + mailMessage.Charset.ToUpper() + "?B?" + this.Base64Encode(attachment.Substring(attachment.LastIndexOf("\\") + 1)) + "?=\"" + this.CRLF + "Content-Transfer-Encoding: base64" + this.CRLF + "Content-Disposition: attachment;" + this.CRLF + " filename=\"=?" + mailMessage.Charset.ToUpper() + "?B?" + this.Base64Encode(attachment.Substring(attachment.LastIndexOf("\\") + 1)) + "?=\"" + this.CRLF + this.CRLF + this.GetStream(attachment) + this.CRLF + this.CRLF; } str6 = str6 + "--=====" + (flag ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====--" + this.CRLF + this.CRLF; } if (!this.Dialog(str6 + this.CRLF + "." + this.CRLF, "错误信件信息") || !this.Dialog("QUIT" + this.CRLF, "断开连接时错误")) { return(false); } this.networkStream.Close(); this.tcpClient.Close(); return(true); }
public bool SendEmail(string smtpServer, int port, MailMessage mailMessage) { return(this.SendEmail(smtpServer, port, false, "", "", mailMessage)); }