473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,394 developers and data experts.

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 17426
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

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

Similar topics

0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.