System.Net.Mail.MailMessage.BuildDeliveryStatusNotificationString C# (CSharp) Method

BuildDeliveryStatusNotificationString() private method

private BuildDeliveryStatusNotificationString ( ) : string
return string
        internal string BuildDeliveryStatusNotificationString()
        {
            if (_deliveryStatusNotification != DeliveryNotificationOptions.None)
            {
                StringBuilder s = new StringBuilder(" NOTIFY=");

                bool oneSet = false;

                //none
                if (_deliveryStatusNotification == DeliveryNotificationOptions.Never)
                {
                    s.Append("NEVER");
                    return s.ToString();
                }

                if ((((int)_deliveryStatusNotification) & (int)DeliveryNotificationOptions.OnSuccess) > 0)
                {
                    s.Append("SUCCESS");
                    oneSet = true;
                }
                if ((((int)_deliveryStatusNotification) & (int)DeliveryNotificationOptions.OnFailure) > 0)
                {
                    if (oneSet)
                    {
                        s.Append(",");
                    }
                    s.Append("FAILURE");
                    oneSet = true;
                }
                if ((((int)_deliveryStatusNotification) & (int)DeliveryNotificationOptions.Delay) > 0)
                {
                    if (oneSet)
                    {
                        s.Append(",");
                    }
                    s.Append("DELAY");
                }
                return s.ToString();
            }
            return string.Empty;
        }
    }

Usage Example

Example #1
0
        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);
            }
        }
All Usage Examples Of System.Net.Mail.MailMessage::BuildDeliveryStatusNotificationString