Ejemplo n.º 1
0
 public static bool mySendMailc1(string SmtpServer, string FromEmail, string FromUserName, string ToEmail, string Subject, string BodyFormat, string BodyContent, string AttachmentFilePath, string Password)
 {
     SmtpMail.SmtpServer = SmtpServer;
     MailMessage mailMessage = new MailMessage {
         From = FromEmail,
         FromName = FromUserName
     };
     mailMessage.AddRecipients(ToEmail);
     mailMessage.Subject = Subject;
     if (BodyFormat == "Text")
     {
         mailMessage.BodyFormat = MailFormat.Text;
     }
     else
     {
         mailMessage.BodyFormat = MailFormat.HTML;
     }
     mailMessage.Body = BodyContent;
     if ((AttachmentFilePath != "") && (AttachmentFilePath != null))
     {
         mailMessage.Attachments.Add(AttachmentFilePath);
     }
     return SmtpMail.Send(mailMessage, FromEmail.Substring(0, FromEmail.LastIndexOf("@")), Password);
 }
Ejemplo n.º 2
0
 public static bool Send(MailMessage mailMessage, int port, string username, string password)
 {
     SmtpServerHelper helper = new SmtpServerHelper();
     log.TraceLog("port: " + port.ToString());
     return helper.SendEmail(_SmtpServer, port, username, password, mailMessage);
 }
Ejemplo n.º 3
0
 public static bool Send(MailMessage mailMessage, string username, string password)
 {
     SmtpServerHelper helper = new SmtpServerHelper();
     return helper.SendEmail(_SmtpServer, 0x19, username, password, mailMessage);
 }
Ejemplo n.º 4
0
 public bool SendEmail(string smtpServer, int port, string username, string password, MailMessage mailMessage)
 {
     return this.SendEmail(smtpServer, port, true, username, password, mailMessage);
 }
Ejemplo n.º 5
0
 public bool SendEmail(string smtpServer, int port, MailMessage mailMessage)
 {
     return this.SendEmail(smtpServer, port, false, "", "", mailMessage);
 }
Ejemplo n.º 6
0
 private bool SendEmail(string smtpServer, int port, bool ESmtp, string username, string password, MailMessage mailMessage)
 {
     string[] strArray;
     string str;
     int num;
     if (!this.Connect(smtpServer, port))
     {
         log.TraceLog("连接邮件服务器不成功,退出");
         return false;
     }
     log.TraceLog("连接邮件服务器成功");
     string priorityString = this.GetPriorityString(mailMessage.Priority);
     bool flag = mailMessage.BodyFormat == MailFormat.HTML;
     log.TraceLog(string.Concat(new object[] { "取得相关数据 priority=", priorityString, " BodyFormat=", mailMessage.BodyFormat }));
     if (ESmtp)
     {
         log.TraceLog("SMTP服务器开始认证状态");
         strArray = new string[] { "EHLO " + smtpServer + this.CRLF, "AUTH LOGIN" + this.CRLF, this.Base64Encode(username) + this.CRLF, this.Base64Encode(password) + this.CRLF };
         if (!this.Dialog(strArray, "SMTP服务器验证失败,请核对用户名和密码。"))
         {
             log.TraceLog("SMTP服务器认证失败");
             return false;
         }
     }
     else
     {
         log.TraceLog("SMTP服务器不需要身份认证状态");
         str = "HELO " + smtpServer + this.CRLF;
         if (!this.Dialog(str, ""))
         {
             return false;
         }
     }
     str = "MAIL FROM:<" + mailMessage.From + ">" + this.CRLF;
     if (!this.Dialog(str, "发件人地址错误,或不能为空"))
     {
         return false;
     }
     strArray = new string[mailMessage.Recipients.Count];
     for (num = 0; num < mailMessage.Recipients.Count; num++)
     {
         strArray[num] = "RCPT TO:<" + ((string) mailMessage.Recipients[num]) + ">" + this.CRLF;
     }
     if (!this.Dialog(strArray, "收件人地址有误"))
     {
         return false;
     }
     str = "DATA" + this.CRLF;
     if (!this.Dialog(str, ""))
     {
         return false;
     }
     str = "From:" + mailMessage.FromName + "<" + mailMessage.From + ">" + this.CRLF;
     if (mailMessage.Recipients.Count == 0)
     {
         return false;
     }
     string str3 = str;
     str = str3 + "To:=?" + mailMessage.Charset.ToUpper() + "?B?" + this.Base64Encode((string) mailMessage.Recipients[0]) + "?=<" + ((string) mailMessage.Recipients[0]) + ">" + this.CRLF;
     str = str + (((mailMessage.Subject == string.Empty) || (mailMessage.Subject == null)) ? "Subject:" : ((mailMessage.Charset == "") ? ("Subject:" + mailMessage.Subject) : ("Subject:=?" + mailMessage.Charset.ToUpper() + "?B?" + this.Base64Encode(mailMessage.Subject) + "?="))) + this.CRLF + "X-Priority:" + priorityString + this.CRLF + "X-MSMail-Priority:" + priorityString + this.CRLF + "Importance:" + priorityString + this.CRLF + "X-Mailer: Lion.Web.Mail.SmtpMail Pubclass [cn]" + this.CRLF + "MIME-Version: 1.0" + this.CRLF;
     if (mailMessage.Attachments.Count != 0)
     {
         str3 = str + "Content-Type: multipart/mixed;" + this.CRLF;
         str = str3 + " boundary=\"=====" + (flag ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====\"" + this.CRLF + this.CRLF;
     }
     if (flag)
     {
         if (mailMessage.Attachments.Count == 0)
         {
             str = str + "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;
         }
         else
         {
             str = str + "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;
         }
         str = str + "--=====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)
         {
             str = str + "--=====001_Dragon303406132050_=====" + this.CRLF;
         }
         str = str + "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 (num = 0; num < mailMessage.Attachments.Count; num++)
         {
             string filePath = mailMessage.Attachments[num];
             str3 = str;
             str3 = str3 + "--=====" + (flag ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====" + this.CRLF + "Content-Type: text/plain;" + this.CRLF;
             str3 = str3 + " name=\"=?" + mailMessage.Charset.ToUpper() + "?B?" + this.Base64Encode(filePath.Substring(filePath.LastIndexOf(@"\") + 1)) + "?=\"" + this.CRLF + "Content-Transfer-Encoding: base64" + this.CRLF + "Content-Disposition: attachment;" + this.CRLF;
             str = str3 + " filename=\"=?" + mailMessage.Charset.ToUpper() + "?B?" + this.Base64Encode(filePath.Substring(filePath.LastIndexOf(@"\") + 1)) + "?=\"" + this.CRLF + this.CRLF + this.GetStream(filePath) + this.CRLF + this.CRLF;
         }
         str3 = str;
         str = str3 + "--=====" + (flag ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====--" + this.CRLF + this.CRLF;
     }
     str = str + this.CRLF + "." + this.CRLF;
     if (!this.Dialog(str, "错误信件信息"))
     {
         return false;
     }
     str = "QUIT" + this.CRLF;
     if (!this.Dialog(str, "断开连接时错误"))
     {
         return false;
     }
     this.networkStream.Close();
     this.tcpClient.Close();
     return true;
 }