My site will send a confirmation email once a user has registered with it. So once the user has registered they will get a nice colourful email showing them their login details;
Within C# the ability to send an email is contained within the System.Net.Mail namespace. I first off created a new class called Postman to encapsulate the code sending the email.
This will have the following properties
public string To { get; set; }
public string From { get; set; }
public string Body { get; set; }
public string Host { get; set; }
public string Subject { get; set; }
public string Token { get; set; }
public Boolean IsBodyHtml { get; set; }
to do this I have taken advantage of C#’s ability to generate field variables automatically- this greatly reduces the amount of code I need to create properties. Of course if I really need to do validation and the lot I could always use a more traditional property definition.
The Email sending functionality is almost all set in a method called Send(). First off we have to create a message object, and an SmtpClient object.
client = new SmtpClient(host: Host);
to = new MailAddress(address: To);
from = new MailAddress(address: From);
msg = new MailMessage(from: From, to: To);
I have also set the address the email is from and to. Next we set the form of the email.
msg.IsBodyHtml = true
then the body of the email
msg.Body = Body;
msg.BodyEncoding = System.Text.Encoding.UTF8;
and it’s subject
msg.Subject = Subject;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
Now because this is being sent from a web service I want to send the email asynchronously. In this way the user is not held up while we are doing the actual send.
This requires that we set up an event handler to execute when the email is sent – at the moment this function does little;
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
.
.
.
void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
string messageId = string.Empty;
try
{
messageId = (String)e.UserState;
}
catch
{
throw;
}
}
Now to send the email
client.SendAsync(message: msg, userToken: Token);
Below is the full code for the Postman object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
namespace Simplicita.Services.Support
{
public class Postman : IDisposable
{
public string To { get; set; }
public string From { get; set; }
public string Body { get; set; }
public string Host { get; set; }
public string Subject { get; set; }
public string Token { get; set; }
public Boolean IsBodyHtml { get; set; }
public void Send()
{
SmtpClient client = null;
MailAddress to = null;
MailAddress from = null;
MailMessage msg = null;
try
{
// validate email parameters
if (this.Host == String.Empty)
throw new Exception(“You must provide an SMTP Host”);
if (this.To == String.Empty)
throw new Exception(“You must provide an address to send the email to”);
if (this.From == String.Empty)
throw new Exception(“You must provide an address that the email is from”);
if (this.Subject == String.Empty)
throw new Exception(“You must provide a subject for the email”);
// set a token to identify this email
if ((this.Token == String.Empty) || (this.Token == null))
{
Guid tokenGuid = Guid.NewGuid();
try
{
this.Token = tokenGuid.ToString();
}
catch
{
throw;
}
}
// initialise SMTP Parameters
client = new SmtpClient(host: Host);
to = new MailAddress(address: To);
from = new MailAddress(address: From);
msg = new MailMessage(from: From, to: To);
// initialise the email parameters
msg.IsBodyHtml = true;
msg.Body = Body;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.Subject = Subject;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
// set up the Async completed property
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
client.SendAsync(message: msg, userToken: Token);
}
catch
{
throw;
}
}
void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
string messageId = string.Empty;
try
{
messageId = (String)e.UserState;
}
catch
{
throw;
}
}
public void Dispose()
{
}
}
}
Pingback: 2010 in review « Developing My Cloud Application