/// <summary> /// Send the email message to the mail server /// </summary> /// <param name="Message">Email message</param> public void SendMail ( SecureSmtpMessage Message ) { try { if (string.IsNullOrWhiteSpace(Message.Subject)) { throw new ApplicationException("Email subject is missing"); } if (Message.From == null || string.IsNullOrWhiteSpace(Message.From.Address)) { throw new ApplicationException("From address is missing or in error"); } if (Message.To.Count == 0) { throw new ApplicationException("At least one mail to address is required"); } foreach (MailAddress MA in Message.To) { if (string.IsNullOrWhiteSpace(MA.Address)) { throw new ApplicationException("To mail address address is missing"); } } foreach (MailAddress MA in Message.Cc) { if (string.IsNullOrWhiteSpace(MA.Address)) { throw new ApplicationException("CC mail address address is missing"); } } foreach (MailAddress MA in Message.Bcc) { if (string.IsNullOrWhiteSpace(MA.Address)) { throw new ApplicationException("To mail address address is missing"); } } // open connection OpenConnection(); // send host name and expect request completed 250 SendCommand("EHLO " + Host, SmtpReplyCode.RequestCompleted); // get supported authorization from last reply SupportedAuthorizations(); // plain text authentication method if (AuthMethod == Authentication.PlainText) { if ((SupportedAuth & AuthenticationMethod.PlainText) == 0) { throw new ApplicationException("Email server does not support plain text authorization"); } SendCommand("AUTH LOGIN", SmtpReplyCode.SendUserAndPassword); SendCommand(Convert.ToBase64String(Encoding.ASCII.GetBytes(UserName)), SmtpReplyCode.SendUserAndPassword); SendCommand(Convert.ToBase64String(Encoding.ASCII.GetBytes(UserPassword)), SmtpReplyCode.AuthenticationSuccessful); } // OAuth2 authentication method else { if ((SupportedAuth & AuthenticationMethod.XOAuth2) == 0) { throw new ApplicationException("Email server does not support XOAUTH2 authorization"); } string OAuth2Password = OAuth2.OAuth2Password(); string AuthStr = string.Format("user={0}\u0001auth=Bearer {1}\u0001\u0001", UserName, OAuth2Password); byte[] AuthBin = new byte[AuthStr.Length]; for (int x = 0; x < AuthStr.Length; x++) { AuthBin[x] = (byte)AuthStr[x]; } SendCommand("AUTH XOAUTH2 " + Convert.ToBase64String(AuthBin), SmtpReplyCode.AuthenticationSuccessful); } // from address SendCommand(string.Format("MAIL FROM: <{0}>", Message.From.Address), SmtpReplyCode.RequestCompleted); // send the addresses of all recipients SendRecipientAddress(Message.To); SendRecipientAddress(Message.Cc); SendRecipientAddress(Message.Bcc); // start data block SendCommand("DATA", SmtpReplyCode.StartInput); // send from SendDataLine(string.Format("From: {0} <{1}>", Message.From.DisplayName, Message.From.Address)); // send to list SendAddressTo("To", Message.To); SendAddressTo("Cc", Message.Cc); // send subject SendSubjectLine(Message.Subject); // send date and time SendDataLine(string.Format("Date: {0:r}", DateTime.UtcNow)); // send mime vertion SendDataLine("MIME-Version: 1.0"); // send all data parts Message.RootPart.SendData(Writer); // data terminating dot SendCommand("\r\n.", SmtpReplyCode.RequestCompleted); // terminate connection SendCommand("QUIT", SmtpReplyCode.ClosingChannel); // dispose connection Dispose(); return; } catch (Exception Exp) { try { SendCommand("QUIT", SmtpReplyCode.IgnoreReply); } catch {} // dispose connection Dispose(); throw new Exception(Exp.Message, Exp); } }
/// <summary> /// Send the email message to the mail server /// </summary> /// <param name="message">Email message</param> public void SendMail(SecureSmtpMessage message) { try { if (string.IsNullOrWhiteSpace(message.Subject)) { throw new ApplicationException("Email subject is missing"); } if (string.IsNullOrWhiteSpace(message.From?.Address)) { throw new ApplicationException("From address is missing or in error"); } if (message.To.Count == 0) { throw new ApplicationException("At least one mail to address is required"); } if (message.To.Any(ma => string.IsNullOrWhiteSpace(ma.Address))) { throw new ApplicationException("To mail address address is missing"); } if (message.Cc.Any(ma => string.IsNullOrWhiteSpace(ma.Address))) { throw new ApplicationException("CC mail address address is missing"); } if (message.Bcc.Any(ma => string.IsNullOrWhiteSpace(ma.Address))) { throw new ApplicationException("To mail address address is missing"); } // open connection OpenConnection(); // send host name and expect request completed 250 SendCommand("EHLO " + _host, SmtpReplyCode.RequestCompleted); // get supported authorization from last reply SupportedAuthorizations(); // plain text authentication method if (_authMethod == Authentication.PlainText) { if ((_supportedAuth & AuthenticationMethod.PlainText) == 0) { throw new ApplicationException("Email server does not support plain text authorization"); } SendCommand("AUTH LOGIN", SmtpReplyCode.SendUserAndPassword); SendCommand(Convert.ToBase64String(Encoding.UTF8.GetBytes(_userName)), SmtpReplyCode.SendUserAndPassword); SendCommand(Convert.ToBase64String(Encoding.UTF8.GetBytes(_userPassword)), SmtpReplyCode.AuthenticationSuccessful); } // OAuth2 authentication method else { if ((_supportedAuth & AuthenticationMethod.XoAuth2) == 0) { throw new ApplicationException("Email server does not support XOAUTH2 authorization"); } var oAuth2Password = _oAuth2.OAuth2Password(); var authStr = $"user={_userName}\u0001auth=Bearer {oAuth2Password}\u0001\u0001"; var authBin = new byte[authStr.Length]; for (var x = 0; x < authStr.Length; x++) { authBin[x] = (byte)authStr[x]; } SendCommand("AUTH XOAUTH2 " + Convert.ToBase64String(authBin), SmtpReplyCode.AuthenticationSuccessful); } // from address SendCommand($"MAIL FROM: <{message.From.Address}>", SmtpReplyCode.RequestCompleted); // send the addresses of all recipients SendRecipientAddress(message.To); SendRecipientAddress(message.Cc); SendRecipientAddress(message.Bcc); // start data block SendCommand("DATA", SmtpReplyCode.StartInput); // send from SendDataLine($"From: {message.From.DisplayName} <{message.From.Address}>"); // send to list SendAddressTo("To", message.To); SendAddressTo("Cc", message.Cc); // send subject SendDataLine("Subject:" + message.Subject); // send date and time SendDataLine($"Date: {DateTime.UtcNow:r}"); // send mime vertion SendDataLine("MIME-Version: 1.0"); // send all data parts message.RootPart.SendData(_writer); // data terminating dot SendCommand("\r\n.", SmtpReplyCode.RequestCompleted); // terminate connection SendCommand("QUIT", SmtpReplyCode.ClosingChannel); // dispose connection Dispose(); return; } catch (Exception exp) { try { SendCommand("QUIT", SmtpReplyCode.IgnoreReply); } catch { } // dispose connection Dispose(); throw new Exception(exp.Message, exp); } }