//Send Mail public void SendMail(SMTPMailMessage mail) { try { MailMessage Newmail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(_strServer); Newmail.From = new MailAddress(_strUserName); Newmail.To.Add(mail.To); Newmail.Subject = mail.Subject; Newmail.Body = mail.Body; System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment(mail.AttachmentPath); Newmail.Attachments.Add(attachment); SmtpServer.EnableSsl = _useSSL; SmtpServer.UseDefaultCredentials = true; SmtpServer.Port = _intPort; SmtpServer.Credentials = new System.Net.NetworkCredential(_strUserName, _strUserPassword); SmtpServer.Send(Newmail); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); return; } }
//-- //-- send mail with integrated retry mechanism //-- //public bool SendMail(SMTPMailMessage mail) //{ // int intRetryInterval = 333; // try // { // SendMailInternal(mail); // } // catch (Exception ex) // { // _intRetries += 1; // if (_intRetries <= _intMaxRetries) // { // Thread.Sleep(intRetryInterval); // SendMail(mail); // } // else // { // throw; // } // } // //Console.WriteLine("sent after " & _intRetries.ToString) // _intRetries = 1; // return true; //} //-- //-- send an email via trivial SMTP //-- private void SendMailInternal(SMTPMailMessage mail) { IPHostEntry iphost = default(IPHostEntry); TcpClient tcp = new TcpClient(); //-- resolve server text name to an IP address try { iphost = Dns.GetHostByName(_strServer); } catch (Exception e) { throw new Exception("Unable to resolve server name " + _strServer, e); } //-- attempt to connect to the server by IP address and port number try { tcp.Connect(iphost.AddressList[0], _intPort); } catch (Exception e) { throw new Exception("Unable to connect to SMTP server at " + _strServer.ToString() + ":" + _intPort.ToString(), e); } //-- make sure we get the SMTP welcome message Command(tcp, "", "220"); Command(tcp, "HELO " + Environment.MachineName); //-- //-- authenticate if we have username and password //-- http://www.ietf.org/rfc/rfc2554.txt //-- if ((_strUserName + _strUserPassword).Length > 0) { Command(tcp, "auth login", "334 VXNlcm5hbWU6"); //VXNlcm5hbWU6=base64'Username:'******'Password:' Command(tcp, ToBase64(_strUserPassword), "235"); } if (string.IsNullOrEmpty(mail.From)) { mail.From = System.AppDomain.CurrentDomain.FriendlyName.ToLower() + "@" + Environment.MachineName.ToLower() + "." + _strDefaultDomain; } Command(tcp, "MAIL FROM: <" + mail.From + ">"); //-- send email to more than one recipient string[] arRecipients = mail.To.Split(_strAddressSeperator.ToCharArray()); string strRecipient = null; foreach (string strRecipient_loopVariable in arRecipients) { strRecipient = strRecipient_loopVariable; Command(tcp, "RCPT TO: <" + strRecipient + ">"); } Command(tcp, "DATA", "354"); StringBuilder objStringBuilder = new StringBuilder(); var _with1 = objStringBuilder; //-- write common email headers _with1.Append("To: " + mail.To + Environment.NewLine); _with1.Append("From: " + mail.From + Environment.NewLine); _with1.Append("Subject: " + mail.Subject + Environment.NewLine); if (_blnPlainTextOnly) { //-- write plain text body _with1.Append(Environment.NewLine + mail.Body + Environment.NewLine); } else { string strContentType = null; //-- typical case; mixed content will be displayed side-by-side strContentType = "multipart/mixed"; //-- unusual case; text and HTML body are both included, let the reader determine which it can handle if (!string.IsNullOrEmpty(mail.Body) & !string.IsNullOrEmpty(mail.BodyHTML)) { strContentType = "multipart/alternative"; } _with1.Append("MIME-Version: 1.0" + Environment.NewLine); _with1.Append("Content-Type: " + strContentType + "; boundary=\"NextMimePart\"" + Environment.NewLine); _with1.Append("Content-Transfer-Encoding: 7bit" + Environment.NewLine); // -- default content (for non-MIME compliant email clients, should be extremely rare) _with1.Append("This message is in MIME format. Since your mail reader does not understand " + Environment.NewLine); _with1.Append("this format, some or all of this message may not be legible." + Environment.NewLine); //-- handle text body (if any) if (!string.IsNullOrEmpty(mail.Body)) { _with1.Append(Environment.NewLine + "--NextMimePart" + Environment.NewLine); _with1.Append("Content-Type: text/plain;" + Environment.NewLine); _with1.Append(Environment.NewLine + mail.Body + Environment.NewLine); } // -- handle HTML body (if any) if (!string.IsNullOrEmpty(mail.BodyHTML)) { _with1.Append(Environment.NewLine + "--NextMimePart" + Environment.NewLine); _with1.Append("Content-Type: text/html; charset=iso-8859-1" + Environment.NewLine); _with1.Append(Environment.NewLine + mail.BodyHTML + Environment.NewLine); } //-- handle attachment (if any) if (!string.IsNullOrEmpty(mail.AttachmentPath)) { _with1.Append(FileToMimeString(mail.AttachmentPath)); } } //-- <crlf>.<crlf> marks end of message content _with1.Append(Environment.NewLine + "." + Environment.NewLine); Command(tcp, objStringBuilder.ToString()); Command(tcp, "QUIT", ""); tcp.Close(); }
//-- //-- send an email via trivial SMTP //-- private void SendMailInternal(SMTPMailMessage mail) { IPHostEntry iphost = null; TcpClient tcp = new TcpClient(); //-- resolve server text name to an IP address try { iphost = Dns.GetHostEntry(_strServer); // GetHostByName(_strServer); } catch (Exception e) { throw new Exception("Unable to resolve server name " + _strServer, e); } //-- attempt to connect to the server by IP address and port number try { tcp.Connect(iphost.AddressList[0], _intPort); } catch (Exception e) { throw new Exception("Unable to connect to SMTP server at " + _strServer.ToString() + ":" + _intPort.ToString(), e); } //-- make sure we get the SMTP welcome message //Interaction.Command(tcp, "", "220"); //Interaction.Command(tcp, "HELO " + Environment.MachineName); Command(tcp, "", "220"); Command(tcp, "HELO " + Environment.MachineName, "250"); //-- //-- authenticate if we have username and password //-- http://www.ietf.org/rfc/rfc2554.txt //-- //if (Strings.Len(_strUserName + _strUserPassword) > 0) if ((_strUserName + _strUserPassword).Length > 0) { //Interaction.Command(tcp, "auth login", "334 VXNlcm5hbWU6"); ////VXNlcm5hbWU6=base64'Username:'******'Password:' //Interaction.Command(tcp, ToBase64(_strUserPassword), "235"); //Command(tcp, "auth login", "334 VXNlcm5hbWU6"); //Command(tcp, ToBase64(_strUserName), "334 UGFzc3dvcmQ6"); Command(tcp, "auth login", "334 VXN"); Command(tcp, ToBase64(_strUserName), "334 UGF"); Command(tcp, ToBase64(_strUserPassword), "235"); } if (string.IsNullOrEmpty(mail.From)) { mail.From = System.AppDomain.CurrentDomain.FriendlyName.ToLower() + "@" + Environment.MachineName.ToLower() + "." + _strDefaultDomain; } //Interaction.Command(tcp, "MAIL FROM: <" + mail.From + ">"); Command(tcp, "MAIL FROM: <" + mail.From + ">", "250"); //-- send email to more than one recipient string[] arRecipients = mail.To.Split(_strAddressSeperator.ToCharArray()); string strRecipient = null; foreach (string strRecipient_loopVariable in arRecipients) { strRecipient = strRecipient_loopVariable; //Interaction.Command(tcp, "RCPT TO: <" + strRecipient + ">"); Command(tcp, "RCPT TO: <" + strRecipient + ">", "250"); } //Interaction.Command(tcp, "DATA", "354"); Command(tcp, "DATA", "354"); System.Text.StringBuilder objStringBuilder = new System.Text.StringBuilder(); var _with1 = objStringBuilder; //-- write common email headers _with1.Append("To: " + mail.To + Environment.NewLine); _with1.Append("From: " + mail.From + Environment.NewLine); _with1.Append("Subject: " + mail.Subject + Environment.NewLine); //if (_blnPlainTextOnly) //{ // //-- write plain text body // _with1.Append(Environment.NewLine + mail.Body + Environment.NewLine); //} //else { string strContentType = null; //-- typical case; mixed content will be displayed side-by-side strContentType = "multipart/mixed"; //-- unusual case; text and HTML body are both included, let the reader determine which it can handle if (!string.IsNullOrEmpty(mail.Body) & !string.IsNullOrEmpty(mail.BodyHTML)) { strContentType = "multipart/alternative"; } _with1.Append("MIME-Version: 1.0" + Environment.NewLine); _with1.Append("Content-Type: " + strContentType + "; boundary=\"NextMimePart\"" + Environment.NewLine); _with1.Append("Content-Transfer-Encoding: 7bit" + Environment.NewLine); // -- default content (for non-MIME compliant email clients, should be extremely rare) _with1.Append("This message is in MIME format. Since your mail reader does not understand " + Environment.NewLine); _with1.Append("this format, some or all of this message may not be legible." + Environment.NewLine); //-- handle text body (if any) if (!string.IsNullOrEmpty(mail.Body)) { _with1.Append(Environment.NewLine + "--NextMimePart" + Environment.NewLine); _with1.Append("Content-Type: text/plain;" + Environment.NewLine); _with1.Append(Environment.NewLine + mail.Body + Environment.NewLine); } // -- handle HTML body (if any) if (!string.IsNullOrEmpty(mail.BodyHTML)) { _with1.Append(Environment.NewLine + "--NextMimePart" + Environment.NewLine); _with1.Append("Content-Type: text/html; charset=iso-8859-1" + Environment.NewLine); _with1.Append(Environment.NewLine + mail.BodyHTML + Environment.NewLine); } //-- handle attachment (if any) if (!string.IsNullOrEmpty(mail.AttachmentPath)) { _with1.Append(FileToMimeString(mail.AttachmentPath)); } } //-- <crlf>.<crlf> marks end of message content _with1.Append(Environment.NewLine + "." + Environment.NewLine); //Interaction.Command(tcp, objStringBuilder.ToString()); //Interaction.Command(tcp, "QUIT", ""); Command(tcp, objStringBuilder.ToString(), "250"); Command(tcp, "QUIT", ""); tcp.Close(); }
//-- //-- send mail with integrated retry mechanism //-- public bool SendMail(SMTPMailMessage mail) { int intRetryInterval = 333; try { SendMailInternal(mail); } catch (Exception) { _intRetries += 1; if (_intRetries <= _intMaxRetries) { //Thread.CurrentThread.Sleep(intRetryInterval); Thread.Sleep(intRetryInterval); SendMail(mail); } else { throw; } } //Console.WriteLine("sent after " & _intRetries.ToString) _intRetries = 1; return true; }