简体   繁体   English

检查电子邮件状态或收件人状态c#

[英]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. Send()方法不返回值,但是会在发送电子邮件时等待,如果出现问题,它将引发异常。 Enclose your Send in a try/catch block. 将“发送”包含在“ try/catch块中。

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. 我不能肯定地说这100%保证了电子邮件的发送。 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. 假设您的SMTP服务器为xyz,并且您向电子邮件为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. 您需要有权访问gmail SMTP,以查看您发送的邮件是否已传递给收件人。

If you don't get any error when you are sending an email, it's safely to assume that email has been delivered. 如果您在发送电子邮件时没有遇到任何错误,可以安全地假定电子邮件已发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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