You are on page 1of 6

4/17/13

JSP - Sending Email

JSP - SENDING EMAIL


http://www.tutorialspoint.com /jsp/jsp_sending_em ail.htm
Copy r ig h t t u t or ia lspoin t .com

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.

Send a Simple Email:


Here is an example to send a simple 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.
< % @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 ! " ) ;

www.tutorialspoint.com/cgi-bin/printpage.cgi

1/6

4/17/13

JSP - Sending Email

/ /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:

SEND EMAIL USING JSP


Result: Sent message successfully....

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

Send an HTML Email:

www.tutorialspoint.com/cgi-bin/printpage.cgi

2/6

4/17/13

JSP - Sending Email

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

JSP - Sending Email

< 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.

Send Attachment in Email:


Here is an example to send an email with attachment from your machine:
< % @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

www.tutorialspoint.com/cgi-bin/printpage.cgi

4/6

4/17/13

JSP - Sending Email

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.

User Authentication Part:


If it is required to provide user ID and Password to the email server for authentication purpose then you can set these properties as follows:

www.tutorialspoint.com/cgi-bin/printpage.cgi

5/6

4/17/13

JSP - Sending Email

p r o p s . s e t P r o p e r t y ( " m a i l . u s e r " ," m y u s e r " ) ; p r o p s . s e t P r o p e r t y ( " m a i l . p a s s w o r d " ," m y p w d " ) ;

Rest of the email sending mechanism would remain as explained above.

Using Forms to send email:


You can use HTML form to accept email parameters and then you can use request object to get all the information as follows:
S t r i n gt o=r e q u e s t . g e t P a r a m e t e r ( " t o " ) ; S t r i n gf r o m=r e q u e s t . g e t P a r a m e t e r ( " f r o m " ) ; S t r i n gs u b j e c t=r e q u e s t . g e t P a r a m e t e r ( " s u b j e c t " ) ; S t r i n gm e s s a g e T e x t=r e q u e s t . g e t P a r a m e t e r ( " b o d y " ) ;

Once you have all the information, you can use above mentioned programs to send email.

www.tutorialspoint.com/cgi-bin/printpage.cgi

6/6

You might also like