send email (localhost and server)
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;
using System.Data;
using System.Web.UI.HtmlControls;
public partial class mailtofrom : System.Web.UI.Page
{
SqlConnection cn;
SqlCommand cm;
SqlDataAdapter adp;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ }
}
protected void send_Click(object sender, EventArgs e)// sever code
{
// server code
MailMessage mail = new MailMessage();//object created
//Setting From , To and CC
mail.To.Add(TextBox1.Text);
mail.From=new MailAddress("email id ");
mail.Subject="thanks for Registartion";
mail.Body="I Am FINE";
mail.IsBodyHtml=true;// it will convert msg format in html
//smtp object
SmtpClient smtp= new SmtpClient();
smtp.Host="smtp.gmail.com";
smtp.Credentials=new System.Net.NetworkCredential("email","pass");//email id & password
smtp.EnableSsl=true;// server security licence will true then only mail will send
smtp.Send(mail);//send function with mail object pass...which will send mail.
}
protected void SENDTO_Click(object sender, EventArgs e)// local data
{
// Local HOst
string fromEmail = "email";
MailMessage mailmessage = new MailMessage(fromEmail, TextBox1.Text, TextBox3.Text, TextBox2.Text);
SmtpClient smtpclient = new SmtpClient("email", 587);
smtpclient.EnableSsl = true;
smtpclient. UseDefaultCredentials = false;
smtpclient.Credentials = new System.Net.NetworkCredential("email", "pass");
try
{
smtpclient.Send(mailmessage);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}