1:简单邮件
简单邮件中是可以实现邮件群发的功能的,区别是收件人可以是一个数组。
package com; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; /** * 本类测试简单邮件 * * @author sunny * */ public class SingleMailSend { public static void main(String args[]){ JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 设定mail server senderImpl.setHost("192.168.1.1"); senderImpl.setUsername("username"); senderImpl.setPassword("password"); // 建立邮件消息 SimpleMailMessage mailMessage = new SimpleMailMessage(); // 设置收件人,寄件人 // String[] array = new String[] {"[email protected]","[email protected]"}; // mailMessage.setTo(array); mailMessage.setTo("[email protected]"); mailMessage.setFrom("[email protected]"); mailMessage.setSubject("测试邮件群发!"); mailMessage.setText("测试我的简单邮件发送机制!!"); // 发送邮件 senderImpl.send(mailMessage); System.out.println("邮件发送成功....."); } }
2:HTML邮件的发送
package com; import javax.mail.internet.MimeMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; /** * 本类测试html邮件 * @author sunny * */ public class HTMLMailDemo { /** * @param args */ public static void main(String[] args) throws Exception { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); //设定mail server senderImpl.setHost("192.168.1.1"); senderImpl.setUsername("username"); senderImpl.setPassword("password"); //建立邮件消息,发送简单邮件和html邮件的区别 MimeMessage mailMessage = senderImpl.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); //设置收件人,寄件人 messageHelper.setTo("[email protected]"); messageHelper.setFrom("[email protected]"); messageHelper.setSubject("测试HTML邮件!"); //true 表示启动HTML格式的邮件 messageHelper .setText( "<html><head></head><body><h1>hello!!zhangjian</h1></body></html>", true); //发送邮件 senderImpl.send(mailMessage); System.out.println("邮件发送成功....."); } }
3:本类测试邮件中嵌套图片
package com; import java.io.File; import javax.mail.internet.MimeMessage; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; /** * 本类测试邮件中嵌套图片 * @author sunny * */ public class AttachedImageMail { public static void main(String[] args) throws Exception{ JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); //设定mail server senderImpl.setHost("192.168.1.2"); senderImpl.setUsername("username"); senderImpl.setPassword("pssword"); //建立邮件消息,发送简单邮件和html邮件的区别 MimeMessage mailMessage = senderImpl.createMimeMessage(); //注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用, multipart模式 MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true); //设置收件人,寄件人 messageHelper.setTo("[email protected]"); messageHelper.setFrom("[email protected]"); messageHelper.setSubject("测试邮件中嵌套图片!!"); //true 表示启动HTML格式的邮件 messageHelper.setText("<html><head></head><body><h1>hello!!zhangjian</h1>" + "<img src=\"cid:aaa\"/></body></html>",true); FileSystemResource img = new FileSystemResource(new File("c:/aaa.jpg")); messageHelper.addInline("aaa",img); //发送邮件 senderImpl.send(mailMessage); System.out.println("邮件发送成功....."); } }
4: 测试邮件中包含附件
package com; import java.io.File; import javax.mail.internet.MimeMessage; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; public class AttachedFileMail { /** * 本类测试的是关于邮件中带有附件的例子 * * @param args */ public static void main(String[] args) throws Exception{ JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 设定mail server senderImpl.setHost("192.168.1.2"); senderImpl.setUsername("username"); senderImpl.setPassword("password"); // 建立邮件消息,发送简单邮件和html邮件的区别 MimeMessage mailMessage = senderImpl.createMimeMessage(); // 注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用, ultipart模式 MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true); // 设置收件人,寄件人 messageHelper.setTo("[email protected]"); messageHelper.setFrom("[email protected]"); messageHelper.setSubject("测试邮件中上传附件!!"); // true 表示启动HTML格式的邮件 messageHelper.setText("<html><head></head><body><h1>你好:附件中有关于CSS的学习资料!</h1></body></html>",true); FileSystemResource file = new FileSystemResource(new File("d:/CSS110.rar")); // 这里的方法调用和插入图片是不同的。 messageHelper.addAttachment("CSS110.rar",file); // 发送邮件 senderImpl.send(mailMessage); System.out.println("邮件发送成功....."); } }
5.spring和quartz进行定时邮件发送
一 发送邮件的基类:
package com.spring.mail; import org.springframework.mail.MailSender; //import org.springframework.mail.javamail.JavaMailSender; public abstract class BaseMailSender { protected String to; protected String from; protected String subject; protected MailSender sender; //当发送的邮件为简单邮件时用MailSender即可; //protected JavaMailSender sender; //当发送的邮件为MIMI时用JavaMailSender public void setTo(String to) { this.to = to; } public void setFrom(String from) { this.from = from; } public void setSubject(String subject) { this.subject = subject; } public void setJavaMailSender(MailSender sender) {//(JavaMailSender sender) { this.sender = sender; } }
发送邮件调用的类
package com.spring.mail; import javax.mail.MessagingException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.SimpleMailMessage; public class SimpleHtmlMailSender extends BaseMailSender { public void sendMessage() throws MessagingException { String textStr = "Helo!!!!!!!!!!!";//发送邮件的消息主体 SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo(to); msg.setFrom(from); msg.setSubject(subject); msg.setText(textStr); sender.send(msg); } public void doIt() throws Exception { ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext( new String[] { "ApplicationContext.xml" }); SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx1 .getBean("messageSender"); sender.sendMessage(); System.out.println("发送成功"); } }
配置文件ApplicationContext:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans public "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd "> <beans> <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host"> <value>smtp.163.com</value> </property> <property name="username"> <value>tomcat0506</value> </property> <property name="password"> <value>wes</value> </property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <bean id="messageSender" class="com.spring.mail.SimpleHtmlMailSender"> <property name="javaMailSender"> <ref bean="sender"/> </property> <property name="to"> <value>[email protected]</value> </property> <property name="from"> <value>[email protected]</value> </property> <property name="subject"> <value>test</value> </property> </bean> <bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- <property name="jobClass"> <value>com.spring.mail.SimpleHtmlMailSender</value> </property>--> <property name="targetObject"><ref bean="messageSender"/></property> <property name="targetMethod"><value>doIt</value></property> </bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property> <property name="startDelay"> <!-- 3 seconds --> <value>3000</value> </property> <property name="repeatInterval"> <!-- repeat every 6 seconds --> <value>6000</value> </property> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="methodInvokingJobDetail"/></property> <property name="cronExpression"> <value>0 0 6,12,20 * * ?</value> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="simpleTrigger"/> </list> </property> </bean> </beans>
二 mimi邮件发送
在编写发送邮件程序时需要用到的类有一下几个:
org.springframework.mail.javamail.JavaMailSender;
//此类继承了spring里面的
//MailSender接口--定义了两个简单的sender方法,用于发送简单的邮件信息,参数类型为:SimpleMailMessage([])
//JavaMailSender类继承了上面方法后支持MIMI邮件,可以发MIMI消息
org.springframework.context.support.ClassPathXmlApplicationContext;
//spring里面读取配置文件时需要用到的类
org.springframework.mail.javamail.MimeMessageHelper;
//在发送MIMI消息时需要用到的辅助类
javax.mail.MessagingException;
javax.mail.internet.MimeMessage;
//J2EE里面支持发送MIMe信息需要的类
例子:
发送邮件的基类
BaseMailSender .java:
import org.springframework.mail.javamail.JavaMailSender; public abstract class BaseMailSender { protected String to; protected String from; protected String subject; protected JavaMailSender sender;//当发送MIMI消息时用到的类 public void setTo(String to) { this.to = to; } public void setFrom(String from) { this.from = from; } public void setSubject(String subject) { this.subject = subject; } public void setJavaMailSender(JavaMailSender sender) { this.sender = sender; } }
发送邮件的类:
package com.spring.mail; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.MimeMessageHelper; public class SimpleHtmlMailSender extends BaseMailSender { public void sendMessage() throws MessagingException { //String htmlHead = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body>"; //String htmlBody = ""; //String htmlEnd = ""; // StringBuffer mailMessage = new StringBuffer(); //mailMessage.append(htmlHead); //mailMessage.append(htmlBody); //mailMessage.append(htmlEnd); //当发送的邮件为简单邮件时注销此句 String textStr = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href=''>^_^!</a></h1></body></html>"; //String textStr = "Helo could you see this email?";//发送邮件的消息主体 //SimpleMailMessage msg = new SimpleMailMessage(); //当发送邮件为简单邮件时把此句注销即可 MimeMessage msg = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true, "GB2312"); helper.setTo(to); helper.setFrom(from); helper.setSubject(subject); helper.setText(textStr, true); /* msg.setTo(to); msg.setFrom(from); msg.setSubject(subject); msg.setText(textStr); */ sender.send(msg); } public void doIt() throws Exception {//static void main(String[] args) throws Exception {// //ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "springexample-creditaccount.xml" }); ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( new String[] { "ApplicationContext.xml" }); SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx .getBean("messageSender"); sender.sendMessage(); System.out.println("发送成功"); } }
配置文件同上!