Send Email using ASP.NET 2.0 and C#
This Example code will show how to send a simple email message with an attachment using ASP.NET 2.0 and C#
Actually it is very simple to send an email with an attachment using ASP.NET 2.0 and C#.
Namespace: you will need to import the System.Web.Mail namespace.
Class: The System.Net.Mail namespace contains the MailMessage and MailAttachment Classes that we need in order to send the email and the message attachment.
We use the btnSubmit_Click event to do the work.
We then call the emailClient.Send to send the message using the variables from our ASP.NET coded page.
The txtAttachmentPath.Text Texbox provides the path to the file to attach to the email message.
Code sendmail.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.Net.Mail;
using System.Web.Mail;
namespace Test
{
public partial class SendMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage MyMessage = new MailMessage();
MyMessage.From = txtFrom.Text.Trim();
MyMessage.To = txtTo.Text.Trim();
MyMessage.Subject = txtSubject.Text.Trim();
MyMessage.Body = txtBody.Text.Trim();
MyMessage.Priority = MailPriority.High;
MailAttachment mailAttachments = new MailAttachment(txtAttachmentPath.Text);
MyMessage.Attachments.Add(mailAttachments);
SmtpMail.Send(MyMessage);
litStatus.Text = "Message Sent";
}
catch (Exception ex)
{
litStatus.Text = ex.ToString();
}
}
}
}
sendmail.aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendMail.aspx.cs" Inherits="Test.SendMail" %>
pls find attached code file for your reference.