Home > ASP.NET > Sending email in asp.net

Sending email in asp.net


Sending email’s from web applications is a very common requirement in most projects irrespective of any language site is deveoped .This article explores how to send Emails in ASP.NET  with attachments with gmail account and using custome host address.

Mail Sending using custome host address

using System.Net.Mail;
MailMessage mail = new MailMessage();
mail.To.Add("sToEmail");
mail.From = new MailAddress("sFromEmail");
if (sCCEmail.ToString() != "")
{
mail.CC.Add(sCCEmail");
}
if (sBCCEmail.ToString() != "")
{
mail.Bcc.Add("sBCCEmail");
}
mail.Subject = "put whatever subject you want to give"
mail.Body = "Your complete message you wanna share ";
mail.IsBodyHtml = true/false
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.abc.com" //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential(userName, password); //Or your Smtp Email ID and Password

smtp.EnableSsl = true/false
mail.Priority = System.Net.Mail.MailPriority.High;
mail.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnFailure;

// want attachment in mail
string fileAttach = Server.MapPath("myEmails") + "\\Mypic.jpg";
Attachment attach = new Attachment(fileAttach);
mail .Attachments.Add(attach);
smtp.Send(mail);

Sending Email Using Gmail In ASP.NET.

if you don’t have a working smtp server to send mails than sending
e-mail with Gmail is best option.Otherwise use of it should be avoided
because it has some limitation like.

using System.Net.Mail;
MailMessage mail = new MailMessage();
mail.To.Add("toemailAddress");
mail.From = new MailAddress("yourid@gmail.com");
mail.Subject = "put whatever subject you want to give";
string Body = " ";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);

If your are getting error mentioned below “The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.”

than you need to check your Gmail username and password.

If you are behind proxy Server then you need to write below mentioned code in your web.config file

<system.net>
<defaultProxy>
<proxy proxyaddress="ProxyIpaddress"/>
</defaultProxy>
</system.net>

If you are still having problems them try changing port number to 587

smtp.Host = "smtp.gmail.com,587";

If you still having problems then try changing code as mentioned below

SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = False;
smtp.Credentials = new System.Net.NetworkCredential
("emailid@gmail.com","GmailPassword");
smtp.EnableSsl = true;
smtp.Send(mail);

If it help you solving your problem then give comment or if any wrong in this then let me know i will rectify.

Advertisements
Categories: ASP.NET
  1. September 3, 2013 at 5:09 AM

    its really helpful for me. Great.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: