Click here to Skip to main content
Click here to Skip to main content

Create Emails and send using your .Net Code

By , 15 May 2006
Rate this:
Please Sign up or sign in to vote.
<!-- @ Author: Prabhakar Manikonda -->
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)

About the Author

Prabhakar Manikonda
Architect IBM Sandton South Africa
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 PinmemberTurner K James17-Dec-10 10:22 
Questionhow to open outlook express in asp.net PingroupMohit329-Mar-10 5:25 
QuestionCheck sent email status Pinmemberfirsan16-Aug-09 0:06 
QuestionProblems with Authentication PinmemberMember 477445511-Dec-07 6:49 
QuestionWhy Use DotNetOpen Mail rather Microsoft API ? Pinmemberneelima manikonda3-Sep-06 20:29 
AnswerBecause System.Net.Mail sucks... [modified] PinmemberMarkus Wolters8-Feb-07 7:39 
QuestionWhy Duplicate what .NET Provides? PinmemberBrian Leach9-May-06 7:15 
AnswerRe: Why Duplicate what .NET Provides? PinmemberSigurd Johansen10-May-06 6:12 
AnswerRe: Why Duplicate what .NET Provides? PinmemberPrabhakar Manikonda14-May-06 21:16 
GeneralRe: Why Duplicate what .NET Provides? PinmemberBrian Leach15-May-06 6:05 
GeneralRe: Why Duplicate what .NET Provides? PinmemberPrabhakar Manikonda15-May-06 19:13 
AnswerRe: Why Duplicate what .NET Provides? Pinmembermikebridge24-May-06 9:10 
GeneralRe: Why Duplicate what .NET Provides? PinmemberBrian Leach24-May-06 12:23 
QuestionHow to configure SmtpServer? Pinmembergyokusei3-May-06 16:29 
AnswerRe: How to configure SmtpServer? PinmemberPrabhakar Manikonda14-May-06 21:17 

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

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

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