SIGN UP MEMBER LOGIN:    
ARTICLE

Send an Email with attachment from Outlook, Yahoo, Hotmail, AOL and Gmail

Posted by Jawed MD Articles | ASP.NET Programming March 07, 2011
Sending an email with attachment through Outlook, Yahoo, AOL, Hotmail and Gmail.
Reader Level:
Download Files:
 

Introduction
 
Using this code snippet the user would be able to send an email with attachment through Outlook, Yahoo, AOL, HOTMAIL and Gmail.
 
Background
 
Already loads of information is available on the internet for this purpose. But not all the code is placed in one place. So here is my small effort to accumulate all the code in one place and that is nothing better than this place. Hopefully it will be helpful for the beginners or whoever in need of it. 
 
Using the code
 
Namespace
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;
 
Block Diagram (SMTP Setting for various client):

 
Below is the Block diagram for the SMTP settings I have used to send out an email from various client providers.

sendemail1.gif 
 
Wherever needed I have explained the code in details. The codes are self explanatory, any way for the same I have included comments. So here is the code snippet to achieve your purpose. 
 
1. Using Outlook:
 
To send an email using outlook, we need to add a reference to the dynamic link library for Outlook which is called Microsoft.Office.Interop.Outlook.dll.
 
For that follow the steps below.
 
1. Go to your solution explorer 
 
2. Click on add a reference
 
3. Click on .Net Tab
 
4. Go through the DLL and select Microsoft.Office.Interop.Outlook.dll correctly.
 
sendemail2.gif
 
5. When you have selected the correct reference you select the "OK" button and this reference will be added to your project under references. 
 
sendemail3.gif
 
 
6. Now we need to add a reference in our class to the Outlook reference we have added to the project in our previous example. 
 
using Outlook = Microsoft.Office.Interop.Outlook;
 
And finally the code would look something like this: 
 
//method to send email to outlook
public void sendEMailThroughOUTLOOK()
{
    try
    {
    // Create the Outlook application.
    Outlook.Application oApp = new Outlook.Application();
    // Create a new mail item.
    Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    // Set HTMLBody.
    //add the body of the email
    oMsg.HTMLBody = "Hello, Jawed your message body will go here!!";
    //Add an attachment.
    String sDisplayName = "MyAttachment";
    int iPosition = (int)oMsg.Body.Length + 1;
    int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
    //now attached the file
    Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);
    //Subject line
    oMsg.Subject = "Your Subject will go here.";
    // Add a recipient.
    Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
    // Change the recipient in the next line if necessary.
    Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("jawed.ace@gmail.com");
    oRecip.Resolve();
    // Send.
    oMsg.Send();
    // Clean up.
    oRecip = null;
    oRecips = null;
    oMsg = null;
    oApp = null;
     }//end of try block
    catch (Exception ex)
    {
    }//end of catch
}//end of Email Method
 
2. Using HOTMAIL:
 
To send an email using HotMail, we need to add a reference to the dynamic link library for Hotmail/Gmail/AOL/Yahoo which is called System.Net.Mail.
 
For the same follow the steps below:
 
1. Go to solution explorer of your project
 
2. Select add a reference
 
3. Click on .Net Tab
 
4. Go through the DLL and select System.Net.Mail.
 
5. When you have selected the correct reference you select the "OK" button and this reference will be added to your project under references.
 
6. Now we need to add a reference in our class to the Hotmail/Gmail/AOL/Yahoo reference we have added to the project. 
 
using System.Net.Mail;
 
Note:  The HOTMAIL SMTP Server requires an encrypted connection (SSL) on port 25.
 
And finally the code would look something like this: 
 
//method to send email to HOTMAIL
public void sendEMailThroughHotMail()
{
    try
    {
        //Mail Message
        MailMessage mM = new MailMessage();
        //Mail Address
        mM.From = new MailAddress("sender@hotmail.com");
        //receiver email id
        mM.To.Add("rcver@gmail.com");
        //subject of the email
        mM.Subject = "your subject line will go here";
        //deciding for the attachment
        mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
        //add the body of the email
        mM.Body = "Body of the email";
        mM.IsBodyHtml = true;
        //SMTP client
        SmtpClient sC = new SmtpClient("smtp.live.com");
        //port number for Hot mail
        sC.Port = 25;
        //credentials to login in to hotmail account
        sC.Credentials = new NetworkCredential("sender@hotmail.com","HotMailPassword");
        //enabled SSL
        sC.EnableSsl = true;
        //Send an email
        sC.Send(mM);
    }//end of try block
    catch (Exception ex)
    {
    }//end of catch
}//end of Email Method HotMail
 
