473,505 Members | 14,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick Reference on how to send an email using .NET

Frinavale
9,735 Recognized Expert Moderator Expert
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 17440
Plater
7,872 Recognized Expert Expert
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 Recognized Expert Moderator Expert
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 Recognized Expert Expert
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 Recognized Expert Moderator Expert
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

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

Similar topics

0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.