Click here to Skip to main content
11,711,149 members (85,871 online)
Click here to Skip to main content

Sending Email through ASP. NET

, 18 May 2005 100.5K 5 32
Rate this:
Please Sign up or sign in to vote.
An article on email sending through ASP. NET
<!-- --><!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (

,

etc) -->

Sending Email through ASP. NET

Common and important aspect used in Web designing is email sending. Basic use of sending email from a Web page is to enable the users to give their comments/suggestions through web form. The .NET Framework provides extremely straightforward means to send emails. ASP .NET makes use of SmtpMail and MailMessage classes to send an emaill. The SmtpMail and MailMessage classes are defined in the System.Web.Mail namespace. The MailMessage class has properties and methods for creating an email and the SmtpMail class has send method to send an email.

Just copy and paste the following code to send an email

public int sendMail(string to,string cc,string bcc,string subject,string body)

{ 

    try 

        {  

           
       SmtpMail.SmtpServer="your_server_address"; 

       MailMessage msg = new MailMessage(); 

            msg.From = "your_email_id";
                
       msg.To = to; 

            msg.Cc = cc; 

            msg.Bcc = bcc; 

            msg.Subject = subject; 

            msg.Body = body; 

       SmtpMail.Send(msg); 

            return(1); 
        } 
        catch 
        { 
               return (0); 
        } 
}

on button click

private void Button1_ServerClick(object sender, System.EventArgs e) 
{ 
    String to = “to_email_id”; 
    String cc = “cc_email_id”; 
    String bcc = “bcc_email_id”; 
    String subject = “your subject goes here”; 
    String body = “your body text goes here”; 

    int status = sendMail(to,cc,bcc,subject,body); 

    if(status == 1) 
        Response.Write("your mail has been sent successfully"); 
    else 
        Response.Write("sorry! your mail could not be sent”); 
}
 

How to send attachments?

The MailMessage not only provide methods and properties to create mail but also to attach files (attachments). The following line can be included in the above program to send attachments.

//
msg.Attachments.Add(new MailAttachment(“file_path_to_be_attached”));
//
// Example: msg.Attachments.Add(new MailAttachments(@”c:\mahantesh.txt”);
//

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Mahantesh_Hongal
Web Developer
India India
Mahantesh Hongal is MCA grad from Karnatak University Dharwad (Karnataka, India). He learned a whole lot from internet community and He would like to contribute back just by posting the useful stuffs for the internet community. He has started his programming career from BASIC programming language and until now He has learned a bit about Pascal, dBase, FoxPro, Cobol, vb, c, c++, c#, java, Perl and asp .net.

You may also be interested in...

Comments and Discussions

 
SuggestionAnother example how to send mail from asp.net Pin
Rakesh Sinha19-Jun-13 22:21
memberRakesh Sinha19-Jun-13 22:21 
Questionsending email through asp.net Pin
naga_raj8-Mar-12 12:59
membernaga_raj8-Mar-12 12:59 
Generalmail problem (through code) Pin
PrakashPaul15-Mar-09 23:40
memberPrakashPaul15-Mar-09 23:40 
Questionhow to send email in asp.net Pin
dhirajjire17-Apr-08 2:43
memberdhirajjire17-Apr-08 2:43 
Questionhow to get scraped page control value Pin
dhirajjire17-Apr-08 2:37
memberdhirajjire17-Apr-08 2:37 
GeneralBhai sab code me error he Pin
loginsunil240819812-Apr-08 4:49
memberloginsunil240819812-Apr-08 4:49 
General[COMException (0x80040213): The transport failed to connect to the server. Pin
Paul's19-Nov-07 6:55
memberPaul's19-Nov-07 6:55 
Questionerror while running this code Pin
sonalpateldayal16-Aug-07 21:10
membersonalpateldayal16-Aug-07 21:10 
QuestionHelp !!!!!!..plz:-) Pin
coolshad12-Mar-06 9:44
membercoolshad12-Mar-06 9:44 
General[Message Removed] Pin
Mojtaba Vali20-May-05 23:00
memberMojtaba Vali20-May-05 23:00 
GeneralRe: It is shamefull Pin
sauquilla18-Aug-05 10:46
membersauquilla18-Aug-05 10:46 
GeneralRe: It is shamefull Pin
ammuv29-Jan-06 20:51
memberammuv29-Jan-06 20:51 
GeneralOne problem I've had with sending mail from asp.net Pin
jugglerjon19-May-05 2:21
memberjugglerjon19-May-05 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 19 May 2005
Article Copyright 2005 by Mahantesh_Hongal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid