Saturday, 4 February 2012

how to send mail in asp.net with attachment using smtp server

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;
using System.Web.Mail;
using System.IO;



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

}
protected void btn_Click(object sender, EventArgs e)
{

//MailMessage ms = new MailMessage();
//ms.From = tb7.Text;
//ms.To = "chinmaya.parija.68@gmail.com";
//ms.Subject = "Urgent";
//ms.BodyFormat = MailFormat.Html;
//if (fu.HasFile)
//{
// string[] ss = fu.FileName.Split('.');
// if (ss[1] == "pdf" || ss[1] == "doc" || ss[1] == "docx")
// {

// fu.SaveAs(Server.MapPath("~/resumes/" + fu.FileName.ToString()));
// ms.Body = "
Name : " + tb.Text + "
Location : " + tb1.Text + "
Qualification : " + tb2.Text + "
Experience Level : " + tb3.Text + "
Skills : " + tb4.Text + "
Domain Expertise : " + tb5.Text + "
Position Applied For : " + tb6.Text + "
Email Id : " + tb7.Text + "
Mobile Number : "+tb8.Text+"
";
// //ms.Attachments.Add(new MailAttachment(fu.PostedFile.FileName.ToString()));
// SmtpMail.Send(ms);
// ClientScript.RegisterClientScriptBlock(GetType(), "yy", "");
// }
// else
// ClientScript.RegisterClientScriptBlock(GetType(), "yy", "");

//}
try
{
MailMessage ms = new MailMessage();
ms.To.Add("chinmaya_68@rediffmial.com");
ms.Subject = "URGENT";
ms.From = new MailAddress("cparija2@gmail.com");
ms.Body = "
Name : " + tb.Text + "
Location : " + tb1.Text + "
Qualification : " + tb2.Text + "
Experience Level : " + tb3.Text + "
Skills : " + tb4.Text + "
Domain Expertise : " + tb5.Text + "
Position Applied For : " + tb6.Text + "
Email Id : " + tb7.Text + "
Mobile Number : " + tb8.Text + "
";
//ms.CC.Add("snehasish@swashconvergence.com");
//ms.Bcc.Add("chinmaya.parija.68@gmail.com");
string[] ss = fu.FileName.Split('.');
if (ss[1] == "pdf" || ss[1] == "doc" || ss[1] == "docx")
{
ms.Attachments.Add(new Attachment(fu.PostedFile.InputStream, fu.FileName));
}
else
ClientScript.RegisterClientScriptBlock(GetType(), "yy", "");
ms.IsBodyHtml = true;
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.gmail.com";
sc.Credentials = new System.Net.NetworkCredential("swash543@gmail.com", "Jagannath@1");
sc.EnableSsl = true;
sc.Port = 587;
sc.EnableSsl = true;
sc.Send(ms);
ClientScript.RegisterClientScriptBlock(GetType(), "yy", "");
}
catch (SmtpException se)
{
ClientScript.RegisterClientScriptBlock(GetType(), "yy", "");

}



}
}

No comments:

Post a Comment