⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendpassword.java

📁 介绍了j2ee开发常用的学习知识,如servlet,javamail,EJB等知识,并有项目源码.
💻 JAVA
字号:
/*
 * SendPassword.java
 *
 * Created on 2007年11月30日, 下午4:13
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.logon;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.mail.Address;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.NamingException;

/**
 * 实体类 SendPassword
 *
 * @author hyl
 */
@MessageDriven(mappedName = "jms/SendPassword", activationConfig =  {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
    @ActivationConfigProperty(propertyName = "clientId", propertyValue = "SendPassword"),
    @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "SendPassword")
})
public class SendPassword implements MessageListener {
    
    @Resource(name = "mymail")
    private Session mymail;
    
    @EJB
    private UserBeanFacadeLocal userBeanFacade;
    
    /** Creates a new instance of SendPassword */
    public SendPassword() {
    }
    
    public void onMessage(Message message) {
        TextMessage tm=(TextMessage)message;
        //从消息中获取用户ID
        String id="";
        try{
            id = tm.getText();
        }catch(JMSException e){
            System.out.println(e.toString());
            return;
        }
        String password="";
        String email="";
        List users = userBeanFacade.findAll();
        boolean flag=true;
        for (Iterator it = users.iterator(); it.hasNext()&&flag;) {
            UserBean ub = (UserBean) it.next();
            if(ub.getUsername().equals(id)){
                password=ub.getPassword();
                email=ub.getEmail();
                flag=false;
            }
        }
        if(!flag){//发送邮件
            MimeMessage message1 = new MimeMessage(mymail);
            try{
            message1.setSubject("password");
            message1.setSentDate(new Date());
            String content=new String("you pasword is: ");
            message1.setText(content+password);
            Address toAddress = new InternetAddress(email);
            message1.addRecipient(javax.mail.Message.RecipientType.TO,toAddress);
            Transport.send(message1);
            }catch(Exception e){
                System.out.println(e.toString());
            }
        }
    }
    
    private void sendMail(String email, String subject, String body) throws NamingException, MessagingException {
        MimeMessage message = new MimeMessage(mymail);
        message.setSubject(subject);
        message.setRecipients(RecipientType.TO, InternetAddress.parse(email, false));
        message.setText(body);
        Transport.send(message);
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -