Sending Mail with attachment from C# using Gmail SMTP Client

Posted by muralikrishnasurap-12704 under C# category on | Points: 40 | Views : 10853
Hi here i want to explain you how to send mail with attachment using C# class with Gmail SMPT client.
we can use using System.Net.Mail; name space which contains list of class like SmtpClient,MailAddressCollection,MailMessage and Attachement used for sending mail in C# code.

public void sendMail()
SmtpClient _SmtpClient = new SmtpClient("");
MailAddressCollection _MailAddressCollection = new MailAddressCollection();
MailMessage _message = new MailMessage();
_message.From = new MailAddress("");
_message.Body = "Body of the mail";
_message.Subject = "Sending mail from c# Code";
Attachment data = new Attachment("D:\\Attachement.xls");
_SmtpClient.Port = 587;
_SmtpClient.Credentials = new System.Net.NetworkCredential("", "*****");
_SmtpClient.EnableSsl = true;

Comments or Responses

Posted by: Biswarup90 on: 11/24/2011 Level:Starter | Status: [Member] | Points: 10
Window application:

using System;
using System.Windows.Forms; //new name space include
using System.Net.Mail; //for mail using this name space

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("you attachment file");

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

MessageBox.Show("mail Send");
catch (Exception ex)
Posted by: T.saravanan on: 11/26/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Murali & Biswarup Ghosh,

Kindly post your code inside the code tag.

Login to post response