Quick Reference on how to send an email using .NET
Many .NET applications will require an email be sent out for various reasons. This article will give a quick examples on how to send an email using VB.NET. The examples given can easily be translated into C#.NET.
Storing Email Credentials
If you are developing a web application the best place to store your email credentials is in your web.config file's <appSettings>. This will allow you to secure your email credentials using encryption and also provide a single location for all of your code to access the credentials.
-
<configuration>
-
<appSettings>
-
<add key="EmailUserName" value="userName"/>
-
<add key="EmailPassword" value="password"/>
-
</appSettings>
-
</configuration>
If you are using .NET Framework version 1.1
You will have to import System.Web.Mail
This Framework will not easily allow you to supply email credentials that allow you to connect to your mail server. I will not cover how to do this in this article.
-
Dim mailMsg As Mail.MailMessage
-
Dim body As new String = "This will be the body of my email message"
-
-
mailMsg= New MailMessage
-
mailMsg.To= "to@blabla.com"
-
mailMsg.From= "from@blabla.com"
-
mailMsg.Subject = "The subject of the email"
-
mailMsg.BodyFormat = MailFormat.Html
-
mailMsg.Body = body
-
-
SmtpMail.SmtpServer="localhost"
-
-
Try
-
SmtpMail.Send(mailMsg)
-
Catch ex As Exception
-
-
End Try
-
If you are using .NET Framework version 2.0
You will have to import System.Net.Mail.MailMessage and if you need to provide email credentials in order to connect to your mail server System.Net.NetworkCredentials
Please note that the following code snippet shows how to retrieve your user credentials from the web.config file. If you are not storing your credentials in this file you can simply add strings for the user name and password instead of using ConfigureationManager.AppSettings(...) to supply your credentials.
Also if you do not have to supply user credentials in order to connect to your mail provider you can ignore anything that has to do with credentials in this example
-
Try
-
Dim emailTitle As String ="My Email Title"
-
-
Dim emailMessage As Net.Mail.MailMessage
-
-
Dim body As String = "This will appear in the body of my email"
-
-
emailMessage = New Net.Mail.MailMessage("from@emailAddress.com", "to@emailAddress.com", emailTitle, body)
-
-
Dim mailClient As New Net.Mail.SmtpClient("urlOfEmailService.com", 25)
-
'25 is the port on which the mail server is listening. If your mail server
-
'runs on the default port this number does not need to be provided
-
-
-
'If you do not need to provide credentials to connect to the mail server you can ignore the next 3 lines
-
Dim myCredentials As New System.Net.NetworkCredential(System.Configuration. ConfigurationManager.AppSettings("EmailUserName"), System.Configuration.ConfigurationManager.AppSetti ngs("EmailPassword"))
-
mailClient.UseDefaultCredentials = False
-
mailClient.Credentials = myCredentials
-
-
mailClient.Send(emailMessage)
-
-
Catch ex As Exception
-
-
End Try
-
I hope this has helped you!
-Frinny
Plater /
May 21st, 2007 04:31 PM
As a side note:
Be aware that sending emails through .NETs built in mailer, for whatever reason, does not send message immediatly. (I have had to wait up to 3hours for an email to myself to arrive)
If the wait time is not an issue it's great, if you want immediate results, you will have to look elsewhere.
Quote:
As a side note:
Be aware that sending emails through .NETs built in mailer, for whatever reason, does not send message immediatly. (I have had to wait up to 3hours for an email to myself to arrive)
If the wait time is not an issue it's great, if you want immediate results, you will have to look elsewhere.
|
Wow, I've never experienced that before.
I guess it depends on your provider.
Plater /
May 22nd, 2007 12:35 PM
Well, I wrote my own mailer using sockets and every email went instantaniously. I did head-to-heads with the .NET implementation and my own and mine won out every time.
|
|
|
|