Thursday, March 22, 2012

SmtpMail - see when its send or not

Hello everyone,

I would like to know how I can see when the mail is actually send. I would like to use an if / then / else statement to show / hide a piece of my page.
How can I see when the SmtpMail is send?

Thanks!


private void Send_Mail(object sender, EventArgs e) {
MailMessage mail = new MailMessage();
mail.To = "test@dotnet.itags.org.test.com";
mail.From = Request.Form["email"];
mail.BodyFormat = MailFormat.Text;
mail.Subject = "[TEST] - ";
mail.Subject = mail.Subject + Request.Form["subject"];
mail.Subject = mail.Subject + " - from: " + Request.Form["firstname"] + " " + Request.Form["lastname"];
mail.Body = "A message was send to you from your website (test)\n\nUsers information:\nFirst name: " + Request.Form["firstname"] + "\nLast name:" + Request.Form["lastname"] +"\nEmail address:" + Request.Form["email"] + "\n\n";
mail.Body = mail.Body + "Message:\n" + Request.Form["comments"];
SmtpMail.SmtpServer = "localhost";
try{ SmtpMail.Send(mail); }
catch(Exception MailEx){Info.Text+=MailEx.Message;}
}
I'm not sure that that is possible. The problem is that the mail is sent by the server beyond where your code can reach, so your code doesnt care whether the email is sent or rejected, it just gets it there.

You could add a BCC field to your email that sends a blind carbon copy of every email to yourself so youll know if it sent or not (the person you are sending the email to wont know you got a copy as well)

add under Mail.To:

Mail.BCC = "youemailaddress@.whatever.com"

Austin W.
Yes but that won't tell if it's been received or not.

There are techniques you can use (web bugs etc) to tell if a message has definitely been received and also techniques that you can use (catching exceptions, parsing NDRs -- there are components such asListNanny which can handle the latter) to tell if it has definitely not been received. However, there is a sizable proportion of e-mail messages that will fall between the two camps -- people who receive e-mail messages in plain text, or set their e-mail programs to not download images off the Internet. You just have to resign yourself to the fact that there's not a lot you can do about these.

There are also components that will validate e-mail addresses by contacting the SMTP server at the other end and querying whether it accepts e-mail messages at that address.
Actually that isn't quite what I meant. I just need to be able to hide my form fields on my page as soon as the email is send (doesn't matter whether it arrived or not).
Put them all in a panel and set its visible property to false after the e-mail is sent (provided that there haven't been any exceptions thrown and that the e-mail address gets validated of course).
so how can I check whether exceptions are thrown or not?

Thanks!
You may handle this in your try - catch block. If there is an exception, send fails. Otherwise, send successful.
psuedo code ( I don't know C#)


try{ SmtpMail.Send(mail); }
panel_with_Fields.visible="False" <----------this only runs if no exceptions
catch(Exception MailEx){Info.Text+=MailEx.Message;}
do things here if mail fails to be sent <-------- runs if there are exceptions
Finally
Do somethings here wether try fails or not <------- runs everytime
end try
}

HTH
Thank you all!
I'm gonna try it out :)

0 comments:

Post a Comment