Thursday 4 April 2013

How to Sent Mail with Attachment in ASP.NET

How to Sent Mail with Attachment in ASP.NET
http://www.programmerschoice.com/tutorials/AspDotNet_Details.aspx?ProgramName=How%20to%20Sent%20Mail%20with%20Attachment%20in%20ASP.NET


Description                                                                   
how to send mail in asp.net now I will explain how to implement mail sending concept with attachment in asp.net. To implement this concept first we need to following reference to our application System.Web.Mail namespace What is System.Web.Mail The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server. How we can get this reference (System.Web.Mail) To add this reference follow below steps. a) On the Project menu, click Add Reference. b) On the .NET tab, locate System.Web.dll, and then click Select. c) Click OK in the Add References.
Html Source Code :
<%@ Page Language="C#" AutoEventWireup="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</title>
    <link href="css/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div class="back-form" style="margin: 25%; font-family: verdana; padding: 5px 10px 5px 10px;">
        <table class="style1">
            <tr>
                <td class="style2" colspan="2" style="background-color: #006699">
                      <b>Send Mail in Asp.net C# with attatchment </b>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    <asp:Label ID="Label1" runat="server" Text="From"></asp:Label>
                </td>
                <td class="style3">
                    <asp:TextBox ID="Fromtxt" runat="server" CssClass="textbox-style" Width="239px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="To"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Totxt" runat="server" CssClass="textbox-style" Width="239px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Subject"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Subjecttxt" runat="server" CssClass="textbox-style" Width="239px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="Attatch File"></asp:Label>
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" CssClass="textbox-style" />
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <asp:Label ID="Label5" runat="server" Text="Body"></asp:Label>
                </td>
                <td valign="top">
                    <asp:TextBox ID="Bodytxt" runat="server" CssClass="textbox-style" Height="113px"
                        TextMode="MultiLine" Width="337px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                     
                </td>
                <td>
                    <asp:Label ID="msg" runat="server" Style="font-weight: 700; font-size: small"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                     
                </td>
                <td>
                    <asp:Button ID="SendMail" runat="server" Text="Send Mail" CssClass="black-button"
                        OnClick="SendMail_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
       
C# Code Behind
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Mail;

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

    }


    protected void SendMail_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage();
            // Sender e-mail address.
            Msg.From = Fromtxt.Text;
            // Recipient e-mail address.
            Msg.To = Totxt.Text;
            // Subject of e-mail
            Msg.Subject = Subjecttxt.Text;
            if (FileUpload1.HasFile)
            {
                // File Upload path
                String FileName = FileUpload1.PostedFile.FileName;
                //Getting Attachment file
                MailAttachment mailAttachment = new MailAttachment(FileName, MailEncoding.Base64);
                //Attaching uploaded file
                Msg.Attachments.Add(mailAttachment);
            }

            Msg.Body = Bodytxt.Text;
            // your remote SMTP server IP.
            SmtpMail.SmtpServer = "Your server ip address";
            SmtpMail.Send(Msg);
            Msg = null;
            msg.ForeColor = System.Drawing.Color.Green;
            msg.Text = "Mail Send Successfully !!";
        }
        catch (Exception ex)
        {
            msg.ForeColor = System.Drawing.Color.Red;
            msg.Text = ex.ToString();

        }


    }
}

No comments:

Post a Comment