Tuesday, September 3, 2013

Sending email from your app

Let’s say that your app needs to have the ability to send an email, “New order has arrived” kind of email.
Let me introduce you “Fluent Email” – a nice library for making your life easy when sending emails for your app.
Fluent Email is available in the nearest nugget server so go ahead and install this package.
Before we send the email we need to configure our email smpt server in our configuration file:
  <system.net>
  <mailSettings>
    <smtp>
      <network enableSsl="true" host="smtp.gmail.com" port="587" userName="nerush.dennis@gmail.com" password="*****" defaultCredentials="false" />
    </smtp>
  </mailSettings>
  </system.net>
All the above values are different when using different smpt servers. If you want gmail to be your email server then you have to specify the port as 587 and set enableSsl to true.

The API

Email.From (myEmail)
     .To(sendToEmail)
     .Subject("New order has arrived!")
     .Body("The order details are…")  
     .Send();

Pretty straight forward.

If you are not willing to wait until the email is being sent you can use the Sendsync method:
Email.From (myEmail)
     .To(sendToEmail)
     .Subject("New order has arrived!")
     .Body("The order details are…")  
     .Sendsync(MailDeliveredCallback);
  
You can specify the “from” email value in the config file:
<system.net>
  <mailSettings>
    <smtp from=”Nerush.dennis@gmail.com”>

And then you can use the “FromDefault” method:
Email.FromDefault()
     .To(sendToEmail)
     .Subject("New order has arrived!")
     .Body("The order details are…")  
     .Send();

Templates

Another cool feature is the templates. Let’s say that you want to send a nice email to your users which has not only simple text but a table, a picture and some more stuff. You can create an HTML template and use it for your mail:

Template.cshtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body dir="rtl">
    <h1>New user has signed in for the website!</h1>
    <p>New user’s name:<b>@Model.Name</b>
        <br/>
        The new user email: <b>@Model.Email</b>
        <br/>
        <u>
The message text:
        </u>
        @Model.Text
    </p>
    <br/>
    <p>Have a great day!</p>
</body>
</html>

Notice the @Model? Those are parameters that you can pass for the template from the API:

Email.FromDefault()
     .To(sendToEmail)
     .Subject("New user has signed in!")
     .UsingTemplate(tempalte, new {Name = message.Name,
       Email = message.Email,
       Text = message.Text})

     .Send();

Culture support

In the last commits of the project Eyal Luxenburg added another cool feature for the library – the ability to use a template based on the current culture. The method is calledUsingCultureTemplateFromFile. This is the methods signature:
UsingCultureTemplateFromFile<T>(string filename, T model, CultureInfo culture = null, bool isHtml = true)

If you specify a culture it will try to load the template file which has the matching culture extension (same way as with the resource files). Specifying the He-IL culture will force fluent email to look for a template file that has the culture extension: template.he-IL.cshtml. This way you can create email templates for all the languages that your app supports.

Email.FromDefault()
     .To(sendToEmail)
     .Subject("See you in the next post")
           .UsingCultureTemplateFromFile(template, emailModel, hebrewCulture)
     .Send();