Search This Blog

Monday, September 26, 2011

Send Mail with multiple attachments in asp.net

Use below code to Send multiple attachments in Email


.ASPX

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title></title>   
   
</head>
<body>
    <table cellspacing="0" cellpadding="3" width="100%" bgcolor="navy" border="0">
        <tr>
            <td align="center">
                <span class="titl">ASP.NET&nbsp;Email</span>
            </td>
        </tr>
    </table>
    <br />
    <div align="center">
        <asp:Label ID="lblMessage" runat="server" Font-Names="Arial" Width="540" Visible="False"></asp:Label></div>
    <br />
    <div align="center">
        <form id="Form1" name="form2" method="post" enctype="multipart/form-data" runat="server">
        <table cellspacing="0" cellpadding="4" bgcolor="navy" border="0">
            <tr>
                <td>
                    <table cellspacing="3" cellpadding="4" width="540" bgcolor="white">
                        <tr>
                            <td valign="middle" align="right" width="80">
                                From:
                            </td>
                            <td>
                                <asp:TextBox ID="txtSender" TabIndex="1" runat="server" MaxLength="100" Width="386px"
                                    CssClass="width386" Font-Names="Arial"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td valign="middle" align="right" width="80">
                                To:
                            </td>
                            <td>
                                <asp:TextBox ID="txtReceiver" TabIndex="1" runat="server" MaxLength="100" Width="386px"
                                    CssClass="width386" Font-Names="Arial"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td valign="middle" align="right">
                                Subject:
                            </td>
                            <td>
                                <asp:TextBox ID="txtSubject" TabIndex="2" runat="server" MaxLength="200" Width="386px"
                                    CssClass="width386" Font-Names="Arial"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td valign="middle" align="right">
                                Format:
                            </td>
                            <td>
                                <asp:RadioButtonList ID="rblMailFormat" TabIndex="3" runat="server" RepeatColumns="2"
                                    RepeatDirection="Horizontal">
                                    <asp:ListItem Value="text" Selected="True">text</asp:ListItem>
                                    <asp:ListItem Value="html">html</asp:ListItem>
                                </asp:RadioButtonList>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top" align="right">
                                Message:
                            </td>
                            <td height="84">
                                <p>
                                    <asp:TextBox ID="txtBody" TabIndex="4" runat="server" Columns="40" Rows="5" TextMode="MultiLine"
                                        Width="451px" CssClass="width386" Font-Names="Arial"></asp:TextBox></p>
                            </td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td>
                                <asp:FileUpload ID="FileUpload1" runat="server" style="margin-left: 0px"
                                    Width="445px" />
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">
                                <asp:Button ID="btnSend" TabIndex="9" runat="server" Width="100px" Text="Send" OnClick="btnSend_Click">
                                </asp:Button>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        </form>
        <h1>
        </h1>
    </div>
</body>
</html>

.ASPX.CS

using System;
using System.Drawing;
using System.Net.Mail;

public partial class SendMail : System.Web.UI.Page
{   
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            // Create a new blank MailMessage
            MailMessage email = new MailMessage();

            // Set the properties of the MailMessage to the values on the form
            if (rblMailFormat.SelectedItem.Text == "text")
                email.IsBodyHtml = false;
            else
                email.IsBodyHtml = true;

            email.From = new MailAddress(txtSender.Text);
            email.To.Add(new MailAddress(txtReceiver.Text));
            email.Subject = txtSubject.Text;
            email.Body = txtBody.Text;
            string strFileName = FileUpload1.PostedFile.FileName;
            //adding attachements from browse & upload
            Attachment attach = new Attachment(strFileName);
            // Follow the same trick for multiple attachment
            email.Attachments.Add(attach);
            //Set the SMTP server
            SmtpClient smtp = new SmtpClient("localhost");
            // send the email
            smtp.Send(email);
           
            // Reset the form
            txtSender.Text = "";
            txtReceiver.Text = "";
            txtSubject.Text = "";
            txtBody.Text = "";

            // Dispaly a friendly message telling the user
            // his email has been sent
            lblMessage.Visible = true;
            lblMessage.ForeColor = Color.Black;
            lblMessage.Text = "Your email has been sent";
        }
        catch (Exception ex)
        {        
            lblMessage.Visible = true;
            lblMessage.ForeColor = Color.Red;
            lblMessage.Text = ex.ToString();
        }
    }
}
Post a Comment

Popular Posts