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 smtp.gmail.com and the port using send mail is 587 . Here using NetworkCredential for password based authentication.
public partial class Form1 : Form
private void button1_Click(object sender, EventArgs e)
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("firstname.lastname@example.org");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
attachment = new System.Net.Mail.Attachment("your attachment file");
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
catch (Exception ex)
You have to provide the necessary information like your gmail username and password etc.