zl程序教程

您现在的位置是:首页 >  后端

当前栏目

JSP发送邮件实例

JSP实例 发送 邮件
2023-06-13 09:13:45 时间
vishal_donthgavethisresponseon10/18/2000:  
//thesearethepakagestobeimportedfrom  
//JavaMail  
//TheJavaMailPAckageeitherbedowloaded  
//seperately  
//orelseisAvailableintheJ2sdkee1.2  
//(JavaEnterpriseEdition)  

importjavax.mail.*;  
importjavax.mail.internet.*;  
importjava.util.*;  


//Thisfunctioncanbeusedtosendthemail  
//withtheparametersgiventoit  
//Uhavetospecifythesmtpserverthrough  
//whichuhavetosendthemail  
//sinceiwastryingwithahomenetmail  
//accountidirectlysentthemailitsserver  
//Forsendingthismailuneedamailserver  
//whichletsutorelaythemessages  
//Trythisthingforsendingtoa  
//www.homenetmail.comaccountbecauseitlets  
//usend  
//mailstotheaccountslikeexampletry  
//sendingittoa"abc@homenetmail.com"  
//account.Createthemailaccountinhomenet  
//mailfirst.Ifugetanyotherserverwhich  
//supportsrelayingucantrythisonthat  
//also.  

//UsethisfunctioninurServlettosend  
//mailbycallingthefunctionwiththe  
//parameters  

publicvoidsendMail(StringtoAddr,Stringsubject,Stringbody,StringfromAddr)throwsRemoteException{  
try{  
Propertiesprops=newProperties();  
props.put("mail.smtp.host","mail.homenetmail.com");  
//HerewespecifytheSMTPserverthrough  
//whichthemailshouldbedelivered  
Sessionsession=Session.getDefaultInstance(props,null);  
Messagemsg=newMimeMessage(session);  
msg.setFrom(newInternetAddress(fromAddr));  
//SpecifytheFromAddress  
InternetAddress[]tos=InternetAddress.parse(toAddr);  
//SpecifytheToAddress  
msg.setRecipients(Message.RecipientType.TO,tos);  
msg.setSubject(subject);  
//SpecifytheSubject  
msg.setText(body);  
//SpecifytheBody  
Transport.send(msg);  
System.out.println("MessageisSent");  
}  
catch(Exceptione){  
System.out.println(e);  
}  
}  

//Uhavetorunthisfunctiononacomputer  
//whichisdirectlyconnected  
//tointernetbutnotthrougha  
//proxy......orelseuseaproxywhich  
//supportsSMTP