SendMail() private method

private SendMail ( System.Net.Mail.MailAddress sender, MailAddressCollection recipients, string deliveryNotify, bool allowUnicode, System.Net.Mail.SmtpFailedRecipientException &exception ) : System.Net.Mail.MailWriter
sender System.Net.Mail.MailAddress
recipients MailAddressCollection
deliveryNotify string
allowUnicode bool
exception System.Net.Mail.SmtpFailedRecipientException
return System.Net.Mail.MailWriter
Example #1
0
        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();
                }
            }
        }
Example #2
0
        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);
                }
            }
        }