Skip to content
Phil Huhn edited this page May 22, 2019 · 2 revisions

An example: This sample shows how to e-mail a message with an attachment:

using MimeKit;
using MailKit.Net.Smtp;
...
    Extensions.NewMimeMessage().From(_fromAddress).To(_toAddress)
        .Subject(_subject).Body(
            Extensions.TextBody(_message)
                .Attachment(_buffer, "Text.txt", "text/plain;charset=utf-8")
        ).SendAsync();
This assumes SMTP host of localhost, SMTP port number of 25, enable SSL of false and no connection required. Those options do exist, see Send and SendAsync at the bottom.

Table of Contents

Assembly: MimeKit.Extensions

Namespace: MimeKit

Namespace of MimeKit. MimeKit is an external namespace from the MailKit/MimeKit libraries. This appends the MimeKit namespace and extends the MimeMessage and BodyBuilder classes.

Class: Extensions

Fluent extension methods for MimeMessage and BodyBuilder classes in the MimeKit library.


Methods

From(MimeMessage, String)

Set the from address with a string of a single email address.

Parameters
mimeMessage

This MimeMessage.

fromAddress

string of an email address

Return Value

this, MimeMessage to allow fluent design.


From(MimeMessage, String, String)

Set the from address with two strings.

Parameters
mimeMessage

This MimeMessage.

fromAddress

string of an email address

name

display name of the email address

Return Value

this, MimeMessage to allow fluent design.


From(MimeMessage, MailboxAddress)

Set the from address with an email address structure.

Parameters
mimeMessage

This MimeMessage.

fromAddress

A MailboxAddress class, including an email address and the name

Return Value

this, MimeMessage to allow fluent design.


To(MimeMessage, String)

Set a To address with a string of a single email address.

Parameters
mimeMessage

This MimeMessage.

toAddress

an email address

Return Value

this, MimeMessage to allow fluent design.


To(MimeMessage, String, String)

Set a To address with two strings.

Parameters
mimeMessage

This MimeMessage.

toAddress

an email address

name

display name of the email address

Return Value

this, MimeMessage to allow fluent design.


To(MimeMessage, MailboxAddress)

Set a To address with an email address structure.

Parameters
mimeMessage

This MimeMessage.

toAddress

A MailboxAddress class, including an email address and the name

Return Value

this, MimeMessage to allow fluent design.


CC(MimeMessage, String)

Set a carbon copy (cc) email address with a string of a single email address.

Parameters
mimeMessage

This MimeMessage.

ccAddress

an email address

Return Value

this, MimeMessage to allow fluent design.


CC(MimeMessage, String, String)

Set a carbon copy (cc) email address with two strings.

Parameters
mimeMessage

This MimeMessage.

ccAddress

an email address

name

display name of the email address

Return Value

this, MimeMessage to allow fluent design.


CC(MimeMessage, MailboxAddress)

Set a carbon copy (cc) email address with an email address structure.

Parameters
mimeMessage

This MimeMessage.

ccAddress

A MailboxAddress class, including an email address and the name

Return Value

this, MimeMessage to allow fluent design.


BCC(MimeMessage, String)

Set a blind carbon copy (bcc) email address with a string of a single email address.

Parameters
mimeMessage

This MimeMessage.

bccAddress

an email address

Return Value

this, MimeMessage to allow fluent design.


BCC(MimeMessage, String, String)

Set a blind carbon copy (bcc) email address with two strings.

Parameters
mimeMessage

This MimeMessage.

bccAddress

an email address

name

display name of the email address

Return Value

this, MimeMessage to allow fluent design.


BCC(MimeMessage, MailboxAddress)

Set a blind carbon copy (bcc) email address with an email address structure.

Parameters
mimeMessage

This MimeMessage.

bccAddress

A MailboxAddress class, including an email address and the name

Return Value

this, MimeMessage to allow fluent design.


Subject(MimeMessage, String)

Set the title of an email message.

Parameters
mimeMessage

This MimeMessage.

subject

the subject line

Return Value

this, MimeMessage to allow fluent design.


Body(MimeMessage, BodyBuilder)

Set the body of the email message.

Parameters
mimeMessage

This MimeMessage.

bodyBuilder

Email body and any attachments.

Return Value

this, MimeMessage to allow fluent design.


HtmlBody(String)

