This tutorial will show you how to send a simple email message with an attachment using ASP.NET 2.0
Sending a email with an attachment using ASP.NET 2.0 is actually very simple.
First, you will need to use the System.Net.Mail namespace.
The System.Net.Mail namespace contains the SmtpClient class
using System.Net.Mail;
[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-asp.net.png" alt= "Smart Social Share" href="http://codecanyon.net/item/smart-social-share/160097" title="Smart Social Share (Asp.net control)" description="Smart Social share is a asp.net control . Which helps you to give user to share your content with social book mark site." ref="marifdu"]
Bellow is the form of our html
<%@ 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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="qform">
<p>
<label>
Name <b>*</b></label><br />
<asp:TextBox ID="_txtName" runat="server" CssClass='qtxt required' />
</p>
<p>
<label>
Email Address <b>*</b></label><br />
<asp:TextBox ID="_txtEmail" runat="server" CssClass='qtxt required email' />
</p>
<p>
<label>
Subject <b>*</b></label><br />
<asp:TextBox ID="_txtSubject" runat="server" CssClass='qtxt required' />
</p>
<p>
<label>
Description of Question</label><br />
<asp:TextBox ID="_txtQuestion" runat="server" TextMode="MultiLine" Columns="51" Rows="6" />
</p>
<p>
<label>
Attachment </label><br />
<asp:FileUpload ID="fuAttachment" runat="server" />
</p>
<p>
<asp:Button runat="server" ID="sendq" Text="Send" OnClick="sendq_Click" />
</p>
<p><asp:Label runat="server" ID="lblMessage"></asp:Label></p> </div>
</div>
</form>
</body>
</html>
Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void sendq_Click(object sender, EventArgs e)
{
string filename = "";
String rootPath = "";
if (fuAttachment.HasFile)
{
string dirUrl = Path.Combine(Request.PhysicalApplicationPath, "attachment/");
filename = fuAttachment.PostedFile.FileName;
string fullUploadPath = Path.Combine(dirUrl, fuAttachment.PostedFile.FileName);
fuAttachment.PostedFile.SaveAs(fullUploadPath);
rootPath = Server.MapPath("~/attachment/" + filename);
}
var subject = _txtSubject.Text;
var from = _txtEmail.Text;
var to = "m_arifdu@yahoo.com";
var questoin = _txtQuestion.Text;
var body = string.Format("A new file send with attachment");
Mailer.Send(from, to, subject, body, true, rootPath);
lblMessage.Text = "Mail send with attachment";
}
}
In our web.config file we con fig our mail credential. As we send mail with gmail so we put here gmail settings.
To send mail with gmail please enable ssl from our mailer class. As google need ssl to send mail. Other wise you will get error.
<system.net>
<mailSettings>
<smtp>
<network port="587" host="smtp.gmail.com" userName="username" password="password"/>
</smtp>
</mailSettings>
</system.net>
We make a mailer class to send mail here is Our Mailer class
using System;
using System.Data;
using System.Configuration;
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.Mail;
public class Mailer
{
/// <summary>
/// Send Email without attachment
/// </summary>
/// <param name="messagefrom">Email from address</param>
/// <param name="toaddress">Email to address</param>
/// <param name="subject">Email subject</param>
/// <param name="body">Email body</param>
/// <param name="ishtml">True if the body contains HTML</param>
/// <param name="fileattachment">attachment</param>
/// <returns></returns>
public static bool Send(string messagefrom, string toaddress, string subject, string body, bool ishtml, string fileattachment)
{
MailMessage message;
//MailAddress mfrom, mto;
SmtpClient sc = new SmtpClient();
sc.EnableSsl = true; // this need for some domail need ssl // to send mail with gmail you need to true this value
try
{
message = new MailMessage();
message.To.Add(toaddress);
message.From = new MailAddress(messagefrom);
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = ishtml;
if (fileattachment != "")
{
Attachment attachment = new Attachment(fileattachment);
message.Attachments.Add(attachment);
}
sc.Send(message);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
}