1 4 package xpetstore.util; 5 6 import java.util.Date ; 7 8 import javax.activation.DataHandler ; 9 10 import javax.mail.MessagingException ; 11 import javax.mail.Session ; 12 import javax.mail.Transport ; 13 import javax.mail.internet.AddressException ; 14 import javax.mail.internet.InternetAddress ; 15 import javax.mail.internet.MimeMessage ; 16 17 import javax.naming.InitialContext ; 18 import javax.naming.NamingException ; 19 20 21 24 public class MailUtil 25 { 26 28 public static final String MAIL_SESSION = "java:comp/env/mail/xpetstore/MailSession"; 29 30 32 public static void send( String to, 33 String subject, 34 String body ) 35 throws NamingException , 36 AddressException , 37 MessagingException 38 { 39 InitialContext ic = new InitialContext ( ); 40 Session session = ( Session ) ic.lookup( MAIL_SESSION ); 41 javax.mail.Message msg = new MimeMessage ( 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 ( body, "text/plain" ) ); 48 msg.setHeader( "X-Mailer", "JavaMailer" ); 49 msg.setSentDate( new Date ( ) ); 50 51 Transport.send( msg ); 52 } 53 } 54 | Popular Tags |