简体   繁体   中英

check email status or recipient status c#

I got a task to check whether if the email is being send successful to the recipient

 using (MailMessage mm = new MailMessage("kayone831@gmail.com", "petrawaretemp@gmail.com"))
            {
                int emailStatus = 1;

                mm.Subject = "TRY";
                mm.Body = "TRY";
                Attachment attachment = new Attachment(pdfFilename,
                MediaTypeNames.Application.Octet);
                mm.Attachments.Add(attachment);
                mm.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential("kayone831@gmail.com", "renkaiheng");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
                Response.Write("<script>alert('Success');</script>");
            }

Would like to ask is there a way to know if my email is successfully sent to the recipient or I can check whether the recipient's email is existing before I send the email. Because I need to save the status of this email every time when an email is sent whether it is success/failure. Any help or guide is very much appreciated.

The Send() method doesn't return a value, but it does wait while the email is transmitted, and it'll throw an exception if something goes wrong. Enclose your Send in a try/catch block.

try
{
    smtp.Send(mm);
    // record the email as successful
}
catch (SmtpException smtpEx)
{
    // authentication failed, operation timed out, etc
    // record the email as failed
}
catch (SmtpFailedRecipientsException ex)
{
    // message couldn't be delivered to one or more recipients
    // record the email as failed
}

I can't say for sure that this 100% guarantees the email was delivered. You're depending on the server that received the email, so hopefully it returns accurate information.

You can find more information in the documentation .

There is no possible way to check, that a message was delivered to recipient.

Let's say your SMTP server is xyz and your sending a mail to the recipient whose email is in gmail. You need to have access to the gmail SMTP to see if the message which you have sent is delivered to the recipient or not.

If you don't get any error when you are sending an email, it's safely to assume that email has been delivered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM