1 16 17 package org.springframework.mail.cos; 18 19 import java.io.IOException ; 20 import java.io.PrintStream ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import com.oreilly.servlet.MailMessage; 25 26 import org.springframework.mail.MailException; 27 import org.springframework.mail.MailParseException; 28 import org.springframework.mail.MailSendException; 29 import org.springframework.mail.MailSender; 30 import org.springframework.mail.SimpleMailMessage; 31 32 49 public class CosMailSenderImpl implements MailSender { 50 51 private String host; 52 53 56 public void setHost(String host) { 57 this.host = host; 58 } 59 60 public void send(SimpleMailMessage simpleMessage) throws MailException { 61 send(new SimpleMailMessage[] {simpleMessage}); 62 } 63 64 public void send(SimpleMailMessage[] simpleMessages) throws MailException { 65 Map failedMessages = new HashMap (); 66 67 for (int i = 0; i < simpleMessages.length; i++) { 68 SimpleMailMessage simpleMessage = simpleMessages[i]; 69 70 if (simpleMessage.getReplyTo() != null) { 71 throw new MailParseException("CosMailSenderImpl does not support replyTo field - " + simpleMessage); 72 } 73 if (simpleMessage.getSentDate() != null) { 74 throw new MailParseException("CosMailSenderImpl does not support sentDate field - " + simpleMessage); 75 } 76 77 try { 78 MailMessage cosMessage = new MailMessage(this.host); 79 cosMessage.from(simpleMessage.getFrom()); 80 if (simpleMessage.getTo() != null) { 81 for (int j = 0; j < simpleMessage.getTo().length; j++) { 82 cosMessage.to(simpleMessage.getTo()[j]); 83 } 84 } 85 if (simpleMessage.getCc() != null) { 86 for (int j = 0; j < simpleMessage.getCc().length; j++) { 87 cosMessage.cc(simpleMessage.getCc()[j]); 88 } 89 } 90 if (simpleMessage.getBcc() != null) { 91 for (int j = 0; j < simpleMessage.getBcc().length; j++) { 92 cosMessage.bcc(simpleMessage.getBcc()[j]); 93 } 94 } 95 cosMessage.setSubject(simpleMessage.getSubject()); 96 PrintStream textStream = cosMessage.getPrintStream(); 97 textStream.print(simpleMessage.getText()); 98 cosMessage.sendAndClose(); 99 } 100 catch (IOException ex) { 101 failedMessages.put(simpleMessage, ex); 102 } 103 } 104 105 if (!failedMessages.isEmpty()) { 106 throw new MailSendException(failedMessages); 107 } 108 } 109 110 } 111 | Popular Tags |