3 May 2013

Send email code in c#.net



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.IO;
using System.Xml.Linq;

public partial class Mail_Welcome : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
    {
       SendMail(ToAddress);
     }

    protected void SendMail(string str)       //mail method
    {
        var fromAddress = "Mail Id";   // From address

        var toAddress = str;  //str means to address

        const string fromPassword = "Password";  //from password

        string subject = "Mail Subject";  //mail subject

        string body = "Mail Body ";    //mail body

        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        smtp.Send(fromAddress, toAddress, subject, body);
    }
}
}

No comments:

Post a Comment