MailerEJB.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package day21ex.mailer;
- import java.util.Date;
- import java.util.Locale;
- import java.util.Properties;
- import javax.naming.InitialContext;
- import javax.activation.DataHandler;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Transport;
- import javax.mail.Session;
- import javax.mail.Multipart;
- import javax.mail.internet.MimeMultipart;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.InternetAddress;
- import java.rmi.RemoteException;
- import javax.ejb.EJBException;
- import javax.ejb.FinderException;
- import javax.ejb.SessionBean;
- import javax.ejb.SessionContext;
- // Session Bean implementation of MailerEJB.
- // Used to send a mail message confirmation such as an email
- // to a student after a registration into courses is completed.
- public class MailerEJB implements SessionBean {
- private Session mailSession = null;
- public void sendMail(String to, String body)throws URSMailerException {
- try {
- MimeMessage msg = new MimeMessage(mailSession);
- msg.setFrom();
- InternetAddress dest = new InternetAddress(to);
- msg.setSubject("Testing STYEJB JavaMail");
- msg.setRecipient(Message.RecipientType.TO, dest);
- msg.setSentDate(new Date());
- msg.setHeader("X-Mailer", "JavaMailer");
- msg.setContent(body, "text/plain");
- Transport.send(msg);
- } catch (Exception e) {
- e.printStackTrace();
- throw new URSMailerException("Failure while sending email");
- }
- }
- public void ejbCreate() {
- try {
- InitialContext ctx = new InitialContext();
- mailSession = (Session) ctx.lookup("java:comp/env/mail/Mail");
- } catch (javax.naming.NamingException e) {
- e.printStackTrace();
- }
- }
- public void ejbPostCreate() {}
- public void ejbActivate() {}
- public void ejbPassivate() {}
- public void ejbRemove() {}
- public void setSessionContext(javax.ejb.SessionContext ec) {}
- }