send email with attachment c#

Tags | send email with attachment c#, asp.net send email c# Gmail, asp net send email example c#, asp.net send email c# to gmail, how to send email with attachment in asp.net using c#

Send Email with Attachment C#

In this asp.net c# tutorial, you will learn that sending an email with an attachment using C# ASP.Net. First, you will need to import the System.Net.Mail namespace. You will learn how to send email using Gmail SMTP Server in ASP.Net.

The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and the message attachment. Find the below asp.net send email 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>
    <title>asp.net send email attachment fileupload</title>
    <link href="ui.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <br />
        <div>
            <br />
            <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#5482fc">
                <tr>
                    <td height="50" align="center" class="lgHeader1">
                        <h2>ASP.Net Send Email with Attachment</h2>
                    </td>
                </tr>
            </table>
            <br />
            <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;To
                    </td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtTo" runat="server" Columns="50"></asp:TextBox>
                    </td>
                </tr>

                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;Subject
                    </td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtSubject" runat="server" Columns="50"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;Body
                    </td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtBody" runat="server" Columns="40" TextMode="MultiLine"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" bgcolor="#eeeeee" class="header1">&nbsp;Attachment
                    </td>
                    <td bgcolor="#FFFFFF">
                        <asp:FileUpload ID="fuAttachment" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;Gmail Email
                    </td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtEmail" runat="server" Columns="50"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" bgcolor="#eeeeee" class="header1">&nbsp;Gmail Password
                    </td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtPassword" TextMode="Password" runat="server" Columns="50"></asp:TextBox>
                    </td>
                </tr>

                <tr>
                    <td align="right" bgcolor="#eeeeee" class="header1">Action
                    </td>
                    <td bgcolor="#FFFFFF">
                        <asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="btnSubmit_Click" />
                    </td>
                </tr>
                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">Status
                    </td>
                    <td bgcolor="#FFFFFF" class="basix">
                        <asp:Literal ID="litStatus" runat="server"></asp:Literal>
                    </td>
                </tr>
            </table>
            <br />
        </div>
    </form>
</body>
</html>

using System;
using System.Net.Mail;
using System.IO;
using System.Net;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string to = txtTo.Text;
            string from = txtEmail.Text;
            string subject = txtSubject.Text;
            string body = txtBody.Text;

            using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
            {
                mm.Subject = txtSubject.Text;
                mm.Body = txtBody.Text;
                if (fuAttachment.HasFile)
                {
                    string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
                    mm.Attachments.Add(new Attachment(fuAttachment.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);
                litStatus.Text = "Your message has been sent.";
            }

        }
        catch (Exception ex)
        {
            litStatus.Text = ex.ToString();
        }
    }
}

Download Source Code

Note: Make sure you have Turn On access to Less Secure Apps as shown below.

less secure access on send email with attachment Send Email with Attachment in ASP.Net using C# less secure access min

Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

LEAVE A REPLY

Please enter your comment!
Please enter your name here