所需要的核心包:
1.activation.jar ----这个包是JAF框架中的
2.mail.jar
3.spring2.5.6.jar
注意:用spring发送邮件时可以向新浪邮箱发邮件
bean的配置文件内容设置:applicationContextMailSender.xml
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.qq.com</value>
</property>
<property name="username">
<value>邮箱名</value>
</property>
<property name="password">
<value>邮箱名密码</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<!-- 设置超时吧 -->
<prop key="mail.smtp.timeout">2500</prop>
</props>
</property>
</bean>
下面是测试类:Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
//测试一个简单的送邮件 的方法
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//ApplicationContext ctx = new FileSystemXmlApplicationContext("D:/28-svn-bak/SpringMail/src/applicationContextMailSender.xml");
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextMailSender.xml");
MailSender sender = (MailSender)ctx.getBean("mailSender");
SimpleMailMessage smm = new SimpleMailMessage();
smm.setTo("收件人的邮件");//[email protected]
smm.setFrom("发件人的邮件");
smm.setSubject("你好啊发發發發");
smm.setText("你好啊啊职");
sender.send(smm);
}
}
下面是spring邮件的简述:
###spring邮件抽象层的主要包为 org.springframework.mail。它包括发送电子邮件的主要接口MailSender,还有一个简单邮件信息
的实现类SimpleMailMessage。以及基于MailException的异常继承树。为了使用javaMial中的MimeMessage,Spring另外提供一个
JavaMailSender接口,以及支持MimeMessage的回调接口MimeMessagePreparator。
###基本上,这三个类代表了Spring邮件抽象层的心。MailSender提拱简单的邮件发送方法,面JavaMailSender则可用于发送MimeMessage信息
,MimeMessagePreparator回调接口用于创建MimeMessage实例。
上面三个接口的源码:
1.MailSender ----interface
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
public interface MailSender {
/**
* 发送简单信息
* @param simpleMessage 需要发送的信息
* @throws MailException 认证失败或发送失败时抛出异常
*/
public void send(SimpleMailMessage simpleMessage) throws MailException;
/**
* 一次发送多条简单信息,发达以数组式组织的多条信息
* @param simpleMessages 需发送的简单信息数组
* @throws MailException 认证失败或发送失败时抛出异常
*/
public void send(SimpleMailMessage[] simpleMessages) throws MailException;
}
2.JavaMailSender -----interface
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.MimeMessagePreparator;
//这个是发送复杂邮件的接口
public interface JavaMailSender extends MailSender {
//主要是用到MimeMessage这个封装内容
public MimeMessage createMimeMessage();
public void send(MimeMessage mimeMessage) throws MailException;
public void send(MimeMessage[] mimeMessages) throws MailException;
//这个是要进行自己扩展的方法(下面的那个接口 3)
public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException;
public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException;
}
3.MimeMessagePreparator-----interface
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
//这个是给自己设置邮件信息封装的类
public interface MimeMessagePreparator {
//要实现这个方法
void prepare(MimeMessage mimeMessage) throws MessagingException;
}