Click here to Skip to main content
15,884,388 members
Articles / Web Development / ASP.NET
Article

Create Emails and send using your .Net Code

Rate me:
Please Sign up or sign in to vote.
3.60/5 (36 votes)
15 May 2006CPOL1 min read 74.1K   49   15
Use Simple ASP.Net code to send emails.
Title      :  Create Emails and send using your .Net Code
Author     :  Prabhakar Manikonda
Email      :  <A href="mailto:email2prabhakar@gmail.comEnvironment">mailto:email2prabhakar@gmail.com
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

 

Introduction

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 http://sourceforge.net/projects/dotnetopenmail/

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("senderID@domainName.com");

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

/*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("smtpout.domainName.com");

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

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

/*Sent the message */
emailMessage.Send(smtpServer);

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 ! ");
    writer.Flush();
    
    /* 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*/
    emailMessage.AddMixedAttachment(attachment);
 
Using dotnetopenmail is very easy and I hope that it will be useful to you.

History

Version 1.0 Release 2006

License

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


Written By
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

Comments and Discussions

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