利用spring-mail 寫的超級smart-mail傳送器

biqing0427發表於2007-12-04

[@more@]

應用spring:

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

lib:

javamail-1.4.1:mail.jar

jaf-1.1:activation.jar

import org.springframework.mail.MailException; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; public class SenderManagerImpl implements SenderManager { private MailSender mailSender; private SimpleMailMessage message; public void placeMessage(Message message) { SimpleMailMessage msg = new SimpleMailMessage(this.message); if (message.getMail() != null) msg.setTo(message.getMail()); if (message.getContent() != null) msg.setText(message.getContent()); if (message.getSubject() != null) msg.setSubject(message.getSubject()); if (message.getCc() != null) msg.setCc(message.getCc()); if (message.getCcMore() != null) msg.setCc(message.getCcMore()); try { mailSender.send(msg); } catch (MailException e) { System.out.println(e.getMessage()); System.out.println(e.getStackTrace()); } } public MailSender getMailSender() { return mailSender; } public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } public SimpleMailMessage getMessage() { return message; } public void setMessage(SimpleMailMessage message) { this.message = message; }; } public class Message { String name = ""; String mail = ""; String content = ""; String subject = ""; String cc = ""; String[] ccMore = null; public Message() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getCc() { return cc; } public void setCc(String cc) { this.cc = cc; } public String[] getCcMore() { return ccMore; } public void setCcMore(String[] ccMore) { this.ccMore = ccMore; }; } imap.net-m.de XWZHOU88!@#$ hzhou hero.zhou@net-m.de spring-mail public void testMail(){ ApplicationContext ctx=new FileSystemXmlApplicationContext("conf/spring/bean.xml"); SenderManager senderManager = (SenderManager) ctx.getBean("senderManager"); Message message = new Message(); message.setContent("this is my first mailProcess using spring-mail"); message.setMail("biqing0427@163.com"); message.setName("me"); message.setSubject("hello hero,this is me"); senderManager.placeMessage(message); //senderManager. }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9879276/viewspace-988490/,如需轉載,請註明出處,否則將追究法律責任。

相關文章