Table of Contents

Class EmailRequestBuilder

Namespace
Mailtrap.Emails.Requests
Assembly
Mailtrap.dll

A set of helper methods to streamline EmailRequest instance construction using fluent style.

public static class EmailRequestBuilder
Inheritance
EmailRequestBuilder

Methods

Attach<T>(T, params Attachment[])

Adds provided attachments to the Attachments collection of the request.

public static T Attach<T>(this T request, params Attachment[] attachments) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
attachments Attachment[]

One or more Attachment objects to add to the request's Attachments collection.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Duplicates can be added by calling this method multiple times with the same Attachment object.

Exceptions

ArgumentNullException

When request or attachments is null.

Attach<T>(T, string, string, DispositionType?, string?, string?)

Adds provided attachment to the Attachments collection of the request.

public static T Attach<T>(this T request, string content, string fileName, DispositionType? disposition = null, string? mimeType = null, string? contentId = null) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
content string

The Base64 encoded content of the attachment.

Required. Must be non-empty string.

fileName string

Attachment file name.

Required. Must be non-empty string.

disposition DispositionType

The attachment's content disposition, specifying how you would like the attachment to be displayed.

Optional. Defaults to Attachment if not specified explicitly.

mimeType string

Attachment MIME type identifier (e.g. "text/plain", "application/pdf", etc.)

Optional.

contentId string

The attachment's content ID.

Optional.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Duplicates can be added by calling this method multiple times with the same parameters.

Exceptions

ArgumentNullException

When request is null.
When content is null or Empty.
When fileName is null or Empty.

Category<T>(T, string?)

Sets provided category to the request.

public static T Category<T>(this T request, string? category) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
category string

Value to initialize request's Category property.

Should be less or equal to 255 characters.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Subsequent calls will override value that was set before (last wins).

Value must remain null if TemplateId is used to create email from template.

Exceptions

ArgumentNullException

When request is null.

CustomVariable<T>(T, params KeyValuePair<string, string>[])

Adds provided variables to the CustomVariables collection of the request.

public static T CustomVariable<T>(this T request, params KeyValuePair<string, string>[] variables) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
variables KeyValuePair<string, string>[]

One or more key/value pairs to add to the request's CustomVariables collection.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Any existing variables with the same keys will be overridden.

Exceptions

ArgumentNullException

When request or variables is null.

CustomVariable<T>(T, string, string)

Adds provided custom variable to the CustomVariables collection of the request.

public static T CustomVariable<T>(this T request, string key, string value) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
key string

Variable key to add.

value string

Variable value to add.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Any existing variable with the same key will be overridden.

Exceptions

ArgumentNullException

When request is null.
When key is null or Empty.

From<T>(T, EmailAddress)

Sets provided sender to the request.

public static T From<T>(this T request, EmailAddress sender) where T : EmailRequest

Parameters

request T

EmailRequest instance to update.

sender EmailAddress

EmailAddress object to initialize request's From property.

Returns

T

Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Subsequent calls will override value that was set before (last wins).

Exceptions

ArgumentNullException

When request or sender is null.

From<T>(T, string, string?)

Sets provided email and displayName as sender for the request.

public static T From<T>(this T request, string email, string? displayName = null) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
email string

Sender's email address.

Required. Must be valid email address.

displayName string

Optional sender's display name.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Subsequent calls will override value that was set before (last wins).

Exceptions

ArgumentNullException

When request is null.
When email is null or Empty.

Header<T>(T, params KeyValuePair<string, string>[])

Adds provided headers to the Headers collection of the request.

public static T Header<T>(this T request, params KeyValuePair<string, string>[] headers) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
headers KeyValuePair<string, string>[]

One or more key/value pairs to add to the request's Headers collection.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Any existing headers with the same keys will be overridden.

Exceptions

ArgumentNullException

When request or headers is null.

Header<T>(T, string, string)

Adds provided header to the Headers collection of the request.

public static T Header<T>(this T request, string key, string value) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
key string

Header key to add.

value string

Header value to add.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Any existing header with the same key will be overridden.

Exceptions

ArgumentNullException

When request is null.
When key is null or Empty.

Html<T>(T, string?)

Sets provided html to the request.

public static T Html<T>(this T request, string? html) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
html string

Value to initialize request's HtmlBody property.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

It is a caller responsibility to ensure that html contains a valid, well-formed HTML markup and is sanitized properly.

Subsequent calls will override value that was set before (last wins).

Value must remain null if TemplateId is used to create email from template.

Exceptions

ArgumentNullException

When request is null.

ReplyTo<T>(T, EmailAddress?)

Sets provided replyTo address in the request.

public static T ReplyTo<T>(this T request, EmailAddress? replyTo) where T : EmailRequest

Parameters

request T

EmailRequest instance to update.

replyTo EmailAddress

EmailAddress object to initialize request's ReplyTo property.

Returns

T

Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Subsequent calls will override value that was set before (last wins).

Exceptions

ArgumentNullException

When request is null.

ReplyTo<T>(T, string, string?)

Sets provided email and displayName as 'Reply To' address in the request.

public static T ReplyTo<T>(this T request, string email, string? displayName = null) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
email string

'Reply To' email address.

Required. Must be valid email address.

displayName string

Optional 'Reply To' display name.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Subsequent calls will override value that was set before (last wins).

Exceptions

ArgumentNullException

When request is null.
When email is null or Empty.

Subject<T>(T, string)

Sets provided subject to the request.

public static T Subject<T>(this T request, string subject) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
subject string

Value to initialize request's Subject property.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Subsequent calls will override value that was set before (last wins).

Value must remain null if TemplateId is used to create email from template.

Exceptions

ArgumentNullException

When request is null.
When subject is null or Empty.

TemplateVariables<T>(T, object?)

Sets provided templateVariables to the request.

public static T TemplateVariables<T>(this T request, object? templateVariables) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
templateVariables object

Value containing object to initialize request's TemplateVariables property.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Subsequent calls will override value that was set before (last wins).

Will be used only in case template is set.

Exceptions

ArgumentNullException

When request is null.

Template<T>(T, string)

Sets provided templateId to the request.

public static T Template<T>(this T request, string templateId) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
templateId string

Value containing UUID of the template to initialize request's TemplateId property.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Subsequent calls will override value that was set before (last wins).

When TemplateId is set, then Subject, TextBody, HtmlBody and Category properties are forbidden and must be set to null.

Exceptions

ArgumentNullException

When request is null.
When templateId is null or Empty.

Text<T>(T, string?)

Sets provided text to the request.

public static T Text<T>(this T request, string? text) where T : EmailRequest

Parameters

request T
EmailRequest instance to update.
text string

Value to initialize request's TextBody property.

Returns

T
Updated EmailRequest instance so subsequent calls can be chained.

Type Parameters

T

Remarks

Subsequent calls will override value that was set before (last wins).

Value must remain null if TemplateId is used to create email from template.

Exceptions

ArgumentNullException

When request is null.