Tải bản đầy đủ - 0 (trang)
Part VIII: Sending Email with ASP.NET

Part VIII: Sending Email with ASP.NET

Tải bản đầy đủ - 0trang

Chapter23.ConfiguringASP.NETfor

Email

INTHISCHAPTER

HowASP.NETSendsMail330

ModifyingtheConfigurationFileforEmail331

Checkpoint334



HowASP.NETSendsMail

Oneofthemostcommonusesofformsinawebsiteistosend

email.Sendingemailfromawebsiteusedtobeacomplexand

difficulttask,butaswithsomanyothertasksthatusedtobe

complex,ASP.NETmakessendingemaileasy.

Inthischapteryou'llseetwodifferentclassesfromthe.NET

FrameworkusedtosendemailwithASP.NET;the

System.Net.SmtpClientclassandthe

System.Net.MailMessageclass.



TheSystem.Net.SmtpClientClass

TheSmtpClientclassfacilitatesthesendingofemailwiththe

SimpleMailTransferProtocolorSMTP.TheSmtpClientclassis

usedtoconnectyourASP.NETapplicationtothenetwork(either

theInternetoranothernetwork)andtoauthenticateyou(if

necessary)totheSMTPmailserver.

WewilluseafewpropertiesoftheSmtpClientclasstoconnect

totheSMTPserveronthenetwork:

HostProperty—TheHostpropertyisusedtospecifythe

nameoftheSMTPservertowhichyouareconnecting.

PortProperty—Everyconnectiononanetworktakes



placeoveraspecificchannelonthenetworkcalledaport.

SMTPcommunicationtakesplaceonport25bydefault.The

Portpropertydefaultstoavalueof25,butifyourSMTP

serverrequiresadifferentport,youcanspecifythatby

usingthePortproperty.

Tip

It'suncommonforanSMTPservertonotuseport

25.Askyournetworkadministrator,ISP,orhosting

companyifyou'renotsurewhatportyourSMTP

serveruses.

CredentialsProperty—MostSMTPserversrequirethat

youprovideausernameandpasswordorotherwise

authenticateyourselfbeforeyoucansendemail.

SomeSMTPservers(usuallyoncorporatenetworks)allow

youtousethecredentialsoftheaccountrunningyour

ASP.NETcode.Inthosecases,youcanspecifytousethe

defaultcredentials,butinthiswebapplication,we'llspecify

ausernameandpassword.



TheSystem.Net.MailMessageClass

