KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > util > MailUtil


1 /*
2  * Created on 27-Feb-2003
3  */

4 package xpetstore.util;
5
6 import java.util.Date JavaDoc;
7
8 import javax.activation.DataHandler JavaDoc;
9
10 import javax.mail.MessagingException JavaDoc;
11 import javax.mail.Session JavaDoc;
12 import javax.mail.Transport JavaDoc;
13 import javax.mail.internet.AddressException JavaDoc;
14 import javax.mail.internet.InternetAddress JavaDoc;
15 import javax.mail.internet.MimeMessage JavaDoc;
16
17 import javax.naming.InitialContext JavaDoc;
18 import javax.naming.NamingException JavaDoc;
19
20
21 /**
22  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
23  */

24 public class MailUtil
25 {
26     //~ Static fields/initializers ---------------------------------------------
27

28     public static final String JavaDoc MAIL_SESSION = "java:comp/env/mail/xpetstore/MailSession";
29
30     //~ Methods ----------------------------------------------------------------
31

32     public static void send( String JavaDoc to,
33                              String JavaDoc subject,
34                              String JavaDoc body )
35         throws NamingException JavaDoc,
36                    AddressException JavaDoc,
37                    MessagingException JavaDoc
38     {
39         InitialContext JavaDoc ic = new InitialContext JavaDoc( );
40         Session JavaDoc session = ( Session JavaDoc ) ic.lookup( MAIL_SESSION );
41         javax.mail.Message JavaDoc msg = new MimeMessage JavaDoc( session );
42
43         msg.setFrom( );
44         msg.setRecipients( javax.mail.Message.RecipientType.TO, InternetAddress.parse( to, false ) );
45         msg.setSubject( subject );
46
47         msg.setDataHandler( new DataHandler JavaDoc( body, "text/plain" ) );
48         msg.setHeader( "X-Mailer", "JavaMailer" );
49         msg.setSentDate( new Date JavaDoc( ) );
50
51         Transport.send( msg );
52     }
53 }
54
Popular Tags