Sie sind auf Seite 1von 6

Sending email in ASP.NET 2.

0
:Introduction The System.Net.Mail namespace in ASP.NET 2.0 has replaced the System.We .Mail namespace in ASP.NET !.". In ASP.NET 2.0# you should $no% that the method o& sending emails has changed slightly. 'ollo% (rad .)ingsley as he sends his &irst email &rom an ASP.NET 2.0 application *hanges to send message in ASP.NET 20 and its pre+ious +ersion System.Net.Mail.SmtpClient is used instead o& .System.Web.Mail.SmtpMail System.Net.MailMessage *lass is used instead o& .System.Web.Mail.MailMessage

The System.Net.MailMessage class collects &rom address as .MailAddress object

The System.Net.MailMessage class collects To# **# (cc addresses as .MailAddressCollection .MailMessage (ody 'ormat is replaced y IsBodyHtml :In the gi+en e"ample the &orm contains &i+e te"t o"es .'irst te"t o" is used &or recipient,s email address .Second te"t o" is used &or sender,s email address .Third te"t o" is used &or su -ect o& the email

.'ourth te"t o" is used &or the ody o& the email

'i&th te"t o" is used &or the list status .it pro+ide the message that mail ..has sent or not /ne utton is used &or sending the email. So %e %rite the code on the . utton,s clic$ e+ent .We add the System.Net.Mail namespace &or sending the email 'or E"ample: Through this e"ample you can understand ho% to send the .email in ASP.NET 2.0 .These are the &ollo%ing steps to help you to send email in ASP.NET 2.0

.Step !: /pen your ne% %e site .Step 2: 0esign the page .Step 1: No% add the System.Net.Mail namespace .Step 2: Write the cs code on the utton,s clic$ e+ent :0e&ault.asp" Page 6666666678*98 AutoE+entWireup78true8 *ode'ile780e&ault.asp".cs8 345 ;4 8Inherits78:0e&ault 0/*T=PE html P>(?I* 8@AAW1*AA0T0 BCTM? !.0 <5 ;8TransitionalAAEN88http:AA%%%.%1.orgATDA"html!A0T0A"html!@transitional.dtd ; 8html "mlns78http:AA%%%.%1.orgA!EEEA"html5 ;8head runat78ser+er 5 ;title5Email sending;Atitle5 ;headA5 ; ody5 ;8&orm id78&orm!8 runat78ser+er 5 ;di+5 ta le cellpadding78!08 cellspacing7808 order7828 %idth782048 5 ;8height78!00p" ;tr5 ;td +align78top8 style78padding@top:20p"F ac$ground@color: 9&&cc&&F85 asp:?a el I078?a el!8 runat78ser+er8 Te"t78To 8 'ore*olor78Purple85 F;Aasp:?a el5 Gn spF Gn spF Gn spFGn spF Gn sp ;asp:Te"t(o" I078T!8 runat78ser+er8 (ac$*olor789*0''''85;Aasp:Te"t(o" 5 ;A r A5; r5 asp:?a el I078?a el28 runat78ser+er8 Te"t78'rom8 'ore*olor78Purple85 5 F;Aasp:?a el5Gn spF Gn sp asp:Te"t(o" I078T28 runat78ser+er8 5 ;A (ac$*olor789*0''''85;Aasp:Te"t(o"5; r A5; r

asp:?a el I078?a el18 runat78ser+er8 Te"t78Su -ect8 5 ;'ore*olor78Purple85;Aasp:?a el asp:Te"t(o" I078T18 runat78ser+er8 5 ;A (ac$*olor789*0''''85;Aasp:Te"t(o"5; r A5; r asp:?a el I078?a el28 runat78ser+er8 Te"t78(ody8 5 F'ore*olor78Purple85;Aasp:?a el5Gn spF Gn sp asp:Te"t(o" I078T28 runat78ser+er8 Ceight78!00p"8 Width78200p"8 5 ;A (ac$*olor789*0''''8 'ore*olor78(lac$85;Aasp:Te"t(o"5; r A5; r asp:?a el I078?a elH8 runat78ser+er8 Te"t78?ist Status8 5 ;'ore*olor78Purple85;Aasp:?a el asp:Te"t(o" I078TH8 runat78ser+er8 (ac$*olor789*0''''8 5 ;A 'ore*olor78Ded85;Aasp:Te"t(o"5; rA5; r asp:(utton I078Send8 runat78ser+er8 Te"t78Send8 /n*lic$78Send:*lic$8 5 ;A 8(ac$*olor789*0*0008 'ore*olor78Na+y ;tdA5 ;trA5 ;ta le5;Adi+A5 ;&ormA5 ; odyA5 ;htmlA5 :0esign +ie% o& the a o+e page is as &ollo%s

'igure !: 0esign o& the &orm.

0e&ault.asp".cs: using SystemF using System.0ataF using System.*on&igurationF using System.We F using System.We .SecurityF using System.We .>IF using System.We .>I.We *ontrolsF using System.We .>I.We *ontrols.We PartsF using System.We .>I.Ctml*ontrolsF using System.Net.MailF pu lic partial class :0e&ault : System.We .>I.Page I protected +oid Page:?oad.o -ect sender# E+entArgs eJ IK protected +oid Send:*lic$.o -ect sender# E+entArgs eJ I Smtp*lient smtpclient 7 ne% Smtp*lient.JF MailMessage message 7 ne% MailMessage.JF smtpclientl.Cost 7 8localhost8F try I MailAddress Send'rom 7 ne% MailAddress.T!.Te"tJF

MailAddress SendTo 7 ne% MailAddress.T2.Te"tJF MailMessage MyMessage 7 ne% MailMessage.Send'rom# SendToJF MyMessage.Su -ect 7 T1.Te"tF MyMessage.(ody 7T2.Te"tF TH.Te"t 7 8Message Sent8F K catch .E"ception e"J I TH.Te"t 7 e".ToString.JF K K K /utput: The &ollo%ing page %ill display %hen you de ug this code. And you should &ill the gi+en entries in the &orm.

'igure 2: A&ter de ug the code this page %ill open.

A&ter &illing all entries clic$ on send utton then you %ill see the &ollo%ing output.

'igure 1: This &igure represents the &inal output. ?ist status gi+es the in&ormation a out the sending message.

Das könnte Ihnen auch gefallen