TheMailMessageclass(asyou'velikelyguessed)represents

theactualmessagethatissent.Thefollowinglistshowssome

ofthepropertiesoftheMailMessageclass,allofwhichshould

beself-explanatory:

To—Thedestinationemailaddress(es)forthemessage.

From—Theemailaddressofthesender.

Cc—Theemailaddress(es)thatappearontheCCline.

Bcc—Theemailaddress(es)thatappearontheBCCline.



Subject—Theemail'ssubject.

Attachments—Alistoffileattachmentsincludedwiththe

email.

IsBodyHtml—ABooleanpropertythatspecifieswhetheror

nottheBodypropertyusesHTMLcode.

Body—Thebodyoftheemailbeingsent.Thisistheactual

textoftheemailmessage.

AfteryousetthepropertiesoftheMailMessageclass,youcall

theSendmethodandtheSmtpClientclassisusedtosendthe

message.

Note

Inthenextchapter,we'llwritethecodenecessaryto

sendanemailfromthecontactformcreatedearlier.



PartVIII:SendingEmailwithASP.NET







23ConfiguringASP.NETforEmail

24SendingFormResultstoEmail



Chapter23.ConfiguringASP.NETfor

Email

INTHISCHAPTER

HowASP.NETSendsMail330

ModifyingtheConfigurationFileforEmail331

Checkpoint334



HowASP.NETSendsMail

Oneofthemostcommonusesofformsinawebsiteistosend

email.Sendingemailfromawebsiteusedtobeacomplexand

difficulttask,butaswithsomanyothertasksthatusedtobe

complex,ASP.NETmakessendingemaileasy.

Inthischapteryou'llseetwodifferentclassesfromthe.NET

FrameworkusedtosendemailwithASP.NET;the

System.Net.SmtpClientclassandthe

System.Net.MailMessageclass.



TheSystem.Net.SmtpClientClass

TheSmtpClientclassfacilitatesthesendingofemailwiththe

SimpleMailTransferProtocolorSMTP.TheSmtpClientclassis

usedtoconnectyourASP.NETapplicationtothenetwork(either

theInternetoranothernetwork)andtoauthenticateyou(if

necessary)totheSMTPmailserver.

WewilluseafewpropertiesoftheSmtpClientclasstoconnect

totheSMTPserveronthenetwork:

HostProperty—TheHostpropertyisusedtospecifythe

nameoftheSMTPservertowhichyouareconnecting.

PortProperty—Everyconnectiononanetworktakes



placeoveraspecificchannelonthenetworkcalledaport.

SMTPcommunicationtakesplaceonport25bydefault.The

Portpropertydefaultstoavalueof25,butifyourSMTP

serverrequiresadifferentport,youcanspecifythatby

usingthePortproperty.

Tip

It'suncommonforanSMTPservertonotuseport

25.Askyournetworkadministrator,ISP,orhosting

companyifyou'renotsurewhatportyourSMTP

serveruses.

CredentialsProperty—MostSMTPserversrequirethat

youprovideausernameandpasswordorotherwise

authenticateyourselfbeforeyoucansendemail.

SomeSMTPservers(usuallyoncorporatenetworks)allow

youtousethecredentialsoftheaccountrunningyour

ASP.NETcode.Inthosecases,youcanspecifytousethe

defaultcredentials,butinthiswebapplication,we'llspecify

ausernameandpassword.



TheSystem.Net.MailMessageClass

TheMailMessageclass(asyou'velikelyguessed)represents

theactualmessagethatissent.Thefollowinglistshowssome

ofthepropertiesoftheMailMessageclass,allofwhichshould

beself-explanatory:

To—Thedestinationemailaddress(es)forthemessage.

From—Theemailaddressofthesender.

Cc—Theemailaddress(es)thatappearontheCCline.

Bcc—Theemailaddress(es)thatappearontheBCCline.



Subject—Theemail'ssubject.

Attachments—Alistoffileattachmentsincludedwiththe

email.

IsBodyHtml—ABooleanpropertythatspecifieswhetheror

nottheBodypropertyusesHTMLcode.

Body—Thebodyoftheemailbeingsent.Thisistheactual

textoftheemailmessage.

AfteryousetthepropertiesoftheMailMessageclass,youcall

theSendmethodandtheSmtpClientclassisusedtosendthe

message.

Note

Inthenextchapter,we'llwritethecodenecessaryto

sendanemailfromthecontactformcreatedearlier.



PartVIII:SendingEmailwithASP.NET







23ConfiguringASP.NETforEmail

24SendingFormResultstoEmail



Chapter23.ConfiguringASP.NETfor

Email

INTHISCHAPTER

HowASP.NETSendsMail330

ModifyingtheConfigurationFileforEmail331

Checkpoint334



HowASP.NETSendsMail

Oneofthemostcommonusesofformsinawebsiteistosend

email.Sendingemailfromawebsiteusedtobeacomplexand

difficulttask,butaswithsomanyothertasksthatusedtobe

complex,ASP.NETmakessendingemaileasy.

Inthischapteryou'llseetwodifferentclassesfromthe.NET

FrameworkusedtosendemailwithASP.NET;the

System.Net.SmtpClientclassandthe

System.Net.MailMessageclass.



TheSystem.Net.SmtpClientClass

TheSmtpClientclassfacilitatesthesendingofemailwiththe

SimpleMailTransferProtocolorSMTP.TheSmtpClientclassis

usedtoconnectyourASP.NETapplicationtothenetwork(either

theInternetoranothernetwork)andtoauthenticateyou(if

necessary)totheSMTPmailserver.

WewilluseafewpropertiesoftheSmtpClientclasstoconnect

totheSMTPserveronthenetwork:

HostProperty—TheHostpropertyisusedtospecifythe

nameoftheSMTPservertowhichyouareconnecting.

PortProperty—Everyconnectiononanetworktakes



placeoveraspecificchannelonthenetworkcalledaport.

SMTPcommunicationtakesplaceonport25bydefault.The

Portpropertydefaultstoavalueof25,butifyourSMTP

serverrequiresadifferentport,youcanspecifythatby

usingthePortproperty.

Tip

It'suncommonforanSMTPservertonotuseport

25.Askyournetworkadministrator,ISP,orhosting

companyifyou'renotsurewhatportyourSMTP

serveruses.

CredentialsProperty—MostSMTPserversrequirethat

youprovideausernameandpasswordorotherwise

authenticateyourselfbeforeyoucansendemail.

SomeSMTPservers(usuallyoncorporatenetworks)allow

youtousethecredentialsoftheaccountrunningyour

ASP.NETcode.Inthosecases,youcanspecifytousethe

defaultcredentials,butinthiswebapplication,we'llspecify

ausernameandpassword.



TheSystem.Net.MailMessageClass

TheMailMessageclass(asyou'velikelyguessed)represents

theactualmessagethatissent.Thefollowinglistshowssome

ofthepropertiesoftheMailMessageclass,allofwhichshould

beself-explanatory:

To—Thedestinationemailaddress(es)forthemessage.

From—Theemailaddressofthesender.

Cc—Theemailaddress(es)thatappearontheCCline.

Bcc—Theemailaddress(es)thatappearontheBCCline.



Subject—Theemail'ssubject.

Attachments—Alistoffileattachmentsincludedwiththe

email.

IsBodyHtml—ABooleanpropertythatspecifieswhetheror

nottheBodypropertyusesHTMLcode.

Body—Thebodyoftheemailbeingsent.Thisistheactual

textoftheemailmessage.

AfteryousetthepropertiesoftheMailMessageclass,youcall

theSendmethodandtheSmtpClientclassisusedtosendthe

message.

Note

Inthenextchapter,we'llwritethecodenecessaryto

sendanemailfromthecontactformcreatedearlier.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Part VIII: Sending Email with ASP.NET

Tải bản đầy đủ ngay(0 tr)

×