internal MailWriter SendMail(MailAddress sender, MailAddressCollection recipients, string deliveryNotify, out SmtpFailedRecipientException exception) { if (sender == null) { throw new ArgumentNullException("sender"); } if (recipients == null) { throw new ArgumentNullException("recipients"); } MailCommand.Send(this.connection, SmtpCommands.Mail, sender.SmtpAddress); this.failedRecipientExceptions.Clear(); exception = null; foreach (MailAddress address in recipients) { string str; if (!RecipientCommand.Send(this.connection, this.connection.DSNEnabled ? (address.SmtpAddress + deliveryNotify) : address.SmtpAddress, out str)) { this.failedRecipientExceptions.Add(new SmtpFailedRecipientException(this.connection.Reader.StatusCode, address.SmtpAddress, str)); } } if (this.failedRecipientExceptions.Count > 0) { if (this.failedRecipientExceptions.Count == 1) { exception = (SmtpFailedRecipientException) this.failedRecipientExceptions[0]; } else { exception = new SmtpFailedRecipientsException(this.failedRecipientExceptions, this.failedRecipientExceptions.Count == recipients.Count); } if (this.failedRecipientExceptions.Count == recipients.Count) { exception.fatal = true; throw exception; } } DataCommand.Send(this.connection); return new MailWriter(this.connection.GetClosableStream()); }
public SmtpFailedRecipientsException(string message, SmtpFailedRecipientException[] innerExceptions) : base(message, ((innerExceptions != null) && (innerExceptions.Length > 0)) ? innerExceptions[0].FailedRecipient : null, ((innerExceptions != null) && (innerExceptions.Length > 0)) ? innerExceptions[0] : null) { if (innerExceptions == null) { throw new ArgumentNullException("innerExceptions"); } this.innerExceptions = (innerExceptions == null) ? new SmtpFailedRecipientException[0] : innerExceptions; }
public SmtpFailedRecipientsException(string message, SmtpFailedRecipientException[] innerExceptions) : base(message, innerExceptions != null && innerExceptions.Length > 0 ? innerExceptions[0].FailedRecipient : null, innerExceptions != null && innerExceptions.Length > 0 ? innerExceptions[0] : null) { if (innerExceptions == null) { throw new ArgumentNullException(nameof(innerExceptions)); } _innerExceptions = innerExceptions == null ? new SmtpFailedRecipientException[0] : innerExceptions; }
internal MailWriter SendMail(MailAddress sender, MailAddressCollection recipients, string deliveryNotify, bool allowUnicode, out SmtpFailedRecipientException exception) { if (sender == null) { throw new ArgumentNullException("sender"); } if (recipients == null) { throw new ArgumentNullException("recipients"); } GlobalLog.Assert(recipients.Count > 0, "SmtpTransport::SendMail()|recepients.Count <= 0"); MailCommand.Send(connection, SmtpCommands.Mail, sender, allowUnicode); failedRecipientExceptions.Clear(); exception = null; string response; foreach (MailAddress address in recipients) { string smtpAddress = address.GetSmtpAddress(allowUnicode); string to = smtpAddress + (connection.DSNEnabled ? deliveryNotify : String.Empty); if (!RecipientCommand.Send(connection, to, out response)) { failedRecipientExceptions.Add( new SmtpFailedRecipientException(connection.Reader.StatusCode, smtpAddress, response)); } } if (failedRecipientExceptions.Count > 0) { if (failedRecipientExceptions.Count == 1) { exception = (SmtpFailedRecipientException) failedRecipientExceptions[0]; } else { exception = new SmtpFailedRecipientsException(failedRecipientExceptions, failedRecipientExceptions.Count == recipients.Count); } if (failedRecipientExceptions.Count == recipients.Count){ exception.fatal = true; throw exception; } } DataCommand.Send(connection); return new MailWriter(connection.GetClosableStream()); }
private void SendMailCallback(IAsyncResult result) { if (GlobalLog.IsEnabled) { GlobalLog.Enter("SmtpClient#" + LoggingHash.HashString(this) + "::SendMailCallback"); } try { _writer = _transport.EndSendMail(result); // If some recipients failed but not others, send the e-mail anyways, but then return the // "Non-fatal" exception reporting the failures. The sync code path does it this way. // Fatal exceptions would have thrown above at transport.EndSendMail(...) SendMailAsyncResult sendResult = (SendMailAsyncResult)result; // Save these and throw them later in SendMessageCallback, after the message has sent. _failedRecipientException = sendResult.GetFailedRecipientException(); } catch (Exception e) { Complete(e, result); if (GlobalLog.IsEnabled) { GlobalLog.Leave("SmtpClient#" + LoggingHash.HashString(this) + "::SendMailCallback"); } return; } try { if (_cancelled) { Complete(null, result); } else { _message.BeginSend(_writer, DeliveryMethod != SmtpDeliveryMethod.Network, ServerSupportsEai, new AsyncCallback(SendMessageCallback), result.AsyncState); } } catch (Exception e) { Complete(e, result); } if (GlobalLog.IsEnabled) { GlobalLog.Leave("SmtpClient#" + LoggingHash.HashString(this) + "::SendMailCallback"); } }
internal MailWriter SendMail(MailAddress sender, MailAddressCollection recipients, string deliveryNotify, bool allowUnicode, out SmtpFailedRecipientException exception) { if (sender == null) { throw new ArgumentNullException(nameof(sender)); } if (recipients == null) { throw new ArgumentNullException(nameof(recipients)); } MailCommand.Send(_connection, SmtpCommands.Mail, sender, allowUnicode); _failedRecipientExceptions.Clear(); exception = null; string response; foreach (MailAddress address in recipients) { string smtpAddress = address.GetSmtpAddress(allowUnicode); string to = smtpAddress + (_connection.DSNEnabled ? deliveryNotify : string.Empty); if (!RecipientCommand.Send(_connection, to, out response)) { _failedRecipientExceptions.Add( new SmtpFailedRecipientException(_connection.Reader.StatusCode, smtpAddress, response)); } } if (_failedRecipientExceptions.Count > 0) { if (_failedRecipientExceptions.Count == 1) { exception = _failedRecipientExceptions[0]; } else { exception = new SmtpFailedRecipientsException(_failedRecipientExceptions, _failedRecipientExceptions.Count == recipients.Count); } if (_failedRecipientExceptions.Count == recipients.Count) { exception.fatal = true; throw exception; } } DataCommand.Send(_connection); return new MailWriter(_connection.GetClosableStream()); }
private void SendCore (MailMessage message) { SmtpResponse status; status = Read (); if (IsError (status)) throw new SmtpException (status.StatusCode, status.Description); // EHLO // FIXME: parse the list of extensions so we don't bother wasting // our time trying commands if they aren't supported. status = SendCommand ("EHLO " + Dns.GetHostName ()); if (IsError (status)) { status = SendCommand ("HELO " + Dns.GetHostName ()); if (IsError (status)) throw new SmtpException (status.StatusCode, status.Description); } else { // Parse ESMTP extensions string extens = status.Description; if (extens != null) ParseExtensions (extens); } if (enableSsl) { InitiateSecureConnection (); ResetExtensions(); writer = new StreamWriter (stream); reader = new StreamReader (stream); status = SendCommand ("EHLO " + Dns.GetHostName ()); if (IsError (status)) { status = SendCommand ("HELO " + Dns.GetHostName ()); if (IsError (status)) throw new SmtpException (status.StatusCode, status.Description); } else { // Parse ESMTP extensions string extens = status.Description; if (extens != null) ParseExtensions (extens); } } if (authMechs != AuthMechs.None) Authenticate (); // The envelope sender: use 'Sender:' in preference of 'From:' MailAddress sender = message.Sender; if (sender == null) sender = message.From; if (sender == null) sender = defaultFrom; // MAIL FROM: status = SendCommand ("MAIL FROM:<" + sender.Address + '>'); if (IsError (status)) { throw new SmtpException (status.StatusCode, status.Description); } // Send RCPT TO: for all recipients List<SmtpFailedRecipientException> sfre = new List<SmtpFailedRecipientException> (); for (int i = 0; i < message.To.Count; i ++) { status = SendCommand ("RCPT TO:<" + message.To [i].Address + '>'); if (IsError (status)) sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.To [i].Address)); } for (int i = 0; i < message.CC.Count; i ++) { status = SendCommand ("RCPT TO:<" + message.CC [i].Address + '>'); if (IsError (status)) sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.CC [i].Address)); } for (int i = 0; i < message.Bcc.Count; i ++) { status = SendCommand ("RCPT TO:<" + message.Bcc [i].Address + '>'); if (IsError (status)) sfre.Add (new SmtpFailedRecipientException (status.StatusCode, message.Bcc [i].Address)); } #if TARGET_JVM // List<T>.ToArray () is not supported if (sfre.Count > 0) { SmtpFailedRecipientException[] xs = new SmtpFailedRecipientException[sfre.Count]; sfre.CopyTo (xs); throw new SmtpFailedRecipientsException ("failed recipients", xs); } #else if (sfre.Count >0) throw new SmtpFailedRecipientsException ("failed recipients", sfre.ToArray ()); #endif // DATA status = SendCommand ("DATA"); if (IsError (status)) throw new SmtpException (status.StatusCode, status.Description); // Send message headers string dt = DateTime.Now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo); // remove ':' from time zone offset (e.g. from "+01:00") dt = dt.Remove (dt.Length - 3, 1); SendHeader (HeaderName.Date, dt); MailAddress from = message.From; if (from == null) from = defaultFrom; SendHeader (HeaderName.From, EncodeAddress (from)); SendHeader (HeaderName.To, EncodeAddresses (message.To)); if (message.CC.Count > 0) SendHeader (HeaderName.Cc, EncodeAddresses (message.CC)); SendHeader (HeaderName.Subject, EncodeSubjectRFC2047 (message)); string v = "normal"; switch (message.Priority){ case MailPriority.Normal: v = "normal"; break; case MailPriority.Low: v = "non-urgent"; break; case MailPriority.High: v = "urgent"; break; } SendHeader ("Priority", v); if (message.Sender != null) SendHeader ("Sender", EncodeAddress (message.Sender)); if (message.ReplyToList.Count > 0) SendHeader ("Reply-To", EncodeAddresses (message.ReplyToList)); #if NET_4_0 foreach (string s in message.Headers.AllKeys) SendHeader (s, ContentType.EncodeSubjectRFC2047 (message.Headers [s], message.HeadersEncoding)); #else foreach (string s in message.Headers.AllKeys) SendHeader (s, message.Headers [s]); #endif AddPriorityHeader (message); boundaryIndex = 0; if (message.Attachments.Count > 0) SendWithAttachments (message); else SendWithoutAttachments (message, null, false); SendDot (); status = Read (); if (IsError (status)) throw new SmtpException (status.StatusCode, status.Description); try { status = SendCommand ("QUIT"); } catch (System.IO.IOException) { // We excuse server for the rude connection closing as a response to QUIT } }
public SmtpFailedRecipientsException(string message, SmtpFailedRecipientException[] innerExceptions) { }
public SmtpFailedRecipientsException (string message, SmtpFailedRecipientException[] innerExceptions) : base (message) { this.innerExceptions = innerExceptions; }
public void Send(MailMessage message) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this, message); } if (_disposed) { throw new ObjectDisposedException(GetType().FullName); } try { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, $"DeliveryMethod={DeliveryMethod}"); NetEventSource.Associate(this, message); } SmtpFailedRecipientException recipientException = null; if (InCall) { throw new InvalidOperationException(SR.net_inasync); } if (message == null) { throw new ArgumentNullException(nameof(message)); } if (DeliveryMethod == SmtpDeliveryMethod.Network) { CheckHostAndPort(); } MailAddressCollection recipients = new MailAddressCollection(); if (message.From == null) { throw new InvalidOperationException(SR.SmtpFromRequired); } if (message.To != null) { foreach (MailAddress address in message.To) { recipients.Add(address); } } if (message.Bcc != null) { foreach (MailAddress address in message.Bcc) { recipients.Add(address); } } if (message.CC != null) { foreach (MailAddress address in message.CC) { recipients.Add(address); } } if (recipients.Count == 0) { throw new InvalidOperationException(SR.SmtpRecipientRequired); } _transport.IdentityRequired = false; // everything completes on the same thread. try { InCall = true; _timedOut = false; _timer = new Timer(new TimerCallback(TimeOutCallback), null, Timeout, Timeout); bool allowUnicode = false; string pickupDirectory = PickupDirectoryLocation; MailWriter writer; switch (DeliveryMethod) { case SmtpDeliveryMethod.PickupDirectoryFromIis: throw new NotSupportedException(SR.SmtpGetIisPickupDirectoryNotSupported); case SmtpDeliveryMethod.SpecifiedPickupDirectory: if (EnableSsl) { throw new SmtpException(SR.SmtpPickupDirectoryDoesnotSupportSsl); } allowUnicode = IsUnicodeSupported(); // Determend by the DeliveryFormat paramiter ValidateUnicodeRequirement(message, recipients, allowUnicode); writer = GetFileMailWriter(pickupDirectory); break; case SmtpDeliveryMethod.Network: default: GetConnection(); // Detected durring GetConnection(), restrictable using the DeliveryFormat paramiter allowUnicode = IsUnicodeSupported(); ValidateUnicodeRequirement(message, recipients, allowUnicode); writer = _transport.SendMail(message.Sender ?? message.From, recipients, message.BuildDeliveryStatusNotificationString(), allowUnicode, out recipientException); break; } _message = message; message.Send(writer, DeliveryMethod != SmtpDeliveryMethod.Network, allowUnicode); writer.Close(); _transport.ReleaseConnection(); //throw if we couldn't send to any of the recipients if (DeliveryMethod == SmtpDeliveryMethod.Network && recipientException != null) { throw recipientException; } } catch (Exception e) { if (NetEventSource.IsEnabled) { NetEventSource.Error(this, e); } if (e is SmtpFailedRecipientException && !((SmtpFailedRecipientException)e).fatal) { throw; } Abort(); if (_timedOut) { throw new SmtpException(SR.net_timeout); } if (e is SecurityException || e is AuthenticationException || e is SmtpException) { throw; } throw new SmtpException(SR.SmtpSendMailFailure, e); } finally { InCall = false; if (_timer != null) { _timer.Dispose(); } } } finally { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } }
private void SendCore(MailMessage message) { SmtpResponse status; status = Read(); if (IsError(status)) { throw new SmtpException(status.StatusCode, status.Description); } // EHLO // FIXME: parse the list of extensions so we don't bother wasting // our time trying commands if they aren't supported. status = SendCommand("EHLO " + Dns.GetHostName()); if (IsError(status)) { status = SendCommand("HELO " + Dns.GetHostName()); if (IsError(status)) { throw new SmtpException(status.StatusCode, status.Description); } } else { // Parse ESMTP extensions string extens = status.Description; if (extens != null) { ParseExtensions(extens); } } if (enableSsl) { InitiateSecureConnection(); ResetExtensions(); writer = new StreamWriter(stream); reader = new StreamReader(stream); status = SendCommand("EHLO " + Dns.GetHostName()); if (IsError(status)) { status = SendCommand("HELO " + Dns.GetHostName()); if (IsError(status)) { throw new SmtpException(status.StatusCode, status.Description); } } else { // Parse ESMTP extensions string extens = status.Description; if (extens != null) { ParseExtensions(extens); } } } if (authMechs != AuthMechs.None) { Authenticate(); } MailAddress from = message.From; if (from == null) { from = defaultFrom; } // MAIL FROM: status = SendCommand("MAIL FROM:<" + from.Address + '>'); if (IsError(status)) { throw new SmtpException(status.StatusCode, status.Description); } // Send RCPT TO: for all recipients List <SmtpFailedRecipientException> sfre = new List <SmtpFailedRecipientException> (); for (int i = 0; i < message.To.Count; i++) { status = SendCommand("RCPT TO:<" + message.To [i].Address + '>'); if (IsError(status)) { sfre.Add(new SmtpFailedRecipientException(status.StatusCode, message.To [i].Address)); } } for (int i = 0; i < message.CC.Count; i++) { status = SendCommand("RCPT TO:<" + message.CC [i].Address + '>'); if (IsError(status)) { sfre.Add(new SmtpFailedRecipientException(status.StatusCode, message.CC [i].Address)); } } for (int i = 0; i < message.Bcc.Count; i++) { status = SendCommand("RCPT TO:<" + message.Bcc [i].Address + '>'); if (IsError(status)) { sfre.Add(new SmtpFailedRecipientException(status.StatusCode, message.Bcc [i].Address)); } } #if TARGET_JVM // List<T>.ToArray () is not supported if (sfre.Count > 0) { SmtpFailedRecipientException[] xs = new SmtpFailedRecipientException[sfre.Count]; sfre.CopyTo(xs); throw new SmtpFailedRecipientsException("failed recipients", xs); } #else if (sfre.Count > 0) { throw new SmtpFailedRecipientsException("failed recipients", sfre.ToArray()); } #endif // DATA status = SendCommand("DATA"); if (IsError(status)) { throw new SmtpException(status.StatusCode, status.Description); } // Send message headers string dt = DateTime.Now.ToString("ddd, dd MMM yyyy HH':'mm':'ss zzz", DateTimeFormatInfo.InvariantInfo); // remove ':' from time zone offset (e.g. from "+01:00") dt = dt.Remove(dt.Length - 3, 1); SendHeader(HeaderName.Date, dt); SendHeader(HeaderName.From, EncodeAddress(from)); SendHeader(HeaderName.To, EncodeAddresses(message.To)); if (message.CC.Count > 0) { SendHeader(HeaderName.Cc, EncodeAddresses(message.CC)); } SendHeader(HeaderName.Subject, EncodeSubjectRFC2047(message)); string v = "normal"; switch (message.Priority) { case MailPriority.Normal: v = "normal"; break; case MailPriority.Low: v = "non-urgent"; break; case MailPriority.High: v = "urgent"; break; } SendHeader("Priority", v); if (message.Sender != null) { SendHeader("Sender", EncodeAddress(message.Sender)); } if (message.ReplyTo != null) { SendHeader("ReplyTo", EncodeAddress(message.ReplyTo)); } foreach (string s in message.Headers.AllKeys) { SendHeader(s, message.Headers [s]); } AddPriorityHeader(message); boundaryIndex = 0; if (message.Attachments.Count > 0) { SendWithAttachments(message); } else { SendWithoutAttachments(message, null, false); } SendDot(); status = Read(); if (IsError(status)) { throw new SmtpException(status.StatusCode, status.Description); } try { status = SendCommand("QUIT"); } catch (System.IO.IOException) { // We excuse server for the rude connection closing as a response to QUIT } }
public SmtpFailedRecipientsException(string message, Exception innerException) : base(message, innerException) { SmtpFailedRecipientException exception = innerException as SmtpFailedRecipientException; this.innerExceptions = (exception == null) ? new SmtpFailedRecipientException[0] : new SmtpFailedRecipientException[] { exception }; }
public SmtpFailedRecipientsException(string message, Exception innerException) : base(message, innerException) { SmtpFailedRecipientException smtpException = innerException as SmtpFailedRecipientException; _innerExceptions = smtpException == null ? new SmtpFailedRecipientException[0] : new SmtpFailedRecipientException[] { smtpException }; }
private void SendMailCallback(IAsyncResult result) { try { this.writer = this.transport.EndSendMail(result); this.failedRecipientException = ((SendMailAsyncResult) result).GetFailedRecipientException(); } catch (Exception ex) { this.Complete(ex, result); return; } try { if (this.cancelled) this.Complete((Exception) null, result); else this.message.BeginSend((BaseWriter) this.writer, this.DeliveryMethod != SmtpDeliveryMethod.Network, this.ServerSupportsEai, new AsyncCallback(this.SendMessageCallback), result.AsyncState); } catch (Exception ex) { this.Complete(ex, result); } }
public void Send(MailMessage message) { if (Logging.On) { Logging.Enter(Logging.Web, this, "Send", message); } if (this.disposed) { throw new ObjectDisposedException(base.GetType().FullName); } try { if (Logging.On) { Logging.PrintInfo(Logging.Web, this, "Send", "DeliveryMethod=" + this.DeliveryMethod.ToString()); } if (Logging.On) { Logging.Associate(Logging.Web, this, message); } SmtpFailedRecipientException exception = null; if (this.InCall) { throw new InvalidOperationException(SR.GetString("net_inasync")); } if (message == null) { throw new ArgumentNullException("message"); } if (this.DeliveryMethod == SmtpDeliveryMethod.Network) { this.CheckHostAndPort(); } MailAddressCollection recipients = new MailAddressCollection(); if (message.From == null) { throw new InvalidOperationException(SR.GetString("SmtpFromRequired")); } if (message.To != null) { foreach (MailAddress address in message.To) { recipients.Add(address); } } if (message.Bcc != null) { foreach (MailAddress address2 in message.Bcc) { recipients.Add(address2); } } if (message.CC != null) { foreach (MailAddress address3 in message.CC) { recipients.Add(address3); } } if (recipients.Count == 0) { throw new InvalidOperationException(SR.GetString("SmtpRecipientRequired")); } this.transport.IdentityRequired = false; try { this.InCall = true; this.timedOut = false; this.timer = new System.Threading.Timer(new TimerCallback(this.TimeOutCallback), null, this.Timeout, this.Timeout); switch (this.DeliveryMethod) { case SmtpDeliveryMethod.SpecifiedPickupDirectory: if (this.EnableSsl) { throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl")); } break; case SmtpDeliveryMethod.PickupDirectoryFromIis: if (this.EnableSsl) { throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl")); } goto Label_0235; default: goto Label_0244; } MailWriter fileMailWriter = this.GetFileMailWriter(this.PickupDirectoryLocation); goto Label_0276; Label_0235: fileMailWriter = this.GetFileMailWriter(IisPickupDirectory.GetPickupDirectory()); goto Label_0276; Label_0244: this.GetConnection(); fileMailWriter = this.transport.SendMail((message.Sender != null) ? message.Sender : message.From, recipients, message.BuildDeliveryStatusNotificationString(), out exception); Label_0276: this.message = message; message.Send(fileMailWriter, this.DeliveryMethod != SmtpDeliveryMethod.Network); fileMailWriter.Close(); this.transport.ReleaseConnection(); if ((this.DeliveryMethod == SmtpDeliveryMethod.Network) && (exception != null)) { throw exception; } } catch (Exception exception2) { if (Logging.On) { Logging.Exception(Logging.Web, this, "Send", exception2); } if ((exception2 is SmtpFailedRecipientException) && !((SmtpFailedRecipientException)exception2).fatal) { throw; } this.Abort(); if (this.timedOut) { throw new SmtpException(SR.GetString("net_timeout")); } if (((exception2 is SecurityException) || (exception2 is AuthenticationException)) || (exception2 is SmtpException)) { throw; } throw new SmtpException(SR.GetString("SmtpSendMailFailure"), exception2); } finally { this.InCall = false; if (this.timer != null) { this.timer.Dispose(); } } } finally { if (Logging.On) { Logging.Exit(Logging.Web, this, "Send", (string)null); } } }