Click here to Skip to main content
Click here to Skip to main content

Sending Email through ASP. NET

, 19 May 2005
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.

Comments and Discussions

 
SuggestionAnother example how to send mail from asp.net PinmemberRakesh Sinha19-Jun-13 23:21 
Questionsending email through asp.net Pinmembernaga_raj8-Mar-12 13:59 
Generalmail problem (through code) PinmemberPrakashPaul16-Mar-09 0:40 
GeneralNeed Help Pinmemberashu fouzdar12-Aug-08 0:25 
Questionhow to send email in asp.net Pinmemberdhirajjire17-Apr-08 3:43 
Questionhow to get scraped page control value Pinmemberdhirajjire17-Apr-08 3:37 
GeneralBhai sab code me error he Pinmemberloginsunil240819812-Apr-08 5:49 
General[COMException (0x80040213): The transport failed to connect to the server. PinmemberPaul's19-Nov-07 7:55 
Questionerror while running this code Pinmembersonalpateldayal16-Aug-07 22:10 
QuestionHelp !!!!!!..plz:-) Pinmembercoolshad12-Mar-06 10:44 
General[Message Removed] PinmemberMojtaba Vali21-May-05 0:00 
GeneralRe: It is shamefull Pinmembersauquilla18-Aug-05 11:46 
GeneralRe: It is shamefull Pinmemberammuv29-Jan-06 21:51 
GeneralOne problem I've had with sending mail from asp.net Pinmemberjugglerjon19-May-05 3: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
Web03 | 2.8.150327.1 | Last Updated 19 May 2005
Article Copyright 2005 by Mahantesh_Hongal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid