public void CreateHost(ConfigHost host) { Host = new SMTP_Client(); Host.Connect(host.Server, host.Port, host.EnableSsl); Host.EhloHelo(host.Server); Host.Auth(Host.AuthGetStrongestMethod(host.Username, host.Password)); }
private Boolean SendMailNew(bool isAsync, object userState) { string[] mailTos = mMailTo; string[] mailCcs = mMailCc; string[] mailBccs = mMailBcc; Dictionary <string, string> attachments = mMailAttachments; var toList = new Dictionary <string, string>(); foreach (var to in MailTo) { toList.Add(to, to); } var musername = mSMTPUsername; if (mSMTPUsername != null && mSMTPUsername.IndexOf("@") >= 0) { musername = mSMTPUsername.Substring(0, mSMTPUsername.IndexOf("@")); } bool checkspmail = CheckSpecialMail(toList); if (!checkspmail && MailCc != null) { checkspmail = CheckSpecialMail(MailCc); } if (checkspmail) { foreach (string address in toList.Keys) { using (SMTP_Client client = new SMTP_Client()) { client.Timeout = this.Timeout; client.Connect(mSMTPServer, mSMTPPort); client.EhloHelo(mSMTPServer); client.Authenticate(musername, mSMTPPassword); client.MailFrom(mMailFrom, -1); client.RcptTo(address); Mail_Message m = Create_PlainText_Html_Attachment_Image(toList, new Dictionary <string, string>() , mMailFrom, mMailFrom, MailSubject, mMailBody, attachments, "", "", CheckSpecialMail(new string[] { address })); try { using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { m.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8); stream.Position = 0; client.SendMessage(stream); } } catch (Exception ex) { throw ex; } if (m != null) { m.Dispose(); } client.Disconnect(); } } return(true); } else { using (SMTP_Client client = new SMTP_Client()) { client.Timeout = this.Timeout; client.Connect(mSMTPServer, mSMTPPort); client.EhloHelo(mSMTPServer); AUTH_SASL_Client authhh = client.AuthGetStrongestMethod(musername, mSMTPPassword); client.Auth(authhh); client.MailFrom(mMailFrom, -1); foreach (string address in toList.Keys) { client.RcptTo(address); } Mail_Message m = Create_PlainText_Html_Attachment_Image(toList, new Dictionary <string, string>() , mMailFrom, mMailFrom, MailSubject, mMailBody, attachments, "", "", false); try { using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { m.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8); stream.Position = 0; client.SendMessage(stream); } return(true); } catch (Exception ex) { throw ex; } finally { if (m != null) { m.Dispose(); } client.Disconnect(); } } } }
/// <summary> /// Is called when EHLO/HELO command has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> /// <exception cref="ArgumentNullException">Is raised when <b>op</b> is null reference.</exception> private void EhloCommandCompleted(SMTP_Client.EhloHeloAsyncOP op) { if (op == null) { throw new ArgumentNullException("op"); } try{ if (op.Error != null) { Dispose(op.Error); } else { // Start TLS requested, start switching to secure. if (!m_pSmtpClient.IsSecureConnection && ((m_pServer.UseTlsIfPossible && IsTlsSupported()) || m_pActiveTarget.SslMode == SslMode.TLS)) { SMTP_Client.StartTlsAsyncOP startTlsOP = new SMTP_Client.StartTlsAsyncOP(null); startTlsOP.CompletedAsync += delegate(object s, EventArgs <SMTP_Client.StartTlsAsyncOP> e){ StartTlsCommandCompleted(startTlsOP); }; if (!m_pSmtpClient.StartTlsAsync(startTlsOP)) { StartTlsCommandCompleted(startTlsOP); } } // Authentication requested, start authenticating. else if (!string.IsNullOrEmpty(m_pActiveTarget.UserName)) { SMTP_Client.AuthAsyncOP authOP = new SMTP_Client.AuthAsyncOP(m_pSmtpClient.AuthGetStrongestMethod(m_pActiveTarget.UserName, m_pActiveTarget.Password)); authOP.CompletedAsync += delegate(object s, EventArgs <SMTP_Client.AuthAsyncOP> e){ AuthCommandCompleted(authOP); }; if (!m_pSmtpClient.AuthAsync(authOP)) { AuthCommandCompleted(authOP); } } // Start MAIL command. else { long messageSize = -1; try{ messageSize = m_pRelayItem.MessageStream.Length - m_pRelayItem.MessageStream.Position; } catch { // Stream doesn't support seeking. } SMTP_Client.MailFromAsyncOP mailOP = new SMTP_Client.MailFromAsyncOP( this.From, messageSize, IsDsnSupported() ? m_pRelayItem.DSN_Ret : SMTP_DSN_Ret.NotSpecified, IsDsnSupported() ? m_pRelayItem.EnvelopeID : null ); mailOP.CompletedAsync += delegate(object s, EventArgs <SMTP_Client.MailFromAsyncOP> e){ MailCommandCompleted(mailOP); }; if (!m_pSmtpClient.MailFromAsync(mailOP)) { MailCommandCompleted(mailOP); } } } } catch (Exception x) { Dispose(x); } }