Popular Posts

Wednesday, 6 July 2011

Send Email In ASP .Net :


Namespaces used :
using System.Net.Mail
using System.Net.Mime;

WithOut Image

MailMessage mail = new MailMessage();
mail.To.Add("xyz@gmail.com");
mail.From = new MailAddress("abc@gmail.com");
mail.Subject = "Your text for subject";
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
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
 ("abc@gmail.com", "xxxxxx");  //  ("YourGmailID@gmail.com", "YourGmailPassword");
smtp.EnableSsl = true;
smtp.Send(mail);

With Image:

MailMessage mail = new MailMessage();
mail.To.Add("xyz@gmail.com");
mail.From = new MailAddress("abc@gmail.com");
mail.Subject = "Test with Image";
string Body = "<b>Welcome to send mail with image demo!!</b><br<img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >";

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\ss.jpg", "image/png");
imagelink.ContentId = "imageId";
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink);
mail.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.Send(mail);

optional:
 Note:SmtpClient mailSender = new SmtpClient(ConfigurationManager.AppSettings["MyCustomId"]); // use this in the Production Server. I have specified my email server in the web.config file

Web.Config

  <appSettings>
    <add key="SMTP" value="smtp.gmail.com"/>
    <add key="FROMEMAIL" value="xxx@gmail.com"/>
    <add key="FROMPWD" value=""/>   
  </appSettings>


With Attachment:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtTo.Text);
        mail.From = new MailAddress(txtFrom.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtMessage.Text;
        mail.IsBodyHtml = true;

        //Attach file using FileUpload Control and put the file in memory stream
        if (FileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("YourGmailID@gmail.com", "YourGmailPassword");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);

    }
}




No comments:

Post a Comment