Exemplo n.º 1
0
 public bool Send(MailMessage mailMessage, string username, string password)
 {
     SmtpServerHelper helper = new SmtpServerHelper();
     if (helper.SendEmail(SmtpServer, 25, username, password, mailMessage))
         return true;
     else
     {
         ErrMsg = helper.ErrMsg;
         return false;
     }
 }
Exemplo n.º 2
0
 public bool SendEmail(string smtpServer, int port, string username, string password, MailMessage mailMessage)
 {
     return SendEmail(smtpServer, port, true, username, password, mailMessage);
 }
Exemplo n.º 3
0
 public bool SendEmail(string smtpServer, int port, MailMessage mailMessage)
 {
     return SendEmail(smtpServer, port, false, "", "", mailMessage);
 }
Exemplo n.º 4
0
        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;

            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, ",。")) return false;
            }
            else
            {
                sendBufferstr = "HELO " + smtpServer + _crlf;
                if (!Dialog(sendBufferstr, "")) return false;
            }

            sendBufferstr = "MAIL FROM:<" + username + ">" + _crlf;
            if (!Dialog(sendBufferstr, ",")) return false;

            sendBuffer = new string[mailMessage.Recipients.Count];
            for (int i = 0; i < mailMessage.Recipients.Count; i++)
            {
                sendBuffer[i] = "RCPT TO:<" + (string)mailMessage.Recipients[i] + ">" + _crlf;
            }
            if (!Dialog(sendBuffer, "")) return false;

            sendBufferstr = "DATA" + _crlf;
            if (!Dialog(sendBufferstr, "")) return false;

            sendBufferstr = "From:" + mailMessage.FromName + "<" + mailMessage.From + ">" + _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 += (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(",") + _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;
            }
            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: 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;
        }