65.9K
CodeProject is changing. Read more.
Home

Create Emails and send using your .Net Code

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.60/5 (36 votes)

May 2, 2006

CPOL

1 min read

viewsIcon

75322

Use Simple ASP.Net code to send emails.

Title      :  Create Emails and send using your .Net Code
Author     :  Prabhakar Manikonda
Email      :  "mailto:email2prabhakar@gmail.comEnvironment">mailto:email2prabhakar@gmail.com
Environment: 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