How to send Email with an Attachment in C#

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", "[email protected]");

        //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 = "[email protected]";

 

        message.To = "[email protected]";

 

        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

 


Up Next
    Ebook Download
    View all
    Learn
    View all