SENDING MAIL USING C#

SENDING MAIL USING C#

email.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="email.aspx.cs" Inherits="email" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table >
    <tr><td><asp:Label ID="labto" runat="server" Text="To"></asp:Label></td><td>:</td><td><asp:TextBox ID="txtto" runat="server" ></asp:TextBox></td></tr>
    <tr><td><asp:Label ID="labsub" runat="server" Text="Sub"></asp:Label></td><td>:</td><td><asp:TextBox ID="txtsub" runat="server" ></asp:TextBox></td></tr>
    <tr><td><asp:Label ID="labmsg" runat="server" Text="Message"></asp:Label></td><td>:</td><td><asp:TextBox ID="txtmsg" runat="server" ></asp:TextBox></td></tr>
   
    </table>
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>
</body>
</html>





email.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string uname = "//Your mail address//";
        string pwd = "//Your Password//";
        string sub = txtmsg.Text.ToString();
        string lalmail = txtto.Text.ToString();
        //string candi_uname = name.Text + Session["id"].ToString();
        //string candi_pwd = name.Text + fname.Text;

        string msg = txtmsg.Text.ToString();
        if (SendMail(uname, pwd, lalmail, sub, msg))
        {



        }
    }
    public static bool SendMail(string gMailAccount, string password, string to, string subject, string message)
    {

        NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(gMailAccount);
        msg.To.Add(new MailAddress(to));
        msg.Subject = subject;
        msg.Body = message;
        msg.IsBodyHtml = true;
        SmtpClient client = new SmtpClient("smtp.gmail.com");
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = loginInfo;
        client.Send(msg);

        return true;


    }
}

Download Source Code:sendingemail