Unosquare
    Show / Hide Table of Contents

    Class SmtpClient

    Represents a basic SMTP client that is capable of submitting messages to an SMTP server.

    Inheritance
    Object
    SmtpClient
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Swan.Net.Smtp
    Syntax
    public class SmtpClient
    Examples

    The following code explains how to send a simple e-mail.

    using System.Net.Mail;
    
    class Example
    {
        static void Main()
        {
            // create a new smtp client using google's smtp server
            var client = new Swan.Net.Smtp.SmtpClient("smtp.gmail.com", 587);
    
            // send an email 
            client.SendMailAsync(
            new MailMessage("sender@test.com", "recipient@test.cm", "Subject", "Body"));
        }
    }

    The following code demonstrates how to sent an e-mail using a SmtpSessionState:

    using Swan.Net.Smtp;
    
    class Example
    {
        static void Main()
        {
            // create a new smtp client using google's smtp server
            var client = new SmtpClient("smtp.gmail.com", 587);
    
            // create a new session state with a sender address 
            var session = new SmtpSessionState { SenderAddress = "sender@test.com" };
    
            // add a recipient
            session.Recipients.Add("recipient@test.cm");
    
            // send
            client.SendMailAsync(session);
        }
    }

    The following code shows how to send an e-mail with an attachment using MimeKit:

    using MimeKit;
    using Swan.Net.Smtp;
    
    class Example
    {
        static void Main()
        {
            // create a new smtp client using google's smtp server
            var client = new SmtpClient("smtp.gmail.com", 587);
    
            // create a new session state with a sender address 
            var session = new SmtpSessionState { SenderAddress = "sender@test.com" };
    
            // add a recipient
            session.Recipients.Add("recipient@test.cm");
    
            // load a file as an attachment
            var attachment = new MimePart("image", "gif")
            {
                Content = new 
                    MimeContent(File.OpenRead("meme.gif"), ContentEncoding.Default),
                ContentDisposition = 
                    new ContentDisposition(ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName = Path.GetFileName("meme.gif")
            };
    
            // send
            client.SendMailAsync(session);
        }
    }

    Constructors

    SmtpClient(String, Int32)

    Initializes a new instance of the SmtpClient class.

    Declaration
    public SmtpClient(string host, int port)
    Parameters
    Type Name Description
    String host

    The host.

    Int32 port

    The port.

    Exceptions
    Type Condition
    ArgumentNullException

    host.

    Properties

    ClientHostname

    Gets or sets the name of the client that gets announced to the server.

    Declaration
    public string ClientHostname { get; set; }
    Property Value
    Type Description
    String

    The client hostname.

    Credentials

    Gets or sets the credentials. No credentials will be used if set to null.

    Declaration
    public NetworkCredential? Credentials { get; set; }
    Property Value
    Type Description
    Nullable<NetworkCredential>

    The credentials.

    EnableSsl

    Gets or sets a value indicating whether the SSL is enabled. If set to false, communication between client and server will not be secured.

    Declaration
    public bool EnableSsl { get; set; }
    Property Value
    Type Description
    Boolean

    true if [enable SSL]; otherwise, false.

    Host

    Gets the host.

    Declaration
    public string Host { get; }
    Property Value
    Type Description
    String

    The host.

    Port

    Gets the port.

    Declaration
    public int Port { get; }
    Property Value
    Type Description
    Int32

    The port.

    Methods

    SendMailAsync(MailMessage, String, Nullable<RemoteCertificateValidationCallback>, CancellationToken)

    Sends an email message asynchronously.

    Declaration
    public Task SendMailAsync(MailMessage message, string sessionId = null, RemoteCertificateValidationCallback? callback = default(RemoteCertificateValidationCallback? ), CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    MailMessage message

    The message.

    String sessionId

    The session identifier.

    Nullable<RemoteCertificateValidationCallback> callback

    The callback.

    CancellationToken cancellationToken

    The cancellation token.

    Returns
    Type Description
    Task

    A task that represents the asynchronous of send email operation.

    Exceptions
    Type Condition
    ArgumentNullException

    message.

    SendMailAsync(SmtpSessionState, String, Nullable<RemoteCertificateValidationCallback>, CancellationToken)

    Sends an email message using a session state object. Credentials, Enable SSL and Client Hostname are NOT taken from the state object but rather from the properties of this class.

    Declaration
    public Task SendMailAsync(SmtpSessionState sessionState, string sessionId = null, RemoteCertificateValidationCallback? callback = default(RemoteCertificateValidationCallback? ), CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    SmtpSessionState sessionState

    The state.

    String sessionId

    The session identifier.

    Nullable<RemoteCertificateValidationCallback> callback

    The callback.

    CancellationToken cancellationToken

    The cancellation token.

    Returns
    Type Description
    Task

    A task that represents the asynchronous of send email operation.

    Exceptions
    Type Condition
    ArgumentNullException

    sessionState.

    SendMailAsync(IEnumerable<SmtpSessionState>, String, Nullable<RemoteCertificateValidationCallback>, CancellationToken)

    Sends an array of email messages using a session state object. Credentials, Enable SSL and Client Hostname are NOT taken from the state object but rather from the properties of this class.

    Declaration
    public Task SendMailAsync(IEnumerable<SmtpSessionState> sessionStates, string sessionId = null, RemoteCertificateValidationCallback? callback = default(RemoteCertificateValidationCallback? ), CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    IEnumerable<SmtpSessionState> sessionStates

    The session states.

    String sessionId

    The session identifier.

    Nullable<RemoteCertificateValidationCallback> callback

    The callback.

    CancellationToken cancellationToken

    The cancellation token.

    Returns
    Type Description
    Task

    A task that represents the asynchronous of send email operation.

    Exceptions
    Type Condition
    ArgumentNullException

    sessionStates.

    SecurityException

    Could not upgrade the channel to SSL.

    Comments

    Back to top Copyright © 2017-2019 Unosquare