Saturday 7 July 2012

Send email with attachment in asp.net


Send email with attachments to any domain email id

Step 1: email.aspx

<table>
<tr>
                            <td align="right" style="width: 98px; color: #000000;">
                                To</td>
            <td>
                <asp:TextBox ID="TextBoxToEmailID" runat="server" Width="385px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" style="width: 98px; color: #000000;">
                Subject</td>
            <td>
                <asp:TextBox ID="TextBoxSubject" runat="server" Width="452px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" style="width: 98px; color: #000000;">
                Attachment</td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" style="width: 98px; color: #000000;">
                Email</td>
            <td>
                <asp:TextBox ID="TextBoxEmailBody" runat="server" Height="175px"
                    TextMode="MultiLine" Width="461px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td align="left">
                <asp:Label ID="Label1" runat="server" ForeColor="Red" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td align="left">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="btnSend" runat="server" onclick="btnSend_Click" Text="Send"
                    Width="66px" />
                &nbsp;</td>
        </tr>
    </table>

Step 2 email.aspx,cs

using System;
using System.Collections;
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.Xml.Linq;
using System.Net;
using System.Net.Mail;

namespace email
{
    public partial class _Default : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Visible = false;
            TextBoxFromEmailID.Visible = false;
            TextBoxFromEmailID.Text = "Type email id here from which email will be sent";
            TextBoxPassword.Visible = false;
            TextBoxPassword.Text = "Type password of above email id";
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add(TextBoxToEmailID.Text);
            msg.From = new MailAddress(TextBoxFromEmailID.Text, "Type sender name here..");
            msg.Subject = TextBoxSubject.Text;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = TextBoxEmailBody.Text;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            if (FileUpload1.HasFile)
            {
                msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
            }
            msg.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(TextBoxFromEmailID.Text, TextBoxPassword.Text);
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            object userState = msg;
            try
            {
                client.Send(msg);
                Label1.Visible = true;
                Label1.Text = "Email sent successfully";
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Label1.Visible = true;
                Label1.Text = ex.Message;
            }
        }
    }
}

No comments:

Post a Comment