`
j040404
  • 浏览: 14376 次
  • 性别: Icon_minigender_2
  • 来自: 河北
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

JMS_发送邮件

阅读更多

看这个例子之前最好先看下“我的资源”里的《EJB3.0开发Message Driven Bean》教怎样开发MDB,还配有图片,很好的实例

下面看怎样开发JMS发送邮件

首先需要配置个mail文件

路径在:%JBOSS_HOME%\server\all\deploy中有个mail-service.xml

打开它,大致修改成:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server>
<!-- $Id: mail-service.xml,v 1.5.6.1 2005/06/01 06:20:43 starksm Exp $ -->

<server>

  <!-- ==================================================================== -->
  <!-- Mail Connection Factory                                              -->
  <!-- ==================================================================== -->

  <mbean code="org.jboss.mail.MailService"
         name="jboss:service=Mail">
    <attribute name="JNDIName">java:/Mail</attribute>
    <attribute name="User">邮箱的用户名</attribute>
    <attribute name="Password">邮箱的密码</attribute>
    <attribute name="Configuration">
       <!-- Test -->
       <configuration>
          <!-- Change to your mail server prototocol -->
          <property name="mail.store.protocol" value="pop3"/>
          <property name="mail.transport.protocol" value="smtp"/>

          <!-- Change to the user who will receive mail  -->
          <property name="mail.user" value="nobody"/>

          <!-- Change to the mail server  -->
          <property name="mail.pop3.host" value="pop3.163.com"/>

          <!-- Change to the SMTP gateway server -->
          <property name="mail.smtp.host" value="smtp.163.com"/>
   
    <property name="mail.smtp.auth" value="true"/>

          <!-- Change to the address mail will be from  -->
          <property name="mail.from" value="用户名@邮箱(例如:163).com"/>

          <!-- Enable debugging output from the javamail classes -->
          <property name="mail.debug" value="false"/>
       </configuration>
       <depends>jboss:service=Naming</depends>
    </attribute>
  </mbean>

</server>

建立一个ejb3.0的项目

图上的myjboss是我先前建的jboss服务器。

下面是如果没有jboss server建立新的服务器的过程

选择jboss as4.0

 

点击完成以后又回到这个界面,因为之前有服务器了,我就没有新建,然后点完成

 

package examples.Message;

import javax.ejb.*;
import javax.naming.*;
import javax.jms.*;
import java.util.*;
import java.text.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 * MessageBean class must implement two interfaces MessageDrivenBean and
 * MessageListener
 */
@SuppressWarnings( { "serial", "unused" })
@MessageDriven(activationConfig =

{

@ActivationConfigProperty(propertyName = "destinationType",

propertyValue = "javax.jms.Queue"),

@ActivationConfigProperty(propertyName = "destination",

propertyValue = "queue/testQueue")

})
public class MessageBean implements MessageListener {

	private transient MessageDrivenContext context = null;

	/**
	 * a no-argument constructor
	 */
	public MessageBean() {
	}

	/**
	 * called by container during object creation
	 */

	/**
	 * The onMessage Method receives the Message parameter, type casts it to the
	 * appropriate message type and prints the contents of the message. A mail
	 * note is then sent to the message sender
	 */
	public void onMessage(javax.jms.Message msg) {
		try {
			if (msg instanceof MapMessage) {
				MapMessage map = (MapMessage) msg;
				System.out.println("Order   received:   ");
				System.out.println("Order   ID:   " + map.getString("OrderID")
						+ "   Item   ID:   " +

						map.getInt("ItemID") + "   Quanity:   "
						+ map.getInt("Quantity") + "   Unit   Price:   " +

						map.getDouble("UnitPrice"));
				sendNote(map.getString("emailID"));
			} else {
				System.out.println("wrong   message   type");
			}
		} catch (Throwable te) {
			te.printStackTrace();
		}
	}

	/**
	 * Sends an e-mail notification to the recipient e-mail ID specified in the
	 * input parameter
	 */
	private void sendNote(String recipient) {
		try {
			// Initialize JNDI context
			Context initial = new InitialContext();

			// look up the mail server session
			
			javax.mail.Session session = (javax.mail.Session)

			initial.lookup("java:/Mail");
			

			// Create a new mail object
			javax.mail.Message msg = new MimeMessage(session);

			// set various mail properties
			msg.setFrom();

			msg.setRecipients(javax.mail.Message.RecipientType.TO,
					InternetAddress.parse(recipient,

					false));

			msg.setSubject("您有一封信邮件");

			DateFormat dateFormatter = DateFormat.getDateTimeInstance(
					DateFormat.LONG,

					DateFormat.SHORT);

			Date timeStamp = new Date();

			String messageText = "恭喜您,邮件发送成功 "
					+ dateFormatter.format(timeStamp) + ".";

			msg.setText(messageText);

			msg.setSentDate(timeStamp);

			// send the mail message
			Transport.send(msg);

		} catch (Exception e) {
			throw new EJBException(e.getMessage());
		}
	}

}

根据上面代码的包名和类名,把这个类建好。然后会报错,解决过程如下图

 

 

 

点击add external jars..导入之前下载的mail.jar

下载地址是:

http://cds-esd.sun.com/ESD36/JSCDL/javamail/1.4.1/javamail-1_4_1.zip?AuthParam=1212507419_fada7a5c54305290323a361afbe9149d&TicketId=B%2Fw2lR6BSl5JSBRCOV5flgDh&GroupName=CDS&FilePath=/ESD36/JSCDL/javamail/1.4.1/javamail-1_4_1.zip&File=javamail-1_4_1.zip

然后把该项目打包成jar文件,放到%JBOSS_HOME%\server\all\deploy下过程如下

然后测试,也相当于客户端

建立动态web项目

 

在doGet中添加以下代码:

		Context jndiContext = null;
		QueueConnectionFactory queueConnectionFactory = null;
		QueueConnection queueConnection = null;
		QueueSession queueSession = null;
		Queue queue = null;
		QueueSender queueSender = null;
		MapMessage message = null;
		@SuppressWarnings("unused")
		final int NUM_MSGS;

		/*
		 * if ((args.length < 1)) { System.out.println("Usage: java
		 * MessageClient " + ""); System.exit(1); }
		 */

		// Obtain JNDI context and
		// Look up connection factory and queue.
		try {
			jndiContext = new InitialContext();

			Object tmp = jndiContext.lookup("ConnectionFactory");

			queueConnectionFactory = (QueueConnectionFactory) tmp;

			// queueConnection = queueConnectionFactory.createQueueConnection();
			/*
			 * queueConnectionFactory = (QueueConnectionFactory) jndiContext
			 * .lookup("java:comp/env/QueueConnectionFactory");
			 * 
			 * queue = (Queue) jndiContext.lookup(args[0]);
			 */
			queue = (Queue) jndiContext.lookup("queue/testQueue");

		} catch (NamingException e) {

			System.out.println("JNDI   lookup   failed:   " + e.toString());
			System.exit(1);
		}

		/*
		 * Create session from connection; false means session is not
		 * transacted. Create sender and map message. Send messages, varying
		 * text slightly. Send end-of-messages message. Finally, close
		 * connection.
		 */
		try {
			// Create connection.
			queueConnection = queueConnectionFactory.createQueueConnection();

			// Create session from connection
			queueSession = queueConnection.createQueueSession(false,

			QueueSession.AUTO_ACKNOWLEDGE);

			queueConnection.start();

			// Create Sender
			queueSender = queueSession.createSender(queue);

			// Create Map Message
			message = queueSession.createMapMessage();

			// Define several Name/Value pairs
			message.setString("OrderID", "1");

			message.setInt("ItemID", 5);

			message.setInt("Quantity", 50);

			message.setDouble("UnitPrice", 5.00);

			message.setString("emailID", "254164472@qq.com");// 注意,邮箱添自己的

			// Send message
			queueSender.send(message);

		} catch (JMSException e) {
			System.out.println("Exception   occurred:   " + e.toString());
		} finally {
			if (queueConnection != null) {
				try {
					queueConnection.close();
				} catch (JMSException e) {
				}
			}
		}
	

上面代码中,有个邮箱地址,那添自己的邮箱。否则邮件就会发送到254164472.qq.com里了

粘贴完如果报错,可以把鼠标放到错误的那行按住Ctrl+1然后选add library...

 

 

然后部署到服务器上,进行测试

点击完成就可以了,如果没有jboss可以选择(单选按钮)下面那个新建一个服务器

然后点击完成。等jboss启动完

在IE中地址栏输入:http://localhost:8080/JMS_Client/testJMS

如果把上面servlet 中的邮箱地址写成自己的,那么此时就会收到一封邮件。

分享到:
评论

相关推荐

    springboot整合jms进行邮件的发送

    springboot整合jms进行邮件的发送,里面包含了常见的txt格式,html格式,以及图片,附件等。新手必备!

    notify 邮件/短信发送

    实现短信/邮件发送,短信邮件服务商通过插件方式集成,需要jms,发送接口在SendNotifyApi.java

    activemq jms的使用

    实现点对点发送消息,jms通讯机制,可以发送电子邮件

    MQMail:使用AQ,JMS等的多队列邮件。

    发送邮件.. 跑步 $&gt; java -Dfile.encoding = UTF-8 -classpath ... jar org.mail.client.CliMain -i 2000 队列 案例1. Oracle流高级排队 过程(PL / SQL): CREATE OR REPLACE TYPE USER_DEFINED_TYPE AS OBJECT ...

    jms-activemq-example:使用Apache ActiveMQ的Belajar JMS

    JMS与电子邮件不同。 系统要求 Java 玛文 Apache ActiveMQ入门 如何运行应用程序: 提炼 跑步 bin/activemq start 启动 JMS术语 提供者,即面向消息的中间件或充当代理的应用程序,示例之一是Apache ActiveMQ,...

    jmsbrowser:浏览JMS队列和主题

    通过基于Eclipse的强大用户界面轻松地发送,查看和浏览队列和主题中的消息。 特征 在linux和Windows上运行 多个同时连接和视图 将邮件从一台服务器复制到另一台服务器 使用选择器过滤消息 有效负载的XML格式 即时...

    全新JAVAEE大神完美就业实战课程 超150G巨制课程轻松实战JAVAEE课程 就业部分.txt

    ERP_day09JavaMail发送预警邮件_使用Quartz任务调度框架_自动发送邮件 ERP_day10_PIO框架应用_订单导入_导出_HSSF读写Excel表格档案 ERP_day11_CXF框架_红日物流BOS系统_ERP物流信息管理 ERP_day12_Easyui--Thee...

    mule开发2---集成ws、JMS、stmp、file

    本案例集成了webservice调用、发送MQ消息、发送邮件、写日志文件功能。开发环境为Anypoint3.7。下载后将例子中的邮箱改为自己的;MQ服务器使用的是apache的activemq,已一同上传。mq-client工程是mq的客户端,配合...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    7.3.2 通过WebLogic的邮件支持来 发送邮件 290 7.3.3 在JBoss中配置JavaMail 292 7.4 本章小结 294 第8章 会话EJB 295 8.1 EJB概述 296 8.1.1 EJB的概念和意义 296 8.1.2 EJB的发展历史 298 8.1.3 EJB的优势和使用...

    Java源码 SpringMVC Mybatis Shiro Bootstrap Rest Webservice

    4. 文件上传、多线程下载服务化、发送邮件、短信服务化、部门信息服务化、产品信息服务化、信息发布服务化、我的订阅服务化、我的任务服务化、公共链接、我的收藏服务化等 系统模块: 1. 用户管理: 用户信息...

    尚硅谷Java视频教程_Spring Boot视频教程(下)整合篇

    SpringBoot高级-消息-RabbitMQ基本概念简介 15、尚硅谷-SpringBoot高级-消息-RabbitMQ运行机制 16、尚硅谷-SpringBoot高级-消息-RabbitMQ安装测试 17、尚硅谷-SpringBoot高级-消息-RabbitTemplate发送接受消息&序列...

    scm1:企业级进销存管理系统

    用户忘记密码,JMS发送邮件帮助用户找回密码 商品添加,批量删除,修改等功能 管理人员添加,删除及角色权限修改 不通角色通过Filter实现不同权限功能 代码生成 商品的数量及销售情况生成柱形及饼状图形报表供用户...

    spring4.1核心包

    11.spring-jms-4.1.1.RELEASE.jar 简单封装jms api接口 jms: Java消息服务(Java Message Service)应用程序接口 12. spring-messaging-4.1.1.RELEASE.jar 消息发送 13. spring-orm-4.1.1.RELEASE.jar 14. spring-...

    从Java走向Java+EE+.rar

    15.3.2 编写发送邮件的实例 223 15.3.3 编写接收邮件的实例 235 15.4 小结 241 第16章 基于良好设计模式的Spring 243 16.1 Spring简介 243 16.2 实例——用Spring来打招呼 246 16.3 小结 248 第17章 ...

    128元尚硅谷Java视频教程_Spring Boot视频教程(下)整合篇

    17、尚硅谷-SpringBoot高级-消息-RabbitTemplate发送接受消息&序列化机制 18、尚硅谷-SpringBoot高级-消息-@RabbitListener&@EnableRabbit 19、尚硅谷-SpringBoot高级-消息-AmqpAdmin管理组件的使用 20、尚硅谷-...

    springboot学习

    chapter4-5-1:实现邮件发送:简单邮件、附件邮件、嵌入资源的邮件、模板邮件 消息服务 chapter5-1-1:[JMS(未完成)] chapter5-2-1:Spring Boot中使用RabbitMQ 其他功能 chapter6-1-1:使用Spring StateMachine...

    JMSSerializerBundle:轻松序列化和反序列化任何复杂度的数据(支持XML,JSON,YAML)

    JMSSerializerBundle 该捆绑软件将集成到Symfony中。 请打开与新存储库中的库相关的新问题或功能请求。文献资料您可以在其了解有关捆绑软件的更多信息。专业支持有关最终的付费支持,请发送电子邮件至 。

    java6.0源码-dataminer:Maas平台的数据提取工具

    主题,通过电子邮件发送,发送到 jtec 服务器 提供 Jmx 和 REST 接口进行管理 ... N 个连接器:每个连接器都知道如何从目标源和技术中提取数据,但不知道要查找什么,因此可以重用 M 模型:每个企业甚至感兴趣的模型...

    Spring in Action(第2版)中文版

    12.2.1配置邮件发送器 12.2.2构建电子邮件 12.3调度任务 12.3.1使用javatimer调度任务 12.3.2使用quartz调度器 12.3.3按调度计划调用方法 12.4使用jmx管理springbean 12.4.1将springbean输出为mbean 12.4.2...

    Spring in Action(第二版 中文高清版).part2

    12.2.1 配置邮件发送器 12.2.2 构建电子邮件 12.3 调度任务 12.3.1 使用Java Timer调度任务 12.3.2 使用Quartz调度器 12.3.3 按调度计划调用方法 12.4 使用JMX管理Spring Bean 12.4.1 将Spring Bean输出为...

Global site tag (gtag.js) - Google Analytics