MailKit.Net.Smtp.SmtpClient.Send C# (CSharp) Method

Send() public method

Send the specified message.

Sends the specified message.

The sender address is determined by checking the following message headers (in order of precedence): Resent-Sender, Resent-From, Sender, and From.

If either the Resent-Sender or Resent-From addresses are present, the recipients are collected from the Resent-To, Resent-Cc, and Resent-Bcc headers, otherwise the To, Cc, and Bcc headers are used.

/// is null. /// -or- /// is null. /// /// The has been disposed. /// /// The is not connected. /// /// Authentication is required before sending a message. /// /// A sender has not been specified. /// -or- /// No recipients have been specified. /// /// Internationalized formatting was requested but is not supported by the server. /// /// The operation has been canceled. /// /// An I/O error occurred. /// /// The SMTP command failed. /// /// An SMTP protocol exception occurred. ///
public Send ( MimeKit.FormatOptions options, MimeMessage message, CancellationToken cancellationToken = default(CancellationToken), ITransferProgress progress = null ) : void
options MimeKit.FormatOptions The formatting options.
message MimeKit.MimeMessage The message.
cancellationToken System.Threading.CancellationToken The cancellation token.
progress ITransferProgress The progress reporting mechanism.
return void
		public override void Send (FormatOptions options, MimeMessage message, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null)
		{
			if (options == null)
				throw new ArgumentNullException (nameof (options));

			if (message == null)
				throw new ArgumentNullException (nameof (message));

			var recipients = GetMessageRecipients (message);
			var sender = GetMessageSender (message);

			if (sender == null)
				throw new InvalidOperationException ("No sender has been specified.");

			if (recipients.Count == 0)
				throw new InvalidOperationException ("No recipients have been specified.");

			Send (options, message, sender, recipients, cancellationToken, progress);
		}

Same methods

SmtpClient::Send ( MimeKit.FormatOptions options, MimeMessage message, MimeKit.MailboxAddress sender, IEnumerable recipients, CancellationToken cancellationToken = default(CancellationToken), ITransferProgress progress = null ) : void
SmtpClient::Send ( MimeKit.FormatOptions options, MimeMessage message, MimeKit.MailboxAddress sender, IList recipients, CancellationToken cancellationToken, ITransferProgress progress ) : void

Usage Example

Example #1
3
        public void Send(EmailDependencies email)
        {
            try
            {
                var message = new MimeMessage();
                message.From.Add(new MailboxAddress(email.FromName, email.FromAddress));

                message.To.Add(new MailboxAddress(email.ToName, email.ToAddress));
                message.To.Add(new MailboxAddress(email.FromName, email.FromAddress));

                message.Subject = email.Title;
                message.Body = new TextPart("html") { Text = email.content };

                using (var client = new SmtpClient())
                {
                    client.Connect("mail.bizmonger.net", 587, false);

                    // Note: since we don't have an OAuth2 token, disable
                    // the XOAUTH2 authentication mechanism.
                    client.AuthenticationMechanisms.Remove("XOAUTH2");

                    // Note: only needed if the SMTP server requires authentication
                    client.Authenticate(Configuration.ServerEmail, Configuration.Password);

                    client.Send(message);
                    client.Disconnect(true);
                }
            }
            catch (Exception ex)
            {
                var errorMessage = ex.GetBaseException().Message;
                Debug.WriteLine(errorMessage);
            }
        }
All Usage Examples Of MailKit.Net.Smtp.SmtpClient::Send