Friday, 17 May 2013

Working Sending Mail



Sending Email
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sending Mail.aspx.cs" Inherits="Mail_sending_Mail" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>Sending Mail</p>
    <table>
    <tr>
<td><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Brown;">To:</span></td>
<td><asp:textbox id="txtName" Width="241" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Brown;">CC:</span></td>
<td><asp:textbox id="txtEmail" Width="241" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td class="auto-style1"><strong>Subject </strong></td>
     <td>
<asp:TextBox ID="TextBox1" runat="server" Width="241"></asp:TextBox>
                    </td>
    </tr>
    <tr>
     <td colSpan="2" ><span style="font-family:Verdana; font-size:12px; font-weight:bold; color:Brown;">Message:</span></td>
    </tr>
    <tr>
     <td align="center" colSpan="2" width=100%><asp:textbox id="txtMessage" Width="100%" Runat="server" Height="99" TextMode="MultiLine" MaxLength="400"></asp:textbox></td>
    </tr>
    <tr>
     <td colSpan="2"> </td>
    </tr>
     <tr>
     <td align=center>
                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" />
                    </td>
     <td align=center> </td>
    </tr>
    <tr>
     <td colSpan="2"><asp:label id="lblStatus" Runat="server" EnableViewState="False"></asp:label></td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>
.CS code
using System.Net.Mail;
protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtName.Text);
        mail.To.Add(txtEmail.Text);
        mail.From = new MailAddress("email address from where you want to send emails");
        mail.Subject = TextBox1.Text;

        string Body = txtMessage.Text;
        mail.Body = Body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("add email address", "password");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }

No comments:

Post a Comment