Exemple #1
0
        public void Send(MailMessage message)
        {
            if (this.InCall)
            {
                throw new InvalidOperationException(Resources.SmtpClientInAsyncCall);
            }

            if (message == null)
            {
                throw new ArgumentNullException("message", Resources.SmtpClientMessageNull);
            }

            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"));
            }

            try
            {
                this.InCall = true;
                SendInternal(message);
            }
            catch (Exception exception)
            {
                throw new SmtpException(SR.GetString("SmtpSendMailFailure"), exception);
            }
            finally
            {
                this.InCall = false;
            }
        }