You can send email from C# using MailMessage class and also send attachments in the email. In this example i have provided one folder and will add all files in that folder as attachments to the email.
You can also check "Send Email using Gmail from C#" and "Send Email by requesting delivery/read receipt from C#"
Please find below C# source code, that shows how to send email with attachments from C# using SMTP server.
I have created sample windows application and added button = btnSendEmailWithAttachment, in this button click event the below code added.
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;
namespace SampleWindowsApp
{
/// <summary>
/// Send Email Form
/// </summary>
/// <seealso cref="System.Windows.Forms.Form" />
public partial class SendEmailForm : Form
{
/// <summary>
/// Initializes a new instance of the <see cref="SendEmailForm"/> class.
/// </summary>
public SendEmailForm()
{
InitializeComponent();
}
/// <summary>
/// Handles the Click event of the btnSendEmailWithAttachment control.
/// Author = Himasagar Kutikuppala
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void btnSendEmailWithAttachment_Click(object sender, EventArgs e)
{
try
{
//Create the object of MailMessage
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("FromEmail@domain.com", "FromDisplayName"); //From Email Id & Display name
mailMessage.Subject = "Test Email"; //Subject of Email
mailMessage.Body = "This is the test email body text"; //body or message of Email
mailMessage.IsBodyHtml = true;
mailMessage.Priority = MailPriority.High; //if you want to send High importance email
//You can add either multiple To,CC,BCC or single emails
mailMessage.To.Add(new MailAddress("ToEmailId1@domain.com;ToEmailId2@domain.com;ToEmailId3@domain.com;")); //adding multiple TO Email Id
mailMessage.CC.Add(new MailAddress("CCEmailId1@domain.com;CCEmailId2@domain.com;CCEmailId3@domain.com;")); //Adding Multiple CC email Id
mailMessage.Bcc.Add(new MailAddress("BCCEmailId1@domain.com;BCCEmailId2@domain.com;BCCEmailId3@domain.com;")); //Adding Multiple BCC email Id
//Add Attachments, here i gave one folder name, and it will add all files in that folder as attachments, Or if you want only one file also can add
string attachmentsPath = @"C:\Himasagar";
if (Directory.Exists(attachmentsPath))
{
string[] files = Directory.GetFiles(attachmentsPath);
foreach (string fileName in files)
{
Attachment file = new Attachment(fileName);
mailMessage.Attachments.Add(file);
}
}
//Assign the SMTP address and port
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.domain.net";
smtp.Port = 25;
smtp.EnableSsl = false; //If required make it true
//Network and security related credentials
NetworkCredential networkCredential = new NetworkCredential ();
networkCredential.UserName = "user name (email id)";
networkCredential.Password = "password";
networkCredential.Domain = "Domain Name";
smtp.Credentials = networkCredential;
//Finally send Email
smtp.Send(mailMessage);
MessageBox.Show("The email has been sent successfully.");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}