C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Email »

Send Email using ASP.NET 2.0 and C#


Posted Date: 27 Dec 2008    Resource Type: Code Snippets    Category: Email
Author: Nikhil AgarwalMember Level: Gold    
Rating: 1 out of 5Points: 10



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" %>





Untitled Page





































To
From
SMTP Server
Subject
Attachment
Body
Action
Status







pls find attached code file for your reference.

Attachments

  • Code zip file for your reference (24402-27141-code.zip)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Send Email using ASP.NET 2.0 and C#  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: How to bulk email with attachment using c#.net
    Previous Resource: Sending email through C#
    Return to Discussion Resource Index
    Post New Resource
    Category: Email


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use