接上篇,在业务逻辑中已经发送JMS消息,而接下来的消息驱动Bean作为JMS消息监听器,主要是负责监听指定的JMS消息,此时已经接受到JMS的消息,那么MDB的onMessage()方法会被触发.调用SimpleMailSender类,发送邮件.
import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.MapMessage; import javax.jms.Message; import org.jnqqls.auction.util.SimpleMailSender; @MessageDriven(activationConfig = { /* 指定MDB所监听消息目的的类型 */ @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), /* 指定MDB所监听的消息目的的JNDI绑定名 */ @ActivationConfigProperty(propertyName = "destiniation", propertyValue = "Queue") }, /* 让MDB的Bean实现类间接地实现MessageListener接口 */ messageListenerInterface = javax.jms.MessageListener.class, /* 指定MDB所监听的消息目的的JNDI绑定名 */ mappedName = "Queue") public class MailMDB { //实现onMessage方法——当JMS消息被送达消息目的时, //该方法被触发 public void onMessage(Message rawMsg){ try { if(rawMsg instanceof MapMessage){ //将消息强制转换为MapMessage MapMessage msg=(MapMessage) rawMsg; String mailTo=msg.getString("mailTo"); String userName=msg.getString("userName"); //准备发送邮件 SimpleMailSender simpleMailSender = new SimpleMailSender(); //设置smtp服务器地址 simpleMailSender.setSmtpServer("smtp.163.com"); //设置登录的用户名 simpleMailSender.setUsername("用户名"); //设置登录密码 simpleMailSender.setPassword("密码"); //设置收件人地址 simpleMailSender.setTo(mailTo); //设置发送人地址 simpleMailSender.setFrom("发件人地址"); //设置标题 simpleMailSender.setSubject("注册通知"); //设置邮件内容 simpleMailSender.setContent("亲爱的" +userName + ", 您注册网站成功! ); if (simpleMailSender.send()) { System.out.println("恭喜邮件发送成功"); }else{ System.out.println("邮件发送失败"); } } } catch (Exception e) { e.printStackTrace(); } } }
以上内容完成了对消息的接受和处理,并且调用JavaMail的相关工具类,这里是指的SimpleMailSender类,完整的将此类写在下面.
import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; public class SimpleMailSender { // 收件人地址。 private String to; // 发件人地址 private String from; // SMTP服务器 private String smtpServer; // 登录服务器用户名. private String username; // 登录服务器密码 private String password; // 邮件主题 private String subject; // 邮件正文. private String content; // 记录附件集合. List<String> attachments = new ArrayList<>(); public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getSmtpServer() { return smtpServer; } public void setSmtpServer(String smtpServer) { this.smtpServer = smtpServer; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public List<String> getAttachments() { return attachments; } public void setAttachments(List<String> attachments) { this.attachments = attachments; } // 无参数的构造器. public SimpleMailSender() { } public SimpleMailSender(String to, String from, String smtpServer, String username, String password, String subject, String content) { super(); this.to = to; this.from = from; this.smtpServer = smtpServer; this.username = username; this.password = password; this.subject = subject; this.content = content; } /** * 把邮件主题转换为中文 * * @param strText * @return */ public String transferChinese(String strText) { try { strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B"); } catch (Exception e) { e.printStackTrace(); } return strText; } /** * 增加附件,将附件文件名添加到List集合中. * * @param name */ public void attachfile(String name) { attachments.add(name); } /** * 发送邮件 * @return */ /** * @return */ public boolean send(){ //创建邮件Session所需要的Properties对象 Properties props = new Properties(); props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.auth", "true"); //创建Session对象 Session session= Session.getDefaultInstance(props //以匿名内部类的形式创建登录服务器的认证对象. ,new Authenticator() { public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(username, password); } }); try { //构造MimeMessage并设置相关属性. MimeMessage msg = new MimeMessage(session); //设置发件人 msg.setFrom(new InternetAddress(from)); //设置收件人 InternetAddress[] addresses={new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, addresses); //设置邮件主题 subject = transferChinese(subject); msg.setSubject(subject); //构造Multipart Multipart mp = new MimeMultipart(); //向 Multipart中 添加正文 MimeBodyPart mbpContext = new MimeBodyPart(); mbpContext.setText(content); //将BodyPart添加到Multipart容器中. mp.addBodyPart(mbpContext); //向Multipart添加附件. //遍历附件列表,将所有的文件添加到邮件消息里 for (String efile : attachments) { MimeBodyPart mbpFile = new MimeBodyPart(); //通过文件名创建FileDataSource对象. FileDataSource fds = new FileDataSource(efile); //处理附件 mbpFile.setDataHandler(new DataHandler(fds)); mbpFile.setFileName(fds.getName()); //将bodypart添加到Multipart容器中. mp.addBodyPart(mbpFile); } //清空附件列表 attachments.clear(); //向Multipart添加MimeMessage msg.setContent(mp); //设置发送日期 msg.setSentDate(new Date()); //发送邮件 Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); return false; } return true; } }
通过以上的四个步骤,我们基本上完成了用JavaMail来发送邮件通知的业务需求,当然,读者也可以根据JavaMail的相关API来实现接收邮件.这样就类似一个迷你的邮箱系统了.