Friday 28 June 2013

Sending Mail in C# Asp.net

Send mail using below code in Asp.net C#



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


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


}
protected void BtnSend_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage obj = new System.Net.Mail.MailMessage();
obj.From = new System.Net.Mail.MailAddress(TxtFrm.Text);
if (TxtTo.Text.Contains(","))
{
string[] to = TxtTo.Text.Split(',');
foreach (string r in to)
{
obj.To.Add(new System.Net.Mail.MailAddress(r));
}
}
else
{
obj.To.Add(new System.Net.Mail.MailAddress(TxtTo.Text));
}
obj.CC.Add(new System.Net.Mail.MailAddress(TxtCc.Text));
obj.Bcc.Add(new System.Net.Mail.MailAddress(TxtBCc.Text));
obj.Subject = TxtSub.Text;
obj.IsBodyHtml = true;
System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream,FileUpload1.PostedFile.FileName,FileUpload1.PostedFile.ContentType);
obj.Attachments.Add(att);
obj.Body = Txtbdy.Text;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("1.0.0.0", 25);
try
{
smtp.Send(obj);
}
catch (Exception ex)
{
Response.Redirect(ex.Message);
}
}
}

No comments:

Post a Comment