Wednesday, March 30, 2016
Sending an Email With and Without Attachments Using C#
Sending an Email is important thing for every Dot net developer working in web applications project. Sometimes required to need send with attachments in email. Sending an email with attachment is easy and simple in C# to send. Follow the below steps to implement in your projects.
In this post I am discussing to send an email in two different ways
1. Send e-mail via SMTP (Gmail) using C#.
2. Send e-mail via Host Address using C#.
Note: Add these namespace in your code behind or Controller in C#.
using System.Net;
using System.Net.Mail;
1. Send e-mail via SMTP (Gmail) using C#.
Function for Without Attachment
protected void sendEmail()
{
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("from_address@gmail.com");
message.To.Add(new MailAddress("to_address@test.med.ca"));
message.CC.Add(new MailAddress("copy@domain.com"));
message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here
message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text; //Body Of Email Content
SmtpClient client = new SmtpClient("smtp.gmail.com"));
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
client.EnableSsl = true;
client.Send(message);
}
}
Function for With Attachment
protected void sendEmail()
{
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("from_address@gmail.com");
message.To.Add(new MailAddress("to_address@test.med.ca"));
message.CC.Add(new MailAddress("copy@domain.com"));
message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here
//Attachment goes here
System.Net.Mail.Attachment attach;
attach = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
attach.Name = "map.png"; // Here You can describe your filename with or without extension
message.Attachments.Add(attach);
message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text; //Body Of Email Content
SmtpClient client = new SmtpClient("smtp.gmail.com"));
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
client.EnableSsl = true;
client.Send(message);
}
}
Function for With Multiple Attachments
protected void sendEmail()
{
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("from_address@gmail.com");
message.To.Add(new MailAddress("to_address@test.med.ca"));
message.CC.Add(new MailAddress("copy@domain.com"));
message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here
//Attachment 1
System.Net.Mail.Attachment attach;
attach = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
attach.Name = "map.png"; // Here You can describe your filename with or without extension
message.Attachments.Add(attach);
//Attachment 2
System.Net.Mail.Attachment attach2;
Attach2 = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
Attach2.Name = "map.png"; // Here You can describe your filename with or without extension
message.Attachments.Add(attach2);
message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text; //Body Of Email Content
SmtpClient client = new SmtpClient("smtp.gmail.com"));
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
client.EnableSsl = true;
client.Send(message);
}
}
2. Send e-mail via Host Address using C#.
Function for Without Attachment
protected void sendEmail()
{
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("from_address@gmail.com");
message.To.Add(new MailAddress("to_address@test.med.ca"));
message.CC.Add(new MailAddress("copy@domain.com"));
message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here
message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text; //Body Of Email Content
SmtpClient client = new SmtpClient();
client.Host = "10.10.18.10"; // Mention your server host IP Address Here
client.EnableSsl = true;
client.Send(message);
}
}
Function for With Attachment
protected void sendEmail()
{
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("from_address@gmail.com");
message.To.Add(new MailAddress("to_address@test.med.ca"));
message.CC.Add(new MailAddress("copy@domain.com"));
message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here
//Attachment goes here
System.Net.Mail.Attachment attach;
attach = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
attach.Name = "map.png"; // Here You can describe your filename with or without extension
message.Attachments.Add(attach);
message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text; //Body Of Email Content
SmtpClient client = new SmtpClient();
client.Host = "10.10.18.10"; // Mention your server host IP Address Here
client.EnableSsl = true;
client.Send(message);
}
}
Function for send an email Multiple Attachments Using c#
protected void sendEmail()
{
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("from_address@gmail.com");
message.To.Add(new MailAddress("to_address@test.med.ca"));
message.CC.Add(new MailAddress("copy@domain.com"));
message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here
//Attachment 1
System.Net.Mail.Attachment attach;
attach = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
attach.Name = "map.png"; // Here You can describe your filename with or without extension
message.Attachments.Add(attach);
//Attachment 2
System.Net.Mail.Attachment attach2;
Attach2 = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
Attach2.Name = "map.png"; // Here You can describe your filename with or without extension
message.Attachments.Add(attach2);
message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text; //Body Of Email Content
SmtpClient client = new SmtpClient();
client.Host = "10.10.18.10"; // Mention your server host IP Address Here
client.EnableSsl = true;
client.Send(message);
}
}
Using above function call in any events to send an email. We can use these in Contact us Form and many more areas where ever required.
If you have any problem in coding comment it here. As soon as possible we will reply to you.