SIGN UP MEMBER LOGIN:    
ARTICLE

How to send Email with an Attachment in C#

Posted by Rajendra Tripathy Articles | How do I August 01, 2009
The code snippet in this article shows how to send emails with attachments in C#.
Reader Level:

Before you use the code below, you must add a reference to the System.Web to your project.

Right click on References on your project name in Visual Studio Solution Explorer, select Add Reference, then choose .NET and then select System.Web from the list and click OK. This will add System.Web reference to your project.

Now, you must import System.Web.Mail namespace to your project by adding this line to your code at the top of the class with other namespace references.

using Sytem.Web.Mail;


Let's say, in my Windows application, I have two buttons, Attach and Send. Here is the code that should be written on Attach and Send button click event handler.

On Attach button click event handler, I simply browse a file that will be attached to the mail.

On Send button click event handler, I create a MailMessage object, set its From, To, Subject, and Body properties. After that, I create an SmtpMail object and set SmtpServer to my server name. In the end, I call Send message by passing MailMessage to this method.


private void btnSend_Click(object sender, EventArgs e) //writing code in Button btnSend

{

    try

    {

        //MailMessage class are used to construct e-mail messages

        MailMessage message = new MailMessage();

 

        // Collaboration Data Objects (CDO) library allows you to access the Global Address

        // List and other server objects, in addition to the contents of mailboxes

 

        message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);               

        //basic authentication

 

        message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "From@gmail.com");

        //set your username here

 

        message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "From-Password");

        //set your password here

 

        message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "True");

 

        message.From = "From@gmail.com";

 

        message.To = "to@gmail.com";

 

        message.Subject = txtSubject.Text;

 

        message.Body = txtMessage.Text;

 

        if (!txtAttach.Text.Equals("")) //adding attachments.

        {

            MailAttachment attach = new MailAttachment(txtAttach.Text);

            if (attach != null)

            {

                message.Attachments.Add(attach);

            }

        }

 

        SmtpMail.SmtpServer = "smtp.gmail.com";

        //The real server goes here

        SmtpMail.Send(message);

        MessageBox.Show("Your message has been sent.", "Mail Sent",

            MessageBoxButtons.OK, MessageBoxIcon.None);

 

    }

 

    catch (Exception ex)

    {

        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,

            MessageBoxIcon.Error);

    }

}//btnSend

 

 

//I have to attach a file so,I created a button called btnAttach.

private void btnAttach_Click_1(object sender, EventArgs e)

{

    DialogResult clicked = OpenattachDialog.ShowDialog();

    if (clicked.Equals(DialogResult.OK))            {

 

        txtAttach.Text = OpenattachDialog.FileName;

    }

}//btnAttach

 


Login to add your contents and source code to this article
Article Extensions
Contents added by Lakhbir chandel on Jun 17, 2011
share this article :
post comment
 

Hi, I could nt get u properly? Will u plz clarify ..

Posted by Rajendra Tripathy Sep 24, 2010

Dear Rajendra;

Can you please help me to certify the properties of an attached document as correct. I have received two attachment from same e-mail sender with two different e-mails on same date 20.11.07. There is a dispute with the properties. Can you help me or guide me what is the best I can do
Thanks in advance
Bhavin

Posted by Bhavinchandra Purohit Sep 23, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
Become a Sponsor