Asp.net中的电子邮件传递消息(如何检查电子邮件是否已发送?)

时间:2013-05-27 06:51:00

标签: c# asp.net email smtp

protected void btnSend_Click(object sender, EventArgs e)
{
    string mFrom = "xxx.com";
    string mTo = "yyy.com";
   string msub="TESTSUBJECT";
    string mMsg="TEST msg";
    string mCc = "Test cc";

    // User credential used for sending mail
    string EmailSender = "xxx.com";
    string EmailSenderPasswd = "zzz";
    System.Net.NetworkCredential myCachet = new System.Net.NetworkCredential(EmailSender, EmailSenderPasswd);
    SmtpClient smtp = new SmtpClient("smtpout.secureserver.net", 12234);
    smtp.Credentials = myCachet;

    MailMessage message = new MailMessage(mFrom.ToString(), mTo.ToString());
    message.Subject = msub.ToString();
    message.Body = mMsg.ToString();
    message.IsBodyHtml = true;
    if (mCc.Trim() != String.Empty)
    {
        message.CC.Add(new MailAddress(mCc.ToString()));
    }
    smtp.Send(message);
    message.Dispose();
}

如果我传递了错误的密码,smtp.send(message)方法将返回“消息失败”。但是,如果我传递错误的电子邮件地址,它返回成功消息作为“消息成功”,它不会传递任何异常,即使我尝试尝试并捕获异常。如何知道该邮件已成功发送?如果该消息未成功传递意味着它应该抛出未传递的消息,如gmail(消息传递失败)。帮助我。

1 个答案:

答案 0 :(得分:0)

添加:

message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

传递通知邮件有效。但我必须在db中存储未传递的邮件详细信息。如何跟踪未交付的邮件(按用户名)。