3. Using Yahoo!
 
//Method to send email from YAHOO!!
public void sendEMailThroughYahoo()
{
    try
    {
        //mail message
        MailMessage mM = new MailMessage();
        //Mail Address
        mM.From = new MailAddress("sender@yahoo.com");
        //emailid to send
        mM.To.Add("recvEmailid@gmail.com");
        //your subject line of the message
        mM.Subject = "your subject line will go here.";
        //now attached the file
        mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
        //add the body of the email
        mM.Body = "Your Body of the email.";
        mM.IsBodyHtml = false;
        //SMTP
        SmtpClient SmtpServer = new SmtpClient();
        //your credential will go here
        SmtpServer.Credentials = new System.Net.NetworkCredential("sender@yahoo.com""password");
        //port number to login yahoo server
        SmtpServer.Port = 587;
        //yahoo host name
        SmtpServer.Host = "smtp.mail.yahoo.com";
        //Send the email
        SmtpServer.Send(mM);
    }//end of try block
    catch (Exception ex)
    {
    }//end of catch
}//end of Yahoo Email Method
 
4. Using AOL:
 
//Method to send email from YAHOO!!
public void sendEMailThroughAOL()
{
    try
    {
         //mail message
        MailMessage mM = new MailMessage();
        //Mail Address
        mM.From = new MailAddress("sender@aol.com");
        //emailid to send
        mM.To.Add("recvEmailid@gmail.com");
        //your subject line of the message
        mM.Subject = "your subject line will go here.";
        //now attached the file
        mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
        //add the body of the email
        mM.Body = "Your Body of the email.";
        mM.IsBodyHtml = false;
        //SMTP
        SmtpClient SmtpServer = new SmtpClient();
        //your credential will go here
        SmtpServer.Credentials = new System.Net.NetworkCredential("sender@aol.com""AOLpassword");
        //port number to login yahoo server
        SmtpServer.Port = 587;
        //yahoo host name
        SmtpServer.Host = "smtp.aol.com";
        //Send the email
        SmtpServer.Send(mM);
    }//end of try block
    catch (Exception ex)
    {
    }//end of catch
}//end of AOLEmail Method
 
5. Using Gmail:
 
Note: The GMAIL SMTP Server requires an encrypted connection (SSL) on port 487.
 
//method to send email to Gmail
public void sendEMailThroughGmail()
{
    try
    {
        //Mail Message
        MailMessage mM = new MailMessage();
        //Mail Address
        mM.From = new MailAddress("sender@gmail.com");
        //receiver email id
        mM.To.Add("rcver@gmail.com");
        //subject of the email
        mM.Subject = "your subject line will go here";
        //deciding for the attachment
        mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
        //add the body of the email
        mM.Body = "Body of the email";
        mM.IsBodyHtml = true;
        //SMTP client
        SmtpClient sC = new SmtpClient("smtp.gmail.com");
        //port number for Gmail mail
        sC.Port = 587;
        //credentials to login in to Gmail account
        sC.Credentials = new NetworkCredential("sender@gmail.com""GmailPassword");
        //enabled SSL
        sC.EnableSsl = true;
        //Send an email
        sC.Send(mM);
    }//end of try block
    catch (Exception ex)
    {
    }//end of catch
}//end of Email Method
 
Already I am using the above code snippet for my automation purpose and it's working fine for me!!
 
Thank you!!
 
Feel Free to provide your comments and suggestion.

Login to add your contents and source code to this article
share this article :
post comment
 

Thank you very much Mahesh for your comments!!

Posted by Jawed MD Mar 08, 2011

This is very useful Jawed. Thanks for sharing.

Posted by Mahesh Chand Mar 08, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor