How to Send Email with Attachment using ASP.NET with c#



This tutorial will show you how to send a simple email message with an attachment using
ASP.NET with C# Sending a email with an attachment using ASP.NET is actually very simple.First, you will need to import the System.Net.Mail namespace.The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and the message attachment.

aspx page

<%@ 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 style="width: 80px">
            To:
        </td>
        <td>
            <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Subject:
        </td>
        <td>
            <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td >
            Body:
        </td>
        <td>
            <asp:TextBox ID="txtBody" runat="server" TextMode = "MultiLine" Height = "150" Width = "200"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            File Attachment:
        </td>
        <td>
            <asp:FileUpload ID="fileattechment" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Email:
        </td>
        <td>
            <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Password:
        </td>
        <td>
            <asp:TextBox ID="txtPassword" runat="server" TextMode = "Password"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button ID="sendmail" Text="Send" OnClick="sendmail_Click" runat="server" />
            <asp:Label ID="lblmssage" runat="server" Text="Label"></asp:Label>
        </td>
    </tr>
</table>
    </div>
    </form>
</body>
</html>

c# code


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
   
    protected void sendmail_Click(object sender, EventArgs e)
    {
        MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text);
       
            mm.Subject = txtSubject.Text;
            mm.Body = txtBody.Text;
            if (fileattechment.HasFile)
            {
                string FileName = Path.GetFileName(fileattechment.PostedFile.FileName);
                mm.Attachments.Add(new Attachment(fileattechment.PostedFile.InputStream, FileName));
            }
            mm.IsBodyHtml = false;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
           lblmssage.Text="Email sent successfully";
        
    }
}

Comments

Popular posts from this blog

Bootstrap Modal Popup keep open on PostBack in ASP.Net

Resolved Issue in Asp core 3.0 serializersettings does not exist in AddJsonOptions

.Net most asked interview questions for experienced professionals (C#,Asp WEBFORM,MVC,ASP CORE,WEB API,SQL Server,Java Script,Jquery)