Set the body of the email message.

Parameters
message

HTML Email message body.

Return Value

A new BodyBuilder to allow fluent design.


TextBody(String)

Set the body of the email message with Html content.

Parameters
message

Text e-mail body

Return Value

A new BodyBuilder to allow fluent design.


Attachment(BodyBuilder, Byte[], String, String)

Add an attachment to the mail message.

For example: This sample shows how to call attachment:

using MimeKit;
using MailKit.Net.Smtp;
...
    MimeMessage _email = (new MimeMessage())
        .From(_fromAddress).To(_toAddress)
        .Subject(_subject).Body(
            Extensions.TextBody(_message)
                .Attachment(_buffer, "Text.txt", "text/plain;charset=utf-8")
        );
Parameters
displayName

name placed on the attachment

buffer

byte buffer containing the attachment

bodyBuilder

this BodyBuilder

mimeType

mime type For example: mime type examples:

  • application/pdf
  • application/ms-excel
  • multipart/form-data
  • text/html
  • text/xml
  • text/plain;charset=utf-8
  • image/png
  • image/jpeg
  • image/gif
Return Value

this, BodyBuilder to allow fluent design.


Send(MimeMessage, String, Int32, Boolean, String, String)

Synchronously send a (this) MimeMessage via the MailKit's SmtpClient.

Parameters
mimeMessage

This MimeMessage.

smtpHost

Host name of the SMTP server or relay.

port

An integer port number.

ssl

Boolean value for enabling SSL.

userName

Connect to the SMTP host with this userName.

passWord

Connect to the SMTP host with this user name's password.

Return Value

this, MimeMessage to allow fluent design.


Send(MimeMessage, String, Int32, Boolean)

Synchronously send a (this) MimeMessage via the MailKit's SmtpClient. Note: This will invoke the following method: Send(this mimeMessage, smtpHost, port, ssl, userName, passWord) This invocation assumes the following parameters:

  • A userName of empty string.
  • A passWord of empty string.
Parameters
mimeMessage

This MimeMessage.

smtpHost

Host name of the SMTP server or relay.

port

An integer port number.

ssl

Boolean value for enabling SSL.

Return Value

this, MimeMessage to allow fluent design.


Send(MimeMessage)

Synchronously send a (this) MimeMessage via the MailKit's SmtpClient. Note: This will invoke the following method: Send(this mimeMessage, smtpHost, port, ssl, userName, passWord) This invocation assumes the following parameters:

  • A smtpHost of localhost.
  • A SMTP port number of 25.
  • An enable SSL of false.
  • A userName of empty string.
  • A passWord of empty string.
Parameters
mimeMessage

This MimeMessage.

Return Value

this, MimeMessage to allow fluent design.


SendAsync(MimeMessage, String, Int32, Boolean, String, String)

Asynchronously send a (this) MimeMessage via the MailKit's SmtpClient.

Parameters
mimeMessage

This MimeMessage.

smtpHost

Host name of the SMTP server or relay.

port

An integer port number.

ssl

Boolean value for enabling SSL.

userName

Connect to the SMTP host with this userName.

passWord

Connect to the SMTP host with this user name's password.

Return Value

this, MimeMessage to allow fluent design.


SendAsync(MimeMessage, String, Int32, Boolean)

Asynchronously send a (this) MimeMessage via the MailKit's SmtpClient. Note: This will invoke the following method: SendAsync(this mimeMessage, smtpHost, port, ssl, userName, passWord) This invocation assumes the following parameters:

  • A userName of empty string.
  • A passWord of empty string.
Parameters
mimeMessage

This MimeMessage.

smtpHost

Host name of the SMTP server or relay.

port

An integer port number.

ssl

Boolean value for enabling SSL.

Return Value

this, MimeMessage to allow fluent design.


SendAsync(MimeMessage)

Asynchronously send a (this) MimeMessage via the MailKit's SmtpClient. Note: This will invoke the following method: SendAsync(this mimeMessage, smtpHost, port, ssl, userName, passWord) This invocation assumes the following parameters:

  • A smtpHost of localhost.
  • A SMTP port number of 25.
  • An enable SSL of false.
  • A userName of empty string.
  • A passWord of empty string.
Parameters
mimeMessage

This MimeMessage.

Return Value

this, MimeMessage to allow fluent design.


Clone this wiki locally