Connecting Tech Pros Worldwide Forums | Help | Site Map

Quick Reference on how to send an email using .NET

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,118
#1   May 18 '07
Introduction
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.

Expand|Select|Wrap|Line Numbers
  1. <configuration>
  2.      <appSettings>
  3.         <add key="EmailUserName" value="userName"/>
  4.         <add key="EmailPassword" value="password"/>
  5.     </appSettings>
  6. </configuration>
  7.  

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.

Expand|Select|Wrap|Line Numbers
  1. Dim mailMsg As Mail.MailMessage
  2. Dim body As new String = "This will be the body of my email message"
  3.  
  4. mailMsg= New MailMessage
  5. mailMsg.To= "to@blabla.com"
  6. mailMsg.From= "from@blabla.com"
  7. mailMsg.Subject = "The subject of the email"
  8. mailMsg.BodyFormat = MailFormat.Html
  9. mailMsg.Body = body
  10.  
  11. SmtpMail.SmtpServer="localhost"
  12.  
  13. Try
  14.     SmtpMail.Send(mailMsg)
  15. Catch ex As Exception
  16.  
  17. End Try
  18.  
  19.  

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

Expand|Select|Wrap|Line Numbers
  1. Try
  2.     Dim emailTitle As String ="My Email Title"
  3.  
  4.     Dim emailMessage As Net.Mail.MailMessage
  5.  
  6.     Dim body As String = "This will appear in the body of my email"
  7.  
  8.     emailMessage = New Net.Mail.MailMessage("from@emailAddress.com", "to@emailAddress.com", emailTitle, body)
  9.  
  10.     Dim mailClient As New Net.Mail.SmtpClient("urlOfEmailService.com", 25)
  11.     '25 is the port on which the mail server is listening.  If your mail server
  12.     'runs on the default port this number does not need to be provided
  13.  
  14.  
  15.     'If you do not need to provide credentials to connect to the mail server you can ignore the next 3 lines
  16.     Dim myCredentials As New System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings("EmailUserName"), System.Configuration.ConfigurationManager.AppSettings("EmailPassword"))
  17.     mailClient.UseDefaultCredentials = False
  18.     mailClient.Credentials = myCredentials
  19.  
  20.     mailClient.Send(emailMessage)
  21.  
  22. Catch ex As Exception
  23.  
  24. End Try
  25.  
  26.  
I hope this has helped you!

-Frinny



Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2   May 21 '07

re: Quick Reference on how to send an email using .NET


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.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,118
#3   May 22 '07

re: Quick Reference on how to send an email using .NET


Quote:

Originally Posted by Plater

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's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4   May 22 '07

re: Quick Reference on how to send an email using .NET


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.
Reply


Similar .NET Framework bytes