Created
August 3, 2012 14:37
-
-
Save zwang/3248189 to your computer and use it in GitHub Desktop.
Sending email using Amazon Simple Email service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Sending email using Amazon SES | |
/// </summary> | |
public class AmazonSES | |
{ | |
private static string accessKey = your access key in string; | |
private static string secretKey = your access secret key; | |
private static string _fromEmail = "[email protected]"; | |
private static int _maximumDeliveryAttempts = 3; | |
/// <summary> | |
/// Send out an email using Amazon SES Service | |
/// </summary> | |
/// <param name="toAddress"></param> | |
/// <param name="subject"></param> | |
/// <param name="body"></param> | |
/// <returns></returns> | |
public static bool SendEmailAWS(Guid ID, string toAddress, string subject, string body, ref int deliveryAttempts, string replyToAddress = "") | |
{ | |
if (deliveryAttempts >= _maximumDeliveryAttempts) | |
{ | |
deliveryAttempts = -1; | |
TraceInformation("Email sending attempts reached limit. ID: " + ID.ToString()); | |
return false; | |
} | |
else | |
{ | |
AmazonSimpleEmailServiceClient client; | |
client = new AmazonSimpleEmailServiceClient(accessKey, secretKey); | |
Message msg = new Message(); | |
msg.Subject = new Content(subject); | |
Body bd = new Body(); | |
bd.Html = new Content(body); | |
msg.Body = bd; | |
Destination dest = new Destination(); | |
dest.ToAddresses.Add(toAddress); | |
string fromEmailWithName = "\"Company Name\"" + "<" + _fromEmail + ">"; | |
SendEmailRequest sendRequest = new SendEmailRequest(fromEmailWithName, dest, msg); | |
if (replyToAddress != "") | |
{ | |
sendRequest.ReplyToAddresses.Add(replyToAddress); | |
} | |
//Specify the returnpath to receive the bounce | |
sendRequest.ReturnPath = _fromEmail; | |
try | |
{ | |
SendEmailResponse sendResp = client.SendEmail(sendRequest); | |
//Successful | |
return true; | |
} | |
catch (MessageRejectedException ex) | |
{ | |
TraceError("SendEmailByAWS message rejected exception. ID: " + ID.ToString() + ";" + ex.Message, ex); | |
return false; | |
} | |
catch (Exception ex) | |
{ | |
TraceError("SendEmailByAWS Exception. ID: " + ID.ToString() + ";" + ex.Message, ex); | |
return false; | |
} | |
} | |
} | |
/// <summary> | |
/// Get a list of verified Email address from which we can use to send email | |
/// </summary> | |
/// <returns></returns> | |
public static List<string> GetListOfVerifiedEmailAddress() | |
{ | |
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(accessKey, secretKey); | |
ListVerifiedEmailAddressesRequest req = new ListVerifiedEmailAddressesRequest(); | |
ListVerifiedEmailAddressesResponse resp = client.ListVerifiedEmailAddresses(req); | |
List<string> verifiedEmails = new List<string>(); | |
try | |
{ | |
verifiedEmails = resp.ListVerifiedEmailAddressesResult.VerifiedEmailAddresses; | |
return verifiedEmails; | |
} | |
catch | |
{ | |
return null; | |
} | |
} | |
/// <summary> | |
/// Verify an email address | |
/// </summary> | |
/// <param name="emailToVerify"></param> | |
/// <returns></returns> | |
public static bool VerifyEmailAddress(string emailToVerify) | |
{ | |
using (var client = new AmazonSimpleEmailServiceClient(accessKey, secretKey)) | |
{ | |
Amazon.SimpleEmail.Model.VerifyEmailAddressRequest veriRequest = new Amazon.SimpleEmail.Model.VerifyEmailAddressRequest(); | |
veriRequest.EmailAddress = emailToVerify; | |
try | |
{ | |
Amazon.SimpleEmail.Model.VerifyEmailAddressResponse veriResp = client.VerifyEmailAddress(veriRequest); | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
} | |
/// <summary> | |
/// Get the statistics about using Amazon SES | |
/// Latest 10 records | |
/// </summary> | |
/// <returns></returns> | |
public static List<SendDataPoint> GetEmailStatistic(int numberToTake) | |
{ | |
using (AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(accessKey, secretKey)) | |
{ | |
GetSendStatisticsRequest req = new GetSendStatisticsRequest(); | |
GetSendStatisticsResponse resp = client.GetSendStatistics(req); | |
List<SendDataPoint> sendDatas = resp.GetSendStatisticsResult.SendDataPoints; | |
return sendDatas.OrderByDescending(s => s.Timestamp).Take(numberToTake).ToList(); | |
} | |
} | |
/// <summary> | |
/// Get the Email Usage Quota | |
/// </summary> | |
/// <returns></returns> | |
public static GetSendQuotaResult GetSendQuota() | |
{ | |
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(accessKey, secretKey); | |
GetSendQuotaRequest req = new GetSendQuotaRequest(); | |
GetSendQuotaResponse resp = client.GetSendQuota(req); | |
GetSendQuotaResult result = resp.GetSendQuotaResult; | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment