Click here to Skip to main content
13,898,301 members
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 1 May 2006
Licenced CPOL

Create Emails and send using your .Net Code

, 15 May 2006
Rate this:
Please Sign up or sign in to vote.
Use Simple ASP.Net code to send emails.
<!-- @ Author: Prabhakar Manikonda -->
Title      :  Create Emails and send using your .Net Code
Author     :  Prabhakar Manikonda
Email      :  <A href="mailto:email2prabhakar@gmail.comEnvironment">
Environment</A>: ASP.NET 1.1 
Keywords   : Email Client, send emails,attach files. 
Level      : Intermediate
Description: Use Simple ASP.Net code to send emails.     
Section    : Web Technology



Sending an email is easy we can do it using the general approach that is either using System.web.mail [ASP.NET 1.1] or System.Net.mail [ASP.NET 2.0], but there is another third-party library which we can use if we want, some people do not use this as they say it will yield compatibility issues, but it has been tested and it is in use, even i will prefer to use the Microsoft API rather that DotNetOpenMail API but we being engineer has to know each and every library which has high frequency of usage, The DotNetOpenMail - an SMTP client library for .net, written in C#. This will allow you to minimize your code to create basic text and html emails with attachments that can be sent via an SMTP server.

you can get this library from

It is a freely available open-source component makes it easy to create HTML and plain text email with file attachments without needing the System.Web.Mail library.

Using the code

Just add the DotNetOpenMail.dll to references and then write the below code

//Using Namespaces that are to be included

using DotNetOpenMail;
using DotNetOpenMail.Logging;
using DotNetOpenMail.Encoding;
using DotNetOpenMail.Resources;
using DotNetOpenMail.SmtpAuth;

Using DotNetOpenMail is very simple the below code will let you know this fact.

/*Create an EmailMessage Object */
EmailMessage emailMessage = new EmailMessage();

/*Add from Address to the emailMessage Object*/
emailMessage.FromAddress = new EmailAddress("");

/*Add to Address to the emailMessage Object*/
emailMessage.AddToAddress(new EmailAddress(""));

/*Add The Subject to the emailMessage Object*/
emailMessage.Subject = "Subject of the Message";

/*Set some body text to HtmlPart using HtmlAttachment*/
emailMessage.HtmlPart = new HtmlAttachment("<html><body><p>Write Down some BODY-Text here</p></body></html>");

      /*Now initilize the smtpServer object to default Out Going smtp address */
      SmtpServer smtpServer=new SmtpServer("");

/* Give your valid id and password for that email server*/
smtpServer.SmtpAuthToken=new SmtpAuthToken("", "password");

/*Set the content type*/
emailMessage.ContentType = "TEXT/HTML";

/*Sent the message */

Response.Write (" Email Successfully Sent");

Attaching The Files to your Message

Here is the sample code to attach a sample text file

    /* Create a memory stream object */        
    MemoryStream memStream = new MemoryStream();
    /* Create a StreamWriter  object */        
    StreamWriter writer = new StreamWriter(memStream);
    /* Write some data into the StreamWriter object */        
    writer.WriteLine("  HELLO WORLD ! ");
    /* let the memory stream point backt to the BOF(begining of  file) */        
    memStream.Seek(0, SeekOrigin.Begin);

    /*Create a FileAttachment Object and attach MemoryStream object to it*/
    FileAttachment attachment = new FileAttachment(new StreamReader(memStream));
    /*Set the Fil name, Content Type, Char Set paramenter of FileAttachment Object*/
    attachment.FileName = "Hello.txt";
    attachment.CharSet = System.Text.Encoding.ASCII;
    attachment.ContentType = "text/plain";
    /*Add the attachment object to emailMessage object*/
Using dotnetopenmail is very easy and I hope that it will be useful to you.


Version 1.0 Release 2006


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Prabhakar Manikonda
Architect Providence Software
South Africa South Africa
Prabhakar Manikonda
MS CS, M.A SW, MCSD.NET, MCTS Sharepoint 2007, 3.0 Services

Proven track record with seeing successful projects through to completion. Excellent team player and completer-finisher. Specialist in methodology, architecture,object-oriented design, and project management. Plan and manage workload, monitoring and resolving programming issues Transform requirements into architectural, Publishes white papers and articles in software development and runs software consulting & recruiting

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Turner K James17-Dec-10 10:22
memberTurner K James17-Dec-10 10:22 
Questionhow to open outlook express in Pin
Mohit329-Mar-10 5:25
groupMohit329-Mar-10 5:25 
QuestionCheck sent email status Pin
firsan16-Aug-09 0:06
memberfirsan16-Aug-09 0:06 
QuestionProblems with Authentication Pin
Member 477445511-Dec-07 6:49
memberMember 477445511-Dec-07 6:49 
QuestionWhy Use DotNetOpen Mail rather Microsoft API ? Pin
neelima manikonda3-Sep-06 20:29
memberneelima manikonda3-Sep-06 20:29 
AnswerBecause System.Net.Mail sucks... [modified] Pin
Markus Wolters8-Feb-07 7:39
memberMarkus Wolters8-Feb-07 7:39 
QuestionWhy Duplicate what .NET Provides? Pin
Brian Leach9-May-06 7:15
memberBrian Leach9-May-06 7:15 
AnswerRe: Why Duplicate what .NET Provides? Pin
Sigurd Johansen10-May-06 6:12
memberSigurd Johansen10-May-06 6:12 
AnswerRe: Why Duplicate what .NET Provides? Pin
Prabhakar Manikonda14-May-06 21:16
memberPrabhakar Manikonda14-May-06 21:16 
GeneralRe: Why Duplicate what .NET Provides? Pin
Brian Leach15-May-06 6:05
memberBrian Leach15-May-06 6:05 
GeneralRe: Why Duplicate what .NET Provides? Pin
Prabhakar Manikonda15-May-06 19:13
memberPrabhakar Manikonda15-May-06 19:13 
AnswerRe: Why Duplicate what .NET Provides? Pin
mikebridge24-May-06 9:10
membermikebridge24-May-06 9:10 
GeneralRe: Why Duplicate what .NET Provides? Pin
Brian Leach24-May-06 12:23
memberBrian Leach24-May-06 12:23 
QuestionHow to configure SmtpServer? Pin
gyokusei3-May-06 16:29
membergyokusei3-May-06 16:29 
AnswerRe: How to configure SmtpServer? Pin
Prabhakar Manikonda14-May-06 21:17
memberPrabhakar Manikonda14-May-06 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 16 May 2006
Article Copyright 2006 by Prabhakar Manikonda
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid