SmtpConsumer Class

Definition

Warning

SmtpClient and its community of varieties are poorly designed, we strongly suggest you employ https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit as a substitute

Permits purposes to ship electronic mail by utilizing the Easy Mail Switch Protocol (SMTP). The SmtpClient sort is now out of date.

C#
public class SmtpClient : IDisposable
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public class SmtpClient : IDisposable
C#
public class SmtpClient
C#
[System.Obsolete("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")]
public class SmtpClient : IDisposable
Inheritance
Object

SmtpClient

Attributes

UnsupportedOSPlatformAttribute


Out of dateAttribute
Implements

IDisposable

Examples

The next code instance demonstrates sending an electronic mail message asynchronously.

C#

utilizing System;
utilizing System.Web;
utilizing System.Web.Mail;
utilizing System.Web.Mime;
utilizing System.Threading;
utilizing System.ComponentModel;
namespace Examples.SmtpExamples.Async
{
    public class SimpleAsynchronousExample
    {
        static bool mailSent = false;
        non-public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the distinctive identifier for this asynchronous operation.
             String token = (string) e.UserState;

            if (e.Cancelled)
            {
                 Console.WriteLine("[{0}] Send canceled.", token);
            }
            if (e.Error != null)
            {
                 Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
            } else
            {
                Console.WriteLine("Message sent.");
            }
            mailSent = true;
        }
        public static void Primary(string[] args)
        {
            // Command-line argument should be the SMTP host.
            SmtpClient shopper = new SmtpClient(args[]);
            // Specify the e-mail sender.
            // Create a mailing deal with that features a UTF8 character
            // within the show title.
            MailAddress from = new MailAddress("jane@contoso.com",
               "Jane " + (char)0xD8+ " Clayton",
            System.Textual content.Encoding.UTF8);
            // Set locations for the e-mail message.
            MailAddress to = new MailAddress("ben@contoso.com");
            // Specify the message content material.
            MailMessage message = new MailMessage(from, to);
            message.Physique = "This is a test email message sent by an application. ";
            // Embody some non-ASCII characters in physique and topic.
            string someArrows = new string(new char[] {'u2190', 'u2191', 'u2192', 'u2193'});
            message.Physique += Atmosphere.NewLine + someArrows;
            message.BodyEncoding =  System.Textual content.Encoding.UTF8;
            message.Topic = "test message 1" + someArrows;
            message.SubjectEncoding = System.Textual content.Encoding.UTF8;
            // Set the strategy that is named again when the ship operation ends.
            shopper.SendCompleted += new
            SendCompletedEventHandler(SendCompletedCallback);
            // The userState could be any object that permits your callback
            // methodology to determine this ship operation.
            // For this instance, the userToken is a string fixed.
            string userState = "test message1";
            shopper.SendAsync(message, userState);
            Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
            string reply = Console.ReadLine();
            // If the person canceled the ship, and mail hasn't been despatched but,
            // then cancel the pending operation.
            if (reply.StartsWith("c") && mailSent == false)
            {
                shopper.SendAsyncCancel();
            }
            // Clear up.
            message.Dispose();
            Console.WriteLine("Goodbye.");
        }
    }
}