Share Blog

Wednesday, May 14, 2014

How to send Mail using Asp.net


Default.aspx(Source)

<%@ Page Language="C#" AutoEventWireup="true" Async="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
    <title>Send Mail using asp.net</title>
    <style type="text/css">
        .auto-style1 {
          
            font-weight: bold;
           
        }
        .auto-style2 {
      
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="border-style: solid; border-color: inherit; border-width: 1px; " align="center">
                <tr>
                    <td colspan="2" align="center" class="auto-style1">Send Mail with Attachment using asp.net
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1">From:
                    </td>
                    <td>
                        <asp:TextBox ID="txt_from" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1">To:
                    </td>
                    <td>
                        <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1">Subject:
                    </td>
                    <td>
                        <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1">Attach a file:
                    </td>
                    <td>
                        <asp:FileUpload ID="fileUpload1" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td valign="top" class="auto-style1">Body:
                    </td>
                    <td>
                        <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2"></td>
                    <td>
                        <asp:Button ID="btnSubmit" Text="Send" runat="server" OnClick="btnSubmit_Click" Style="height: 26px" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Default.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.Mail;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtTo.Text);
        mail.From = new MailAddress(txt_from.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtBody.Text;
        mail.IsBodyHtml = true;
        if (fileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new System.Net.NetworkCredential("vivekkumarsingh.vns@gmail.com", "*******");
        smtp.EnableSsl = true;
        smtp.Send(mail);
        txt_from.Text = "";
        txtBody.Text = "";
        txtSubject.Text = "";
        txtTo.Text = "";
        Response.Write("Send Mail Successfully");
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}






No comments:

Post a Comment