Mehdi Abbas

Mehdi Abbas

  • NA
  • 1
  • 888

Get notification of every Emails

Jun 25 2015 6:54 AM
public void SendEmail()
{
byte[] kSecret = Encoding.UTF8.GetBytes(("AWS4" + ConfigurationManager.AppSettings["AWSSecretKey"].ToString()).ToCharArray());
//////INITIALIZE AWS CLIENT//
AmazonSimpleEmailServiceConfig amConfig = new AmazonSimpleEmailServiceConfig();
//amConfig.UseSecureStringForAwsSecretKey = false;
AmazonSimpleEmailServiceClient amzClient = new AmazonSimpleEmailServiceClient(
ConfigurationManager.AppSettings["AWSAccessKey"].ToString(),
ConfigurationManager.AppSettings["AWSSecretKey"].ToString(), Amazon.RegionEndpoint.USEast1);
Destination dest = new Destination();
var em = db.EmailIds.Where(m => m.MsgID == null || m.MsgID=="").ToList();
double cnt = em.Count();
double loopcnt = Math.Ceiling(cnt / 50);
string msgref="";
for (int i = 0; i < loopcnt; i++)
{
var emails = db.EmailIds.Where(m => m.MsgID == null || m.MsgID=="").Take(50);
foreach (var email in emails)
{
ArrayList to = new ArrayList();
Regex regemail = new Regex(@"^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$");
if (regemail.IsMatch(email.Email_Id))
{
dest.BccAddresses.Add(email.Email_Id);
}
}
string body = this.PopulateBody("UserName", "[email protected]", "Teststring", "Link Here", "This is a enterprise mailing test by Pelsoft development team. Please intimate your reciept of the email to the development team for assessment of inbox deliverability. ");
string subject = "My subject";
Body bdy = new Body();
bdy.Html = new Amazon.SimpleEmail.Model.Content(body);
Amazon.SimpleEmail.Model.Content title = new Amazon.SimpleEmail.Model.Content(subject);
Message message = new Message(title, bdy);
try
{
System.IO.File.AppendAllText(@"D:\\time.txt", "Before" + DateTime.Now + " " + DateTime.Now.Millisecond + " " + Environment.NewLine);
SendEmailRequest ser = new SendEmailRequest("myemail#xyz.in", dest, message);
ser.Source = "[email protected]";
ser.ReturnPath = "[email protected]";
SendEmailResponse seresponse = amzClient.SendEmail(ser);
ser.ReturnPath = "[email protected]";
SendEmailResult seresult = seresponse.SendEmailResult;
foreach (var email in emails)
{
Regex regemail = new Regex(@"^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$");
if (regemail.IsMatch(email.Email_Id))
{
email.MsgID = seresponse.MessageId;
email.SentDateTime = System.DateTime.Now;
}
}
System.IO.File.AppendAllText(@"D:\\time.txt", "After" + DateTime.Now + " " + DateTime.Now.Millisecond + " " + Environment.NewLine);
foreach (var email in emails)
{
Regex regemail = new Regex(@"^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$");
if (regemail.IsMatch(email.Email_Id))
{
dest.BccAddresses.Remove(email.Email_Id);
}
}
}
catch (Exception ex)
{
System.IO.File.AppendAllText(@"D:\\time.txt", ex.Message+" "+ex.InnerException+" "+ex.Source);
}
db.SubmitChanges();
}
}
And i can get the details like this
public ContentResult getstatistics()
{
string data = "";
GetSendStatisticsRequest gssReq = new GetSendStatisticsRequest();
GetSendStatisticsResponse gssResp = amzClient.GetSendStatistics(gssReq);
GetSendStatisticsResult gssResult = gssResp.GetSendStatisticsResult;
GetIdentityNotificationAttributesRequest nreq = new GetIdentityNotificationAttributesRequest();
foreach (SendDataPoint sdp in gssResult.SendDataPoints)
{
data = data + "bounce: " + sdp.Bounces.ToString() + "; \nCOMPLAINTS:" + sdp.Complaints.ToString() + "; \nDELIVERY ATTEMPTS:" + sdp.DeliveryAttempts.ToString() + ";\n REJECTS:" + sdp.Rejects.ToString() + "; \nDateTime:" + sdp.Timestamp.ToLongDateString() + " - " + sdp.Timestamp.ToLongTimeString() + "";
string rejects = sdp.Rejects.ToString();
}
return Content(data);
}
I am using the package AMAZON SDK AWS.
this is my code to send email and get details. it is working fine. but I dont have any logic to get notification of delivery,bounce and complaints.
I am sending the email with the batch of 50. and getting the messageid on every batch of 50 after every response..
In secont function i can get the statistics but it is showing the complete detail but I want to get the status of each and every emails which is delivered or bounce.
How to get them and handle in my c# code. I am searching for this help since last one week but not have any exact solution.
Please help me.
Thanks in advance.