`
shupili141005
  • 浏览: 118574 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类

Spring 高效批量邮件发送

阅读更多

Gmail 邮件发送配置

 

email.properties 文件:

# Gmail Configuration
email.host=smtp.gmail.com
email.port=587
email.username=******
email.password=******
email.defaultEncoding=UTF-8

 

applicationContext.xml 文件:

    <!-- Email Service -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${email.host}"/>
        <property name="port" value="${email.port}"/>
        <property name="username" value="${email.username}"/>
        <property name="password" value="${email.password}"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.transport.protocol">smtp</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
        <prop key="mail.smtp.host">smtp.gmail.com</prop>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.port">465 </prop>
            </props>
            <!--
            <value>
                mail.smtp.auth=true
            </value>
             -->
        </property>
        <property name="defaultEncoding" value="${email.defaultEncoding}"></property>
    </bean>
    <bean id="emailSender" class="com.tucue.common.util.SimpleEmailSender">
        <property name="mailSender" ref="mailSender"></property>
    </bean>
    <!-- end of Email Service -->

 

FAQ

com .sun .mail .smtp .SMTPSendFailedException: 530   5 .7 . 0  Must  issue  a  STARTTLS  command  first .

 

A

<prop key="mail.smtp.starttls.enable">true</prop>

 

Send Batch Mail

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/mail/MailSender .html

 

Method Summary
 void send (SimpleMailMessage  simpleMessage)
          Send the given simple mail message.
 void send (SimpleMailMessage [] simpleMessages)
          Send the given array of simple mail messages in batch.

 

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/mail/javamail/JavaMailSender .html

 

Method Summary
 MimeMessage createMimeMessage ()
          Create a new JavaMail MimeMessage for the underlying JavaMail Session of this sender.
 MimeMessage createMimeMessage (InputStream  contentStream)
          Create a new JavaMail MimeMessage for the underlying JavaMail Session of this sender, using the given input stream as the message source.
 void send (MimeMessage  mimeMessage)
          Send the given JavaMail MIME message .
 void send (MimeMessage [] mimeMessages)
          Send the given array of JavaMail MIME messages in batch .
 void send (MimeMessagePreparator  mimeMessagePreparator)
          Send the JavaMail MIME message prepared by the given MimeMessagePreparator.
 void send (MimeMessagePreparator [] mimeMessagePreparators)
          Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.

 

方案一:首先获取要发送邮件的所有地址,然后迭代的封装单个 MimeMessage 之后就将其发送出去(即发送一封邮件)。

 

SimpleEmailSender.java 文件

	/**
	 * 功能: 传入邮件接收者地址、标题和内容,然后发送单个邮件服务
	 * @param {@link String} emailAddress
	 * @param {@link String} subject
	 * @param {@link String} content
	 * @throws MessagingException
	 */
	public static void sendSimpleEmail(String receiver
, String subject, String content) throws MessagingException {
		JavaMailSender sender = (JavaMailSender) mailSender;
		
		MimeMessage mimeMessage
 = sender.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
		messageHelper.setFrom("***@gmail
.com");
		messageHelper.setTo(receiver);
		messageHelper.setSubject(subject);
		messageHelper.setText(content, true);
		
		sender.send(mimeMessage
);
	}

 

 UserInfoAction.java 文件

	// 用户 -> 批量邮件发送
	public String sendBatchEmail() {
		if(!emailReceiverList.equals("")) {	// 发送用户邮件
//			String[] receiverList = emailReceiverList.split(",");
			String[] receiverList = new String[20];
			for(int i = 0; i < 20; ++i) {
				receiverList[i] = "shupili141005@126.com";
			}
			long start = System.currentTimeMillis();
			for(String receiver: receiverList
) {
				try {
					SimpleEmailSender.sendSimpleEmail(receiver, emailSubject, emailContent);

				} catch (MessagingException e) {
					e.printStackTrace();
				}
			}
			long end = System.currentTimeMillis();
			System.out.println(end - start);
		} else {// 发送用户组邮件
			List<String> receiverList = userInfoService.getUserEmailByGroupId(groupID);
			for(String receiver: receiverList) {
				try {
					SimpleEmailSender.sendSimpleEmail(receiver, emailSubject, emailContent);
				} catch (MessagingException e) {
					e.printStackTrace();
				}
			}
		}

		return SUCCESS;
	}

 方案二:首先获取要发送邮件的所有地址,然后一起封装成 MimeMessage[] 之后就将其发送出去(即批量发送所有要发送的邮件)。

 

SimpleEmailSender.java 文件

/**
	 * 功能: 传入邮件接收者地址列表、标题和内容,然后发送批量邮件服务
	 * @param {@link List<String>} receiverList
	 * @param {@link String} subject
	 * @param {@link String} content
	 * @throws MessagingException
	 */
	public static void sendBulkEmail(List<String> receiverList
, String subject, String content) throws MessagingException {
		JavaMailSender sender = (JavaMailSender) mailSender;
		int len = receiverList.size();
		MimeMessage[] mimeMessageList
 = new MimeMessage[len];	// 注意:是数组类型

		MimeMessageHelper messageHelper = null;
		
		for(int i = 0; i < len; ++i) {
			mimeMessageList[i]
 = sender.createMimeMessage();
			messageHelper = new MimeMessageHelper(mimeMessageList[i], true, "UTF-8");
			messageHelper.setFrom("tucueservice@gmail.com");
			messageHelper.setTo(receiverList.get(i));
			messageHelper.setSubject(subject);
			messageHelper.setText(content, true);
		}
		
		sender.send(mimeMessageList
);
	}
 

UserInfoAction.java 文件

	
	// 用户 -> 批量邮件发送
	public String sendBatchEmail() {
		if(!emailReceiverList.equals("")) {	// 发送用户邮件
//			String[] receiverList = emailReceiverList.split(",");
			List<String> receiverList
 = new ArrayList<String>(20);
			for(int i = 0; i < 20; ++i) {
				receiverList.add("shupili141005@126.com");
			}
			long start = System.currentTimeMillis();
			try {
//				SimpleEmailSender.sendBulkEmail(Arrays.asList(receiverList), emailSubject, emailContent);
				SimpleEmailSender.sendBulkEmail(receiverList
, emailSubject, emailContent);
			} catch (MessagingException e) {
				e.printStackTrace();
			}
			long end = System.currentTimeMillis();
			System.out.println(end - start);
		} else {// 发送用户组邮件
			List<String> receiverList = userInfoService.getUserEmailByGroupId(groupID);
			try {
				SimpleEmailSender.sendBulkEmail(receiverList, emailSubject, emailContent);
			} catch (MessagingException e) {
				e.printStackTrace();
			}
		}

		return SUCCESS;
	}

 

根据上述两种方案,通过发送 20 封邮件进行测试后发,发现第二种方案(用时 36984 ms)比第一种(81266 ms)的 执行效率 2.1 倍多

 

总结

通过这次实验,感觉自己收获了不少。第一,评价哪种方案优劣,用你需要的数据测试就能体现执行效率,但健壮性和可扩展性就不能一概而论了;第二,要想编写更加高效的代码,就必须对你特别重要(特别是常用基础类)的 API 有足够的熟知。就以这个案例为例,刚开始我没有仔细看过 JavaMailSender 的 API ,导致就不知道用 send (MimeMessage [] mimeMessages) 这个函数,所以也不知道存在第二种方案。后来突然发现有这个函数,就像能不能对先前的发送单个邮件服务加以改进,第二种方案也就是这样萌生的。

分享到:
评论
1 楼 xiong_biao 2011-12-24  

相关推荐

    java开源包1

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包11

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包2

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包3

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包6

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包5

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包10

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包4

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包8

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包7

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包9

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    java开源包101

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    Java资源包01

    Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式...

    JAVA上百实例源码以及开源项目源代码

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    JAVA上百实例源码以及开源项目

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

Global site tag (gtag.js) - Google Analytics