EmailProvider.

SendEmail(string, string, IEnumerable<MailAddress>, string, string, string, IEnumerable<Attachment>, EmailSettings) Method

Summary

Sends an email via SMTP, based on the provided settings.
Assembly
Cake.Email.dll
Namespace
Cake.Email
Containing Type
EmailProvider

Syntax

public EmailResult SendEmail(string senderName, string senderAddress, IEnumerable<MailAddress> recipients, string subject, string htmlContent, string textContent, IEnumerable<Attachment> attachments, EmailSettings settings)

Examples

 using Cake.Email.Common;

 var smtpHost = "... your smtp host ...";
 var port = 1234;
 var enableSsl = true;
 var username = "... your username ...";
 var password = "... your password ...";
 var attachments = new[]
 {
     Attachment.FromLocalFile("C:\\temp\\MyFile.txt"),
     Attachment.FromLocalFile("C:\\temp\\MySpreadsheet.xls"),
     Attachment.FromLocalFile("C:\\temp\\MyFile.pdf")
 };
 try
 {
     var result = Email.SendEmail(
         senderName: "Bob Smith",
         senderAddress: "[email protected]",
         recipients: new[]
         {
             new MailAddress("[email protected]", "Jane Doe"),
             new MailAddress("[email protected]", "Bob Smith"),
         },
         subject: "This is a test",
         htmlContent: "This is a test",
         textContent: "This is the text only version of this email",
         attachments: attachments,
         settings: new EmailSettings
         {
             SmtpHost = smtpHost,
             Port = port,
             EnableSsl = enableSsl,
             Username = username,
             Password = password
         }
     );
     if (result.Ok)
     {
         Information("Email succcessfully sent");
     }
     else
     {
         Error("Failed to send email: {0}", result.Error);
     }
 }
 catch(Exception ex)
 {
     Error("{0}", ex);
 }

Parameters

Name Type Description
senderName string The name of the person sending the email.
senderAddress string The email address of the person sending the email.
recipients IEnumerable<MailAddress> An enumeration of recipients who will receive the email.
subject string The subject line of the email.
htmlContent string The HTML content of the email.
textContent string The text content of the email.
attachments IEnumerable<Attachment> Attachments to send with the email.
settings EmailSettings The settings to be used when sending the email.

Return Value

Type Description
EmailResult An instance of EmailResult indicating success/failure.