How to konw email sent status(email successfully sent or not ?) using kentico code ?

Jainik Patel asked on June 7, 2017 12:07

How to konw email sent status(email successfully sent or not ?) using kentico code ?

Here is my code.

     EmailMessage msg = new CMS.EmailEngine.EmailMessage();
            EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("RequestQuoteSupplier", SiteContext.CurrentSiteID);
            MacroResolver mcr = MacroResolver.GetInstance();
            mcr.SetNamedSourceData("WebsiteName", SettingsKeyInfoProvider.GetValue("WebsiteName"));
            mcr.SetNamedSourceData("WebsiteURL", SettingsKeyInfoProvider.GetValue("WebsiteURL"));
            mcr.SetNamedSourceData("FullName", txtfullName.Text);
            mcr.SetNamedSourceData("PhoneNumber", txtPhoneNumber1.Text);
            mcr.SetNamedSourceData("Email", txtEmail.Text);
            mcr.SetNamedSourceData("City", txtProjectCity.Text);
            mcr.SetNamedSourceData("State", drpState.SelectedValue);
            mcr.SetNamedSourceData("Comments", txtComment.Text);
            mcr.SetNamedSourceData("ZipCode", txtProjectZip.Text);
            mcr.SetNamedSourceData("Company", txtCompany.Text);
            mcr.SetNamedSourceData("quoteOption", QuoteOptions);
            msg.EmailFormat = EmailFormatEnum.Html;
            msg.From = eti.TemplateFrom; //make sure this is 

            msg.Recipients = Convert.ToString(values[i].Trim());


           // msg.Recipients = Convert.ToString("jainik.spinx@gmail.com");
            msg.Subject = mcr.ResolveMacros(eti.TemplateSubject);
            msg.Body = mcr.ResolveMacros(eti.TemplateText);
            EmailSender.SendEmail(SiteContext.CurrentSiteName, msg,true);

Recent Answers


Dawid Jachnik answered on June 7, 2017 12:28 (last edited on June 7, 2017 12:28)

Hello,

There isn't any preprared method for that. You need to get the send message and check it's status. If you have multiple recipients you will need to have something like this:

foreach (var recipient in msg.Recipients.Split(';'))
    {
        var email = EmailInfoProvider.GetEmailInfos(string.Format("EmailFrom='{0}' and EmailTo='{1}' and EmailSubject='{2}'", msg.Subject, recipient, msg.Subject), null).FirstOrDefault();
        if (email != null)
        {
            if (email.EmailStatus == EmailStatusEnum.Archived)
            {
                //sent
            }
        }
    }
0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on June 7, 2017 12:36 (last edited on June 7, 2017 12:37)

I guess the method will throw an error, if it can't send email message. Also, you use immediate sending, it means that the message is sent to SMTP server directly without processing by Kentico. If you pass false to the last parameter, messages will be send via Kentico Email queue. And it will possible to check the status of email message by fetching the message via EmailInfoProvider as Dawid mentioned

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on June 7, 2017 12:42

Yes, Anoton you're right. My method will work only if you use email queue.

0 votesVote for this answer Mark as a Correct answer

Jainik Patel answered on June 7, 2017 12:51 (last edited on June 7, 2017 12:55)

I just know how to email sent success or faild throug code? In kentico

EmailSender.SendEmail(SiteContext.CurrentSiteName, msg,true);

method did not any return any status like mail sent fail or success.

example : I have insert wrong smtp in setting so How identify the mail sent or fail through code.

In C# return the true or false for success and fail mail so..In kentico How I implement ?

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on June 7, 2017 13:41

You just need to catch errors if they happen. If you catch it, the message was not sent, and you should return false, otherwise it is true. But I'm afraid that this behavior is not guaranteed, because delivery depends on lots of settings of your SMTP server and the target SMTP server.
Links on StackOverflow:
How to check if the mail has been sent sucessfully
How to confirm that mail has been delivered or not?

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.