Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.batch.item.mail.javamail;
 
 import java.util.List;
 import java.util.Map;
 
 

A simple org.springframework.batch.item.ItemWriter that can send mail messages. If it fails there is no guarantee about which of the messages were sent, but the ones that failed can be picked up in the error handler. Because the mail protocol is not transactional, failures should be dealt with here if possible rather than allowing them to be rethrown (which is the default).

Delegates the actual sending of messages to a org.springframework.mail.javamail.JavaMailSender, using the batch method org.springframework.mail.javamail.JavaMailSender.send(javax.mail.internet.MimeMessage[]), which normally uses a single server connection for the whole batch (depending on the implementation). The efficiency of for large volumes of messages (repeated calls to the item writer) might be improved by the use of a special org.springframework.mail.javamail.JavaMailSender that caches connections to the server in between calls.

Stateless, so automatically restartable.

Author(s):
Dave Syer
Since:
2.1
 
 public class MimeMessageItemWriter implements ItemWriter<MimeMessage> {
 
 
A org.springframework.mail.javamail.JavaMailSender to be used to send messages in write(java.util.List).

Parameters:
mailSender
 
 	public void setJavaMailSender(JavaMailSender mailSender) {
 		this. = mailSender;
 	}

The handler for failed messages. Defaults to a org.springframework.batch.item.mail.DefaultMailErrorHandler.

Parameters:
mailErrorHandler the mail error handler to set
 
 	public void setMailErrorHandler(MailErrorHandler mailErrorHandler) {
 		this. = mailErrorHandler;
 	}

Check mandatory properties (mailSender).

 
 	public void afterPropertiesSet() throws IllegalStateException {
 		Assert.state( != null"A MailSender must be provided.");
 	}

Parameters:
items the items to send
See also:
org.springframework.batch.item.ItemWriter.write(java.util.List)
	public void write(List<? extends MimeMessageitemsthrows MailException {
		try {
			.send(items.toArray(new MimeMessage[items.size()]));
		}
		catch (MailSendException e) {
			@SuppressWarnings("unchecked")
			Map<ObjectExceptionfailedMessages = e.getFailedMessages();
			for (Entry<ObjectExceptionentry : failedMessages.entrySet()) {
			}
		}
	}
New to GrepCode? Check out our FAQ X