469,343 Members | 5,380 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Quick Reference on how to send an email using .NET

Frinavale
9,735 Expert Mod 8TB
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 not using the using .NET Framework version 1.1
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
May 18 '07 #1
4 16670
Plater
7,872 Expert 4TB
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.
May 21 '07 #2
Frinavale
9,735 Expert Mod 8TB
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.
May 22 '07 #3
Plater
7,872 Expert 4TB
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.
May 22 '07 #4
Frinavale
9,735 Expert Mod 8TB
Just thought that I would post a code snippet here that sends email using GMail.

I used Gmail's reference to find information about ports etc. here: Configuring Gmail for Other Mail Clients


First you need to include a reference to System.Net.Mail and System.NET at the top of your VB.NET or C# code file.

Like this:
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Imports System.Net.Mail
  2. Imports System.Net
(C#)
Expand|Select|Wrap|Line Numbers
  1. using System.Net.Mail;
  2. using System.Net;

Then you can send the email using the following:
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Try
  2.   Dim emailMessage As New MailMessage("fromAccount@gmail.com", "toAccount@gmail.com", "test email", "testing email")
  3.   Dim mailClient As New SmtpClient("smtp.gmail.com", 587)
  4.   mailClient.EnableSsl = True
  5.   mailClient.UseDefaultCredentials = False
  6.   mailClient.DeliveryMethod = SmtpDeliveryMethod.Network
  7.   Dim myCredentials As New System.Net.NetworkCredential("theGmailAccount@gmail.com", "theGmailAccountPassword")
  8.   mailClient.Credentials = myCredentials
  9.   mailClient.Send(emailMessage)
  10.   MessageBox.Show("Sent")
  11. Catch ex As Exception
  12.   MessageBox.Show(ex.Message)
  13. End Try
(C#)
Expand|Select|Wrap|Line Numbers
  1. try {
  2.   MailMessage emailMessage = new MailMessage("fromAccount@gmail.com", "toAccount@gmail.com", "test email", "testing email");
  3.   SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
  4.   mailClient.EnableSsl = true;
  5.   mailClient.UseDefaultCredentials = false;
  6.   mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
  7.   System.Net.NetworkCredential myCredentials = new System.Net.NetworkCredential("theGmailAccount@gmail.com", "theGmailAccountPassword");
  8.   mailClient.Credentials = myCredentials;
  9.   mailClient.Send(emailMessage);
  10.   MessageBox.Show("Sent");
  11. } catch (Exception ex) {
  12.   MessageBox.Show(ex.Message);
  13. }
-Frinny
Dec 12 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.