Adding e-mail capability to a .NET system is not much of a programming task;
it's mostly an analysis or design adventure.
So the first question I would ask is why do you need e-mail in your system?
After all, most if not all users already have some form of e-mail.
Here are the areas where, in my opinion, e-mail offers a worthwhile addition to
a .NET system:
- A general notification to a long list of clients, perhaps about an upcoming
seminar;
- A specific e-mail to one or more destinations in order to attach a periodic
document (like a weekly sales summary report);
- A hidden e-mail (without user's knowledge) to communicate technical details
about a system problem to the support team.
Therefore, in situations where a user can open his/her own e-mail system and
send something unique and on-demand, a system-generated e-mail may not be
feasible.
Beyond that, once you have verified the justification for adding e-mail to your
.NET system, you can easily implement something like the following code sample
where an e-mail is sent from Adam to Eve (using only eight lines of VB.NET
code):
Dim email As New System.Web.Mail.MailMessage
email.To = "eve@gardenofeden.com"
email.From = "adam@gardenofeden.com"
email.Subject = "Thank you"
email.Body = "The apple was delicious!"
email.BodyFormat = Web.Mail.MailFormat.Html
System.Web.Mail.SmtpMail.SmtpServer =
"SmtpServerName"
System.Web.Mail.SmtpMail.Send(email)
While the above code is, for the most part, self-explanatory, I'd like to add a
few comments:
a) You'll notice that on the 3rd line, we're defining Adam's e-mail address.
If you think about this for a second, you'll notice something that may not be
obvious: Unlike a personally generated e-mail where the sender's e-mail
address is obvious, in a system-generated e-mail, you can choose whatever you
want to be the sender's e-mail address. In other words, you can generate
an e-mail which, in effect, hides the true identity of the sender (being a
system and not a real person) and pretend it to have been sent by any real
person you want.
b) Regarding the SmtpServerName, you want
to make sure that the server accepts the e-mail. I have noticed two
situations where the SmtpServerName causes
an error: First, your test machine and your production web server may not
work with the same SmtpServerName.
Second, if a user is connected to your website from inside a corporate firewall,
you may need to use an SmtpServerName that
is acceptable to the firewall. To solve the first problem, you can use two
different SmtpServerName addresses, one
for your test machine and another for the web (selected dynamically via a
parameter that comes from your web.config file).
To solve the second problem, you may need to enable your web page to receive the
SmtpServerName as an argument.
c) And last but not least, no matter how many pre-cautions you plan for,
the Send method may cause an error.
Therefore, as a general rule of good coding practice, the Send method should be part of a
Try-Catch block; something like:
Try
System.Web.Mail.SmtpMail.Send(email)
lblErrorLine.Text = "Email successfully
sent."
Catch ex As Exception
lblErrorLine.Text = "The following
error occurred during the Email process:<br>" & _
"<br>" & _
"From: " & email.From & "<br>" & _
"SMTP: " & Session("MySMTP") & "<br>" &
_
"Error Message: " & ex.Message & "<br>"
& _
"<br>" & _
"Please notify the administrator:
<B>administrator@mysystem.com</B>"
End Try
For further information, please refer to our feedback
page.
|