C# 如何检查邮件是否发送成功
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2342456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if the mail has been sent successfully
提问by Vaibhav Jain
I am developing an Asp.Net application, where I am sending a mail to the user's email address, if he forgets the password.
我正在开发一个 Asp.Net 应用程序,如果他忘记密码,我将向用户的电子邮件地址发送邮件。
I want to check if the mail has been sent sucessfully or not. Is there any method to know that for sure.
我想检查邮件是否已成功发送。有什么方法可以肯定地知道这一点。
EDIT
编辑
In case if an email id does'nt exists, then would I detect a failure.
如果电子邮件 ID 不存在,那么我会检测到故障。
采纳答案by Francisco Aquino
if your SmtpMail.Send(message)
method returns no error, it means the email was sent to the SMTP server, then you are out of your jurisdiction, that is how far you can know.
如果您的SmtpMail.Send(message)
方法没有返回错误,则表示电子邮件已发送到 SMTP 服务器,那么您不在您的管辖范围内,这就是您可以知道的程度。
回答by Anton Gogolev
According to spec:
根据规格:
S: 220 smtp.example.com ESMTP Postfix
C: HELO relay.example.org
S: 250 Hello relay.example.org, I am glad to meet you
C: MAIL FROM:<[email protected]>
S: 250 Ok
C: RCPT TO:<[email protected]>
S: 250 Ok
C: RCPT TO:<[email protected]>
S: 250 Ok
C: DATA
S: 354 End data with <CR><LF>.<CR><LF>
C: From: "Bob Example" <[email protected]>
C: To: Alice Example <[email protected]>
C: Cc: [email protected]
C: Date: Tue, 15 Jan 2008 16:02:43 -0500
C: Subject: Test message
C:
C: Hello Alice.
C: This is a test message with 5 header fields and 4 lines in the message body.
C: Your friend,
C: Bob
C: .
S: 250 Ok: queued as 12345
C: QUIT
S: 221 Bye
{The server closes the connection}
As soon as server says 250 Ok: queued as 12345
, you cannot know for sure if it had really sent an email or not, or whether it was delivered.
服务器一说250 Ok: queued as 12345
,您就无法确定它是否真的发送了电子邮件,或者是否已发送。
回答by jarnbjo
No. E-mail (SMPT based) is an unreliable transport protocol and although there are some hacks to detect that an e-mail has been received and read, e.g. by embedding an individualized image URL in the e-mail and tracking that the image has been requested by the recipient's client, there is no absolutely reliable way to fulfil your request.
不。电子邮件(基于 SMPT)是一种不可靠的传输协议,尽管有一些黑客可以检测到电子邮件已被接收和阅读,例如通过在电子邮件中嵌入个性化的图像 URL 并跟踪图像接收方的客户要求,没有绝对可靠的方法来满足您的要求。
回答by Ken Pespisa
The SmtpClient.Sendmethod will raise an Exception if there's a problem sending. But beyond getting that message to the SMTP server, there's no way to know if it makes it to the destination from there.
如果发送出现问题,SmtpClient.Send方法将引发异常。但是除了将该消息发送到 SMTP 服务器之外,没有办法知道它是否从那里到达目的地。
回答by Brandon
Put the .Send(msg) method in a try catch block, and catch SmtpFailedRecipientException.
将 .Send(msg) 方法放在 try catch 块中,并捕获 SmtpFailedRecipientException。
try
{
mail.Send(msg);
}
catch (SmtpFailedRecipientException ex)
{
// ex.FailedRecipient and ex.GetBaseException() should give you enough info.
}
回答by statenjason
If you're using System.Net.Mail try out
如果你使用 System.Net.Mail 试试
message.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnSuccess;
回答by Aaron
You can use the DeliveryNotificationOptions to receive a receipt.
您可以使用 DeliveryNotificationOptions 来接收收据。
If you have a MailMessage
object named mail, do this:
如果您有一个MailMessage
名为 mail的对象,请执行以下操作:
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
回答by Ismail Gunes
I'am using gmail SMTP for sending mails with my program. A fake mail sent returns Ok even with SmtpFailedRecipientException trap.
我正在使用 gmail SMTP 通过我的程序发送邮件。即使有 SmtpFailedRecipientException 陷阱,发送的假邮件也会返回 Ok。
But when I check with outlook my gmail recipient I see that mail was not sent with explanation. With a subject Delivery Status Notification (Failure)
但是当我查看我的 gmail 收件人的 Outlook 时,我看到邮件没有发送解释。带有主题的传递状态通知(失败)
My question is it possible to get this notificiation whitin the program.
我的问题是有可能在程序中获得此通知。
I found this but it's not for POP
我找到了这个,但它不适用于 POP