Saturday 3 August 2013

How to Send Mail or Email with Attachment a File in Asp.Net






Description:-

                        In the previous Example we only Explain How to Send mail in Asp.Net .but in this Example we can not only send simple mail but we ca also Atteched the word file,Excel file,image File or any other type of File with an Email.

in which Example you can send Email with an attechment using SMTP server. the System.Web.Mail Namespace Provide classes MailMessage are used for sending Email and Mailattechment class are used for manage the Attechment of the File.

we provide same facility as provide in Gmail you can easily upload any type of file with mail and send to the user.

You have to add the Foolowing Namespace to Send mail to user like

using System.Net.Mail;

Here we Define Simple Code that How to Attach File in Email

MailMessage mail = new MailMessage();
//Attach file using FileUpload Control and put the file in memory stream
        if (fileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
        }

You Have Require SMTP Email Configuration before sendind Email to other user so to configure the Setting please Click Here this post will Explain that in briefly.

To send SMS in Asp.Net to any User then click Here Send SMS or message in Asp.net

To Send Email in Asp.Net click Here Send Email to Other user in ASP.net

to show example of insert,update,delete in Modal Popup click here Modal Popup for CRUD operation


To Import Gmail Contact in Asp.Net click here Import contact from Gmail in asp.net




Default.aspx:-



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



<!DOCTYPE html>



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
     <tr>
<td>
From:
</td>
<td>
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" />
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</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 Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtTo.Text);

        //mail.To.Add
        mail.From = new MailAddress(txtFrom.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtBody.Text;
        mail.IsBodyHtml = true;

        //Attach file using FileUpload Control and put the file in memory stream
        if (fileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("your EmailId", "gmail password");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}
 

3 comments:

  1. thanks for comment and also visit blog for latest new topic upadated post and share with your friends.thanks

    ReplyDelete