I have a really small application that sends and email. I would like to know if the email was sent successfully or not. The System.Net.Mail has a property "DeliveryNotificationOptions", but I'm unsure of how should used.
My question is, Can I use the DeliveryNotificationOptions to determine if the email was sent or not? If yes, could you explain how I would do that? If not, can you recommend a way to accomplish this?
Thanks a TON!!!!
Bob
I copied the code for the button click to show the creation and sending of the email.(Really basic)
protected
void btnSubmit_OnClick(object sender, EventArgs e){
try
{
MailAddress toAddress = new MailAddress("[email protected]");
MailAddress fromAddress = new MailAddress(tboxEmailAddress.Text);
MailAddress ccAddress = new MailAddress("[email protected]");
MailMessage email = new MailMessage(fromAddress, toAddress);
email.CC.Add(ccAddress);
email.Subject = "Subject";
email.Body = "Customer Name: " + tboxFirstName.Text + " " + tboxLastName.Text + "\n";
email.Body += "Email Address: " + tboxEmailAddress.Text + "\n";
//////////////////////////////////////////////////////////////////
/// Add Attachment
//////////////////////////////////////////////////////////////////
if (btnFileBrowse.FileName != "")
{
Attachment data = new Attachment(btnFileBrowse.PostedFile.FileName);
// Add the file attachment to this e-mail message.
email.Attachments.Add(data);
email.Body += "Uploaded File: " + btnFileBrowse.FileName.ToString();
}
SmtpClient newClient = new SmtpClient("127.0.0.1");
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.On;
lblStatus.Visible = true;
newClient.Send(email);
}
catch (Exception ex)
{
lblStatus.Visible = true;
lblStatus.Text = ex.Message.ToString();
}
}
AlanPosted Oct 9, 2008, 9:52 AM
Yes, you can use that property to notify whether the email was sent successfully or not and I see from your code that you're almost there with OR'ing the values for failure or success together. What you want is:
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess;
You might also want to add a notification if delivery is delayed which would be:
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.Delay;
Michael HidalgoPosted Aug 27, 2010, 1:45 PM
What if I have the following code snippet:
With emailMessaje
.From = New MailAddress("[email protected]")
.ReplyTo = New MailAddress("[email protected]")
End With
Notice that both address has different domains. If I set a DeliveryNotificationOptions who email address will receive the notification? The From or ReplyTo?
AlanPosted Oct 10, 2008, 7:23 AM
All that property does is to ensure that the sender receives an email notification if the message was or was not successfully sent or was delayed.
It doesn't cause any properties to be set in the MailMessage or SmtpClient objects or raise any events.
As I said in my previous post, all you can do programatically to is examine the StatusCode if an exception is thrown but otherwise to assume that the message was sent successfully.
bob greenPosted Oct 9, 2008, 8:19 PM
So what does DeliveryNotificationOptions do for me?
(email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess;)
I assumed that by setting this, I was going to be able to determine a success or failure at this point. So I'm really not sure what good this does for me?
Can you please explain this to me?
Thanks Bob
AlanPosted Oct 9, 2008, 5:05 PM
Well, email notification of the success, failure or delay will be sent to the sender.
I don't think there's a way to get the result programatically though if there's an SmtpException thrown you can examine its StatusCode property to see what went wrong:
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpexception.statuscode.aspx
bob greenPosted Oct 9, 2008, 2:01 PM