Professional Documents
Culture Documents
To send an email using a JSP is simple enough but to start with you should have JavaMail API and Java Activation Framework (JAF) installed on your machine. You can download latest version of JavaMail (Version 1.2) from Java's standard website. You can download latest version of JavaBeans Activation Framework JAF (Version 1.0.2) from Java's standard website. Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. You need to add mail.jar and activation.jar files in your CLASSPATH.
www.tutorialspoint.com/cgi-bin/printpage.cgi
1/6
4/17/13
/ /N o ws e tt h ea c t u a lm e s s a g e m e s s a g e . s e t T e x t ( " T h i si sa c t u a lm e s s a g e " ) ; / /S e n dm e s s a g e T r a n s p o r t . s e n d ( m e s s a g e ) ; r e s u l t=" S e n tm e s s a g es u c c e s s f u l l y . . . . " ; } c a t c h( M e s s a g i n g E x c e p t i o nm e x ){ m e x . p r i n t S t a c k T r a c e ( ) ; r e s u l t=" E r r o r :u n a b l et os e n dm e s s a g e . . . . " ; } % > < h t m l > < h e a d > < t i t l e > S e n dE m a i lu s i n gJ S P < / t i t l e > < / h e a d > < b o d y > < c e n t e r > < h 1 > S e n dE m a i lu s i n gJ S P < / h 1 > < / c e n t e r > < pa l i g n = " c e n t e r " > < % o u t . p r i n t l n ( " R e s u l t :"+r e s u l t+" \ n " ) ; % > < / p > < / b o d y > < / h t m l >
Now let us put above code in SendEmail.jsp file and call this JSP using URL http://localhost:8080/SendEmail.jsp which would send an email to given email ID abcd@gmail.com and would display following response:
If you want to send an email to multiple recipients then following methods would be used to specify multiple email IDs:
v o i da d d R e c i p i e n t s ( M e s s a g e . R e c i p i e n t T y p et y p e , A d d r e s s [ ]a d d r e s s e s ) t h r o w sM e s s a g i n g E x c e p t i o n
Here is the description of the parameters: type: This would be set to TO, CC or BCC. Here CC represents Carbon Copy and BCC represents Black Carbon Copy. Example Message.RecipientType.TO addresses: This is the array of email ID. You would need to use InternetAddress() method while specifying email IDs
www.tutorialspoint.com/cgi-bin/printpage.cgi
2/6
4/17/13
Here is an example to send an HTML email from your machine. Here it is assumed that your localhost is connected to the internet and capable enough to send an email. Same time make sure all the jar files from Java Email API package and JAF package ara available in CLASSPATH. This example is very similar to previous one, except here we are using setContent() method to set content whose second argument is "text/html" to specify that the HTML content is included in the message. Using this example, you can send as big as HTML content you like.
< % @p a g ei m p o r t = " j a v a . i o . * , j a v a . u t i l . * , j a v a x . m a i l . * " % > < % @p a g ei m p o r t = " j a v a x . m a i l . i n t e r n e t . * , j a v a x . a c t i v a t i o n . * " % > < % @p a g ei m p o r t = " j a v a x . s e r v l e t . h t t p . * , j a v a x . s e r v l e t . * "% > < % S t r i n gr e s u l t ; / /R e c i p i e n t ' se m a i lI Dn e e d st ob em e n t i o n e d . S t r i n gt o=" a b c d @ g m a i l . c o m " ; / /S e n d e r ' se m a i lI Dn e e d st ob em e n t i o n e d S t r i n gf r o m=" m c m o h d @ g m a i l . c o m " ; / /A s s u m i n gy o ua r es e n d i n ge m a i lf r o ml o c a l h o s t S t r i n gh o s t=" l o c a l h o s t " ; / /G e ts y s t e mp r o p e r t i e so b j e c t P r o p e r t i e sp r o p e r t i e s=S y s t e m . g e t P r o p e r t i e s ( ) ; / /S e t u pm a i ls e r v e r p r o p e r t i e s . s e t P r o p e r t y ( " m a i l . s m t p . h o s t " ,h o s t ) ; / /G e tt h ed e f a u l tS e s s i o no b j e c t . S e s s i o nm a i l S e s s i o n=S e s s i o n . g e t D e f a u l t I n s t a n c e ( p r o p e r t i e s ) ; t r y { / /C r e a t ead e f a u l tM i m e M e s s a g eo b j e c t . M i m e M e s s a g em e s s a g e=n e wM i m e M e s s a g e ( m a i l S e s s i o n ) ; / /S e tF r o m :h e a d e rf i e l do ft h eh e a d e r . m e s s a g e . s e t F r o m ( n e wI n t e r n e t A d d r e s s ( f r o m ) ) ; / /S e tT o :h e a d e rf i e l do ft h eh e a d e r . m e s s a g e . a d d R e c i p i e n t ( M e s s a g e . R e c i p i e n t T y p e . T O , n e wI n t e r n e t A d d r e s s ( t o ) ) ; / /S e tS u b j e c t :h e a d e rf i e l d m e s s a g e . s e t S u b j e c t ( " T h i si st h eS u b j e c tL i n e ! " ) ; / /S e n dt h ea c t u a lH T M Lm e s s a g e ,a sb i ga sy o ul i k e m e s s a g e . s e t C o n t e n t ( " < h 1 > T h i si sa c t u a lm e s s a g e < / h 1 > " , " t e x t / h t m l ") ; / /S e n dm e s s a g e T r a n s p o r t . s e n d ( m e s s a g e ) ; r e s u l t=" S e n tm e s s a g es u c c e s s f u l l y . . . . " ; } c a t c h( M e s s a g i n g E x c e p t i o nm e x ){ m e x . p r i n t S t a c k T r a c e ( ) ; r e s u l t=" E r r o r :u n a b l et os e n dm e s s a g e . . . . " ; } % > < h t m l > < h e a d >
www.tutorialspoint.com/cgi-bin/printpage.cgi
3/6
4/17/13
< t i t l e > S e n dH T M LE m a i lu s i n gJ S P < / t i t l e > < / h e a d > < b o d y > < c e n t e r > < h 1 > S e n dE m a i lu s i n gJ S P < / h 1 > < / c e n t e r > < pa l i g n = " c e n t e r " > < % o u t . p r i n t l n ( " R e s u l t :"+r e s u l t+" \ n " ) ; % > < / p > < / b o d y > < / h t m l >
Now try to use above JSP to send HTML message on a given email ID.
www.tutorialspoint.com/cgi-bin/printpage.cgi
4/6
4/17/13
m e s s a g e . s e t S u b j e c t ( " T h i si st h eS u b j e c tL i n e ! " ) ; / /C r e a t et h em e s s a g ep a r t B o d y P a r tm e s s a g e B o d y P a r t=n e wM i m e B o d y P a r t ( ) ; / /F i l lt h em e s s a g e m e s s a g e B o d y P a r t . s e t T e x t ( " T h i si sm e s s a g eb o d y " ) ; / /C r e a t eam u l t i p a rm e s s a g e M u l t i p a r tm u l t i p a r t=n e wM i m e M u l t i p a r t ( ) ; / /S e tt e x tm e s s a g ep a r t m u l t i p a r t . a d d B o d y P a r t ( m e s s a g e B o d y P a r t ) ; / /P a r tt w oi sa t t a c h m e n t m e s s a g e B o d y P a r t=n e wM i m e B o d y P a r t ( ) ; S t r i n gf i l e n a m e=" f i l e . t x t " ; D a t a S o u r c es o u r c e=n e wF i l e D a t a S o u r c e ( f i l e n a m e ) ; m e s s a g e B o d y P a r t . s e t D a t a H a n d l e r ( n e wD a t a H a n d l e r ( s o u r c e ) ) ; m e s s a g e B o d y P a r t . s e t F i l e N a m e ( f i l e n a m e ) ; m u l t i p a r t . a d d B o d y P a r t ( m e s s a g e B o d y P a r t ) ; / /S e n dt h ec o m p l e t em e s s a g ep a r t s m e s s a g e . s e t C o n t e n t ( m u l t i p a r t) ; / /S e n dm e s s a g e T r a n s p o r t . s e n d ( m e s s a g e ) ; S t r i n gt i t l e=" S e n dE m a i l " ; r e s u l t=" S e n tm e s s a g es u c c e s s f u l l y . . . . " ; } c a t c h( M e s s a g i n g E x c e p t i o nm e x ){ m e x . p r i n t S t a c k T r a c e ( ) ; r e s u l t=" E r r o r :u n a b l et os e n dm e s s a g e . . . . " ; } % > < h t m l > < h e a d > < t i t l e > S e n dA t t a c h e m e n tE m a i lu s i n gJ S P < / t i t l e > < / h e a d > < b o d y > < c e n t e r > < h 1 > S e n dA t t a c h e m e n tE m a i lu s i n gJ S P < / h 1 > < / c e n t e r > < pa l i g n = " c e n t e r " > < % o u t . p r i n t l n ( " R e s u l t :"+r e s u l t+" \ n " ) ; % > < / p > < / b o d y > < / h t m l >
Now try to run above JSP to send a file as an attachment along with a message on a given email ID.
www.tutorialspoint.com/cgi-bin/printpage.cgi
5/6
4/17/13
Once you have all the information, you can use above mentioned programs to send email.
www.tutorialspoint.com/cgi-bin/printpage.cgi
6/6