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.
1 comments:
For Attachment Location for IIS Server or Web Server Location Mention as below
string strtpath = System.Web.HttpContext.Current.Server.MapPath("~");
Path.GetFullPath(strtpath).Replace(@"/", @"//");
System.Net.Mail.Attachment attachment1;
attachment1 = new System.Net.Mail.Attachment(strtpath + "/assets/images/Benefits.jpg");
attachment1.Name = "Benefits.jpg";
message.Attachments.Add(attachment1);
Post a Comment