WYSIWYG

http://kufli.blogspot.com
http://github.com/karthik20522

Saturday, September 1, 2012

Amazon SES (Simple Email Service) - Code

SES (simple email service) provided by Amazon is sleek easy to use email service. Unfortunately as always, anything new has limited documentation or overly complicated documentation. Following is a ready to use simple code snippet for using Amazon SES written in CSharp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
 
namespace Manager
{
    public class EmailManager
    {
        public string SendMail(List<string> toAddresses, List<string> ccAddress, List<string> bccAddress,
            string fromName, string fromAddress,
            string subject, string content,            
            string awsAccessKey, string awsSecretKey)
        {
            try
            {
                var amazonConfiguration = new AmazonSimpleEmailServiceConfig();
                var client = new AmazonSimpleEmailServiceClient(awsAccessKey, awsSecretKey, amazonConfiguration);
                 
                var destination = new Destination();
                destination.ToAddresses = toAddresses;
                destination.CcAddresses = ccAddress;
                destination.BccAddresses = bccAddress;
 
                var body = new Body()
                {
                    Html = new Content(content)
                };
 
                var emailSubject = new Content(subject);
                var message = new Message(emailSubject, body);
                var sendEmailRequest = new SendEmailRequest(string.Format("{0} <{1}>", fromName, fromAddress), destination, message);
 
                var response = client.SendEmail(sendEmailRequest);
                return response.SendEmailResult.MessageId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Note: Amazon SDK needs to be installed or AWSSDK.dll need to be referenced. Download AWS SDK here So where can I find the “awsAccessKey” (Access key id) and “awsSecretKey” (Secret Key). Well, first login into AWS portal and then head over to Security Credentials page.



In the Security Credentials page look for Access Credentials section and you should have your Access Key Id and your Secret key.

Labels: , ,