How to send email with attachment from C#

C# simplifies network programming in .Net framework. C# describes various protocols using communication programming like Socket communications , SMTP mail , UDP , URL etc. The System.Net classes uses to communicate with other applications by using the HTTP, TCP, UDP, Socket etc. In the previous program we saw how to SMTP email from C# describes how to send an email with text body . Here we are sending an email with an attachment.

The following C# source code shows how to send an email with an attachment from a Gmail address . The Gmail SMTP server name is and the port using send mail is 587 . Here using NetworkCredential for password based authentication.

using System;
using System.Windows.Forms;
using System.Net.Mail;

namespace WindowsApplication1
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("");
                mail.From = new MailAddress("");
                mail.Subject = "Test Mail - 1";
                mail.Body = "mail with attachment";

                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment("your attachment file");

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                MessageBox.Show("mail Send");
            catch (Exception ex)

You have to provide the necessary information like your gmail username and password etc. (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About