How to Send Mail in asp.net C# ?
How to Send Mail To Multiple Address in C#?
How to Send Mail Attachment in C#?
Here I am going to Show How to Send Mail in Asp.net C#
Step-1:
First Add the following Namespace to your Code
using System.Net.Mail;
using System.Text;
using System.Net.Mime;
Step -2:
Write the following Code in Your Code File
private void CreateMail()
{
MailMessage email = BuildMail();
SendEMail(email);
}
How to Send Mail To Multiple Address in C#?
How to Send Mail Attachment in C#?
Here I am going to Show How to Send Mail in Asp.net C#
Step-1:
First Add the following Namespace to your Code
using System.Net.Mail;
using System.Text;
using System.Net.Mime;
Step -2:
Write the following Code in Your Code File
private void CreateMail()
{
MailMessage email = BuildMail();
SendEMail(email);
}
private MailMessage BuildMail()
{
string from, to, bcc, cc, subject, body;
from = "yourgmailid";//Email Address of Sender
to = "funkybik@gmail.com,guycrispy@gmail.com,bikram.sahoo@gmail.com";
{
string from, to, bcc, cc, subject, body;
from = "yourgmailid";//Email Address of Sender
to = "funkybik@gmail.com,guycrispy@gmail.com,bikram.sahoo@gmail.com";
//Email Address of Receiver
bcc = "";
cc = "";
subject = "This is to Test wether mail configuration is working or not.";
bcc = "";
cc = "";
subject = "This is to Test wether mail configuration is working or not.";
//Provides the physical location to the curreent folder
string filePath = Server.MapPath("~") + "\\";
string fileName = filePath + "Logo.JPG";//Contains the path to image
StringBuilder sb = new StringBuilder();
sb.Append("Hi User,<br/>");
sb.Append("This is a test email. How you doing? :p .<br/>");
sb.Append("<br/>");
sb.Append("Thanking you<br/>");
sb.Append("dotnetdoctorbikram.blogspot.com");
body = sb.ToString();
AlternateView avw = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
LinkedResource linkedRes = new LinkedResource(fileName);
linkedRes.ContentId = "image1";
linkedRes.ContentType.Name = fileName;
avw.LinkedResources.Add(linkedRes);
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
if (to.Contains(","))
{
string[] recepient = to.Split(',');
for (int i = 0; i < recepient.Length; i++)
{
mail.To.Add(new MailAddress(recepient[i]));
}
}
else
{
mail.To.Add(new MailAddress(to));
}
if (!string.IsNullOrEmpty(bcc))
{
mail.Bcc.Add(new MailAddress(bcc));
}
if (!string.IsNullOrEmpty(cc))
{
mail.CC.Add(new MailAddress(cc));
}
mail.Subject = subject;
mail.Body = body;
mail.AlternateViews.Add(avw);
mail.IsBodyHtml = true;
return mail;
}
private void SendEMail(MailMessage mail)
{
SmtpClient client= new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("yourgmailpassword","yourgmailid");
try
{
client.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
}
StringBuilder sb = new StringBuilder();
sb.Append("Hi User,<br/>");
sb.Append("This is a test email. How you doing? :p .<br/>");
sb.Append("<br/>");
sb.Append("Thanking you<br/>");
sb.Append("dotnetdoctorbikram.blogspot.com");
body = sb.ToString();
AlternateView avw = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
LinkedResource linkedRes = new LinkedResource(fileName);
linkedRes.ContentId = "image1";
linkedRes.ContentType.Name = fileName;
avw.LinkedResources.Add(linkedRes);
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
if (to.Contains(","))
{
string[] recepient = to.Split(',');
for (int i = 0; i < recepient.Length; i++)
{
mail.To.Add(new MailAddress(recepient[i]));
}
}
else
{
mail.To.Add(new MailAddress(to));
}
if (!string.IsNullOrEmpty(bcc))
{
mail.Bcc.Add(new MailAddress(bcc));
}
if (!string.IsNullOrEmpty(cc))
{
mail.CC.Add(new MailAddress(cc));
}
mail.Subject = subject;
mail.Body = body;
mail.AlternateViews.Add(avw);
mail.IsBodyHtml = true;
return mail;
}
private void SendEMail(MailMessage mail)
{
SmtpClient client= new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("yourgmailpassword","yourgmailid");
try
{
client.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
}
No comments:
Post a Comment