6
Answers

Question Understanding a Feature System.Net.Mail

bob green

bob green

14y
11.2k
1

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();

}

}


Answers (6)