以前写的,在MDB(消息驱动bean)里发邮件

类别:Java 点击:0 评论:0 推荐:
public void send(String to,String cname,String pas)
    {
        try
        {

            String host = "";
            String from = "";
            //发送的邮件
            String username = "";
            String password = "";

            // Get system properties
            Properties props = new Properties();

            // Setup mail server
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            // Get session
            Session session = Session.getDefaultInstance(props);

            // watch the mail commands go by to the mail server
            session.setDebug(true);

            // Define message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(javax.mail.Message.RecipientType.TO,
                                 new InternetAddress(to));
            String content=cname+"你好!你在music store的注册信息是    emial:"+to+"    密码是:"+pas;
            message.setSubject("music store 用户注册信息");
            message.setText(content);
            //        message.setContentLanguage();
            //        FileTypeMap
            //
            //

            //        message.setDataHandler(
            //         new DataHandler(source));
            // Send message
            message.setSentDate(new Date());
            message.saveChanges();
            Transport transport = session.getTransport("smtp");
            transport.connect(host, username, password);
            transport.sendMessage(message, message.getAllRecipients());
            System.out.println("over");
            transport.close();

        }
        catch (Exception e)
        {
            System.out.println(e);
        }

    }

    public void setMessageDrivenContext(MessageDrivenContext
                                        messageDrivenContext)
    {
        this.messageDrivenContext = messageDrivenContext;
    }
}

本文地址:http://com.8s8s.com/it/it10003.htm