Click here to Skip to main content
Licence 
First Posted 18 May 2005
Views 69,718
Bookmarked 32 times

Sending Email through ASP. NET

By | 18 May 2005 | Article
An article on email sending through ASP. NET

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

About the Author

Mahantesh_Hongal

Web Developer

India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionsending email through asp.net Pinmembernaga_raj12:59 8 Mar '12  
Generalmail problem (through code) PinmemberPrakashPaul23:40 15 Mar '09  
GeneralNeed Help Pinmemberashu fouzdar23:25 11 Aug '08  
Questionhow to send email in asp.net Pinmemberdhirajjire2:43 17 Apr '08  
Questionhow to get scraped page control value Pinmemberdhirajjire2:37 17 Apr '08  
GeneralBhai sab code me error he Pinmemberloginsunil240819814:49 2 Apr '08  
General[COMException (0x80040213): The transport failed to connect to the server. PinmemberPaul's6:55 19 Nov '07  
Questionerror while running this code Pinmembersonalpateldayal21:10 16 Aug '07  
QuestionHelp !!!!!!..plz:-) Pinmembercoolshad9:44 12 Mar '06  
General[Message Removed] PinmemberMojtaba Vali23:00 20 May '05  
GeneralRe: It is shamefull Pinmembersauquilla10:46 18 Aug '05  
don't ne annoying.... the guy is just trying to help everybody, even YOU.... but it looks like guys like you don't deserve this kind of help.
Shame on you !!!
 
liliana
 
..
GeneralRe: It is shamefull Pinmemberammuv20:51 29 Jan '06  
GeneralOne problem I've had with sending mail from asp.net Pinmemberjugglerjon2:21 19 May '05  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 19 May 2005
Article Copyright 2005 by Mahantesh_Hongal
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid