private void ConnectCallback(IAsyncResult result) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } try { _transport.EndGetConnection(result); if (_cancelled) { Complete(null, result); } else { // Detected durring Begin/EndGetConnection, restrictable using DeliveryFormat bool allowUnicode = IsUnicodeSupported(); ValidateUnicodeRequirement(_message, _recipients, allowUnicode); _transport.BeginSendMail(_message.Sender ?? _message.From, _recipients, _message.BuildDeliveryStatusNotificationString(), allowUnicode, new AsyncCallback(SendMailCallback), result.AsyncState); } } catch (Exception e) { Complete(e, result); } finally { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } }
private void ConnectCallback(IAsyncResult result) { if (GlobalLog.IsEnabled) { GlobalLog.Enter("SmtpClient#" + LoggingHash.HashString(this) + "::ConnectCallback"); } try { _transport.EndGetConnection(result); if (_cancelled) { Complete(null, result); } else { // Detected durring Begin/EndGetConnection, restrictable using DeliveryFormat bool allowUnicode = IsUnicodeSupported(); ValidateUnicodeRequirement(_message, _recipients, allowUnicode); _transport.BeginSendMail(_message.Sender ?? _message.From, _recipients, _message.BuildDeliveryStatusNotificationString(), allowUnicode, new AsyncCallback(SendMailCallback), result.AsyncState); } } catch (Exception e) { Complete(e, result); } if (GlobalLog.IsEnabled) { GlobalLog.Leave("SmtpClient#" + LoggingHash.HashString(this) + "::ConnectCallback"); } }
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); } } }
public void Send(MailMessage message) { if (NetEventSource.Log.IsEnabled()) { NetEventSource.Enter(NetEventSource.ComponentType.Web, this, nameof(Send), message); } if (_disposed) { throw new ObjectDisposedException(GetType().FullName); } try { if (MailEventSource.Log.IsEnabled()) { MailEventSource.Log.Send(nameof(DeliveryMethod), DeliveryMethod.ToString()); MailEventSource.Log.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.Log.IsEnabled()) NetEventSource.Exception(NetEventSource.ComponentType.Web, this, "Send", 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.Log.IsEnabled()) NetEventSource.Exit(NetEventSource.ComponentType.Web, this, "Send", null); } }
public void Send(MailMessage message) { ObjectDisposedException.ThrowIf(_disposed, this); if (NetEventSource.Log.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(); // Determined by the DeliveryFormat parameter ValidateUnicodeRequirement(message, recipients, allowUnicode); writer = GetFileMailWriter(pickupDirectory); break; case SmtpDeliveryMethod.Network: default: GetConnection(); // Detected during GetConnection(), restrictable using the DeliveryFormat parameter 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(); //throw if we couldn't send to any of the recipients if (DeliveryMethod == SmtpDeliveryMethod.Network && recipientException != null) { throw recipientException; } } catch (Exception e) { if (NetEventSource.Log.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(); } } }
public void Send(MailMessage message) { if (Logging.On) { Logging.Enter(Logging.Web, this, "Send", message); } if (disposed) { throw new ObjectDisposedException(this.GetType().FullName); } try { if (Logging.On) { Logging.PrintInfo(Logging.Web, this, "Send", "DeliveryMethod=" + DeliveryMethod.ToString()); } if (Logging.On) { Logging.Associate(Logging.Web, this, message); } SmtpFailedRecipientException recipientException = null; if (InCall) { throw new InvalidOperationException(SR.GetString(SR.net_inasync)); } if (message == null) { throw new ArgumentNullException("message"); } if (DeliveryMethod == SmtpDeliveryMethod.Network) { CheckHostAndPort(); } MailAddressCollection recipients = new MailAddressCollection(); if (message.From == null) { throw new InvalidOperationException(SR.GetString(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.GetString(SR.SmtpRecipientRequired)); } transport.IdentityRequired = false; // everything completes on the same thread. try { InCall = true; timedOut = false; timer = new Timer(new TimerCallback(this.TimeOutCallback), null, Timeout, Timeout); bool allowUnicode = false; string pickupDirectory = PickupDirectoryLocation; MailWriter writer; switch (DeliveryMethod) { #if !FEATURE_PAL case SmtpDeliveryMethod.PickupDirectoryFromIis: pickupDirectory = IisPickupDirectory.GetPickupDirectory(); goto case SmtpDeliveryMethod.SpecifiedPickupDirectory; #endif // !FEATURE_PAL case SmtpDeliveryMethod.SpecifiedPickupDirectory: if (EnableSsl) { throw new SmtpException(SR.GetString(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; } this.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 (Logging.On) { Logging.Exception(Logging.Web, this, "Send", e); } if (e is SmtpFailedRecipientException && !((SmtpFailedRecipientException)e).fatal) { throw; } Abort(); if (timedOut) { throw new SmtpException(SR.GetString(SR.net_timeout)); } if (e is SecurityException || e is AuthenticationException || e is SmtpException) { throw; } throw new SmtpException(SR.GetString(SR.SmtpSendMailFailure), e); } finally { InCall = false; if (timer != null) { timer.Dispose(); } } } finally { if (Logging.On) { Logging.Exit(Logging.Web, this, "Send", null); } } }
public void Send(MailMessage message) { if (Logging.On) Logging.Enter(Logging.Web, (object) this, "Send", (object) message); if (this.disposed) throw new ObjectDisposedException(this.GetType().FullName); try { if (Logging.On) Logging.PrintInfo(Logging.Web, (object) this, "Send", "DeliveryMethod=" + ((object) this.DeliveryMethod).ToString()); if (Logging.On) Logging.Associate(Logging.Web, (object) this, (object) message); SmtpFailedRecipientException exception = (SmtpFailedRecipientException) 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 mailAddress in (Collection<MailAddress>) message.To) ((Collection<MailAddress>) recipients).Add(mailAddress); } if (message.Bcc != null) { foreach (MailAddress mailAddress in (Collection<MailAddress>) message.Bcc) ((Collection<MailAddress>) recipients).Add(mailAddress); } if (message.CC != null) { foreach (MailAddress mailAddress in (Collection<MailAddress>) message.CC) ((Collection<MailAddress>) recipients).Add(mailAddress); } 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), (object) null, this.Timeout, this.Timeout); string pickupDirectory = this.PickupDirectoryLocation; bool allowUnicode; MailWriter mailWriter; switch (this.DeliveryMethod) { case SmtpDeliveryMethod.SpecifiedPickupDirectory: if (this.EnableSsl) throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl")); allowUnicode = this.IsUnicodeSupported(); this.ValidateUnicodeRequirement(message, recipients, allowUnicode); mailWriter = this.GetFileMailWriter(pickupDirectory); break; case SmtpDeliveryMethod.PickupDirectoryFromIis: pickupDirectory = IisPickupDirectory.GetPickupDirectory(); goto case 1; default: this.GetConnection(); allowUnicode = this.IsUnicodeSupported(); this.ValidateUnicodeRequirement(message, recipients, allowUnicode); mailWriter = this.transport.SendMail(message.Sender ?? message.From, recipients, message.BuildDeliveryStatusNotificationString(), allowUnicode, out exception); break; } this.message = message; message.Send((BaseWriter) mailWriter, this.DeliveryMethod != SmtpDeliveryMethod.Network, allowUnicode); mailWriter.Close(); this.transport.ReleaseConnection(); if (this.DeliveryMethod == SmtpDeliveryMethod.Network && exception != null) throw exception; } catch (Exception ex) { if (Logging.On) Logging.Exception(Logging.Web, (object) this, "Send", ex); if (ex is SmtpFailedRecipientException && !((SmtpFailedRecipientException) ex).fatal) { throw; } else { this.Abort(); if (this.timedOut) throw new SmtpException(SR.GetString("net_timeout")); if (!(ex is SecurityException) && !(ex is AuthenticationException) && !(ex is SmtpException)) throw new SmtpException(SR.GetString("SmtpSendMailFailure"), ex); throw; } } finally { this.InCall = false; if (this.timer != null) this.timer.Dispose(); } } finally { if (Logging.On) Logging.Exit(Logging.Web, (object) this, "Send", (string) null); } }
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); } } }