`
Technoboy
  • 浏览: 154205 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Spring-mail(1)

阅读更多
1. Java Mail介绍
  JavaEE框架为应用提供了JavaMail接口,通过JavaMail相关的接口可以读取邮件服务器的邮件,并且可以完成邮件的发送过程。JavaMail是JavaEE中的标准API,是对邮件服务器访问过程的封装。使用JavaMail API则不需要编写与邮件服务器交互的详细过程,只需要调用相关接口即可。
  Java API 主要包括四个部分: Session, Message, Transport 和 InternetAddress。
  1.1 Session
   Session定义了全局的和每个用户的与邮件相关的属性。这些属性说明了客户机和服务器如何交流信息。
  mail.store.protocol:检索邮件使用的协议
  mail.transport.protocol:发送邮件使用的协议
  mail.host:服务器主机名
  mail.user:检索邮件或者发送邮件的用户名
  mail.protocol.host:发送邮件服务器或者接受邮件服务器
  mail.protocol.user:登陆特定邮件服务器使用的用户名
  mail.from:指定默认的回复地址
 
  1.2 Message
   一个抽象类,表示单个邮件信息,其属性包括消息类型。地址消息和锁定义的目录结构。一般我们使用其MimeMessage子类。
   Message类的主要方法有两部分,一是在发送邮件时候使用,用于设置邮件的相关信息,包括发送者,接受者,主题等信息。
  如:
 
  setFrom(Address address); //注意这里的参数为javax.mail.Address类型
  setRecipients();
  setSentDate();
  

  二是用于获取邮件的相关信息。
  如:
 Flags getFlags();   //获取与邮件相关的标记属性
 Folder getFolder(); //获取邮件所在的文件夹


1.3 Transport
  一个抽象类,用于发送邮件。我们经常用这两个方法:
public static void send(Message msg); //发送邮件
public static void send(Message msg, Address[] addresses); //第一参数是要发送邮件的本身,第二个参数是邮件发送的目的地,它会忽略邮件中设置的接受者。


1.4 InternetAddress
  InternetAddress就是把用户的Email映射为Internet地址。注意,在Message中确定邮件的接受者和发送者,以及在发送邮件的时候使用的都是Address对象。InternetAddress是Address的子类。经常使用的构造函数:
 
  public InternetAddress(String address);//把字符串构造成InternetAddress


1.5 发送邮件的步骤!
  1)获取Session
  2)将邮件地址解析为InternetAddress,构造Message消息
  3)通过Transport的send方法发送消息。

我们先来通过apache的commons email来简单实现个邮件发送,然后使用spring的mail,并详解spring的mail封装。(本文的commons email为commons-email-1.2)
需要mail.jar和commons-email-1.2.jar。

public class CommonsEmail {
	public static void main(String[] args) throws Exception {
		Email email = new SimpleEmail();
		email.setHostName("smtp.qq.com");
		email.setSmtpPort(25);
		email.setAuthenticator(new DefaultAuthenticator("465553115#qq.com", "your password"));
		email.setFrom("465553115@qq.com"); //注意,这里的发送地址一定要和上面的验证地址一致
		email.setSubject("commons email");
		email.setMsg("a mail sent used by commons email");
		email.addTo("technoboy#yeah.net");
		email.send();
		System.out.println("over");
	}
}


好了,利用commons email我们简单发了一封邮件。(分析过spring的mail后,大家也就明白了commons email)
我们看一下Spring里的mail。Spring提供了一个发送电子邮件的高级抽象层,它向用户屏蔽了底层邮件系统的一些细节,同时代表客户端负责底层的资源处理。发送邮件的主要接口为MailSender以及值对象SimpleMailMessage。
看一下MailSender接口中的方法:
void send(SimpleMailMessage simpleMessage);
void send(SimpleMailMessage[] simpleMessages);

这里方法的参数就是我们提到的值对象。
而SimpleMailMessage的部分定义如下:
public class SimpleMailMessage implements MailMessage, Serializable {

	private String from;

	private String replyTo;

	private String[] to;

	private String[] cc;

	private String[] bcc;

	private Date sentDate;

	private String subject;

	private String text;

那么,我们就明白了SimpleMailMessage的用处了,是用来设置发送者,接受者,主题的一些信息的类。
回过头,我们继续看一下MailSender的层次结构图:


我们可以看到,真正实现mailSender类是JavaMailSenderImpl。这个也就是spring中发送邮件的核心类。看一下它部分的代码:
public class JavaMailSenderImpl implements JavaMailSender {

	/** The default protocol: 'smtp' */
	public static final String DEFAULT_PROTOCOL = "smtp";

	/** The default port: -1 */
	public static final int DEFAULT_PORT = -1;

	private static final String HEADER_MESSAGE_ID = "Message-ID";


	private Properties javaMailProperties = new Properties();

	private Session session;

	private String protocol = DEFAULT_PROTOCOL;

	private String host;

	private int port = DEFAULT_PORT;

	private String username;

	private String password;

	private String defaultEncoding;

	private FileTypeMap defaultFileTypeMap;

从这里,我们可以看出,JavaMailSenderImpl使用的默认协议为smtp,如果需要安全验证,我们需要制定用户名,提供密码。那么,我们使用spring来实现mail的发送:
(需要导入mail.jar 和activation.jar。这里使用的是spring3.0)

1. spring_mail.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	
<bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl">
	<property name="host" value = "smtp.qq.com"></property>
	<property name="protocol" value = "smtp"></property>
	<property name="username" value = "465553115#qq.com"></property>
	<property name="password" value = "your password"></property>
	<property name="port" value = "25"></property>
	<!-- 这里设置验证 -->
	<property name="javaMailProperties">
		<props>
			<prop key="mail.smtp.auth">true</prop>
		</props>
	</property>
	
</bean>	

<bean id ="mailMessage" class = "org.springframework.mail.SimpleMailMessage">
    <!-- from的地址需要和上面username地址一致 -->
	<property name="from" value = "465553115#qq.com"></property>
	<property name="subject" value = "spring mail test"></property>
	<property name="to" value = "technoboy#yeah.net"></property>
	<property name="text" value = "a mail sent used by spring mail"></property>
</bean>

<bean id ="DummySpringMail" class = "org.com.mail.DummySpringMail">
	<property name="mailSender" ref = "mailSender"/>
	<property name="mailMessage" ref = "mailMessage"/>
</bean>

</beans>


2. DummySpringMail类
public class DummySpringMail {
	
	//
	private MailSender mailSender;
	private SimpleMailMessage mailMessage;
	
	public void setMailMessage(SimpleMailMessage mailMessage) {
		this.mailMessage = mailMessage;
	}

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}
	
	//
	public void sendMsg(){
		SimpleMailMessage  smm = new SimpleMailMessage(this.mailMessage);
		this.mailSender.send(smm);
	}
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring_mail.xml");
		DummySpringMail som = (DummySpringMail)context.getBean("DummySpringMail");
		som.sendMsg();
		System.out.println("over");
	}
	
}

  • 大小: 7.1 KB
3
1
分享到:
评论

相关推荐

    springboot 整合spring-boot-starter-mail 发邮件.rar

    springboot 整合spring-boot-starter-mail 发邮件

    spring-boot-starter-mail-2.2.4.RELEASE.jar

    maven配置了阿里云的仓库,不能下载spring-boot-starter-mail的依赖,从官网下载的,希望对有同样问题的小伙伴有用

    spring-boot-mail

    spring.mail.default-encoding=UTF-8 #电子邮件地址 spring.mail.host=smtp.126.com #Application spring.application.name=SEND-MAIL #授权密码 spring.mail.password=password #邮箱服务器默认端口 spring.mail....

    Spring Boot 集成各种框架 使用案例(spring-boot-rabbitmq、spring-boot-mail)

    Spring Boot 集成各种框架 使用案例(spring-boot-rabbitmq、spring-boot-mail、spring-boot-thymeleaf、spring-boot-shiro)

    spring-boot-examples-master.zip

    spring-boot-examples-master示例程序,与各种...spring-boot-mail spring-boot-mongodb spring-boot-mybatis spring-boot-rabbitmq spring-boot-redis spring-boot-shiro spring-boot-swagger spring-boot-web

    spring4.3.0lib

    10\spring-integration-mail-4.3.0.RELEASE.jar 11\spring-jdbc-4.3.0.RELEASE.jar 12\spring-messaging-4.3.0.RELEASE.jar 13\spring-orm-4.3.0.RELEASE.jar 14\spring-oxm-4.3.0.RELEASE.jar 15\spring-retry-...

    spring-framework-3.0.5.RELEASE-dependencies-1

    javax.mail javax.persistence javax.portlet javax.resource javax.servlet javax.transaction javax.validation javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws ...

    spring-boot2.0全新教程实例20例.zip

    - [spring-boot-mail](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mail):Spring Boot 和邮件服务 - [spring-boot-mongodb]...

    spring-boot-mail邮件发送服务 v1.0.zip

    spring-boot-mail邮件发送服务 v1.0.zip

    spring-boot-邮件发送

    Spring Boot通过集成spring-boot-starter-mail依赖和相关配置,可以方便地实现邮件发送功能。具体的步骤: 添加依赖:在Spring Boot项目的pom.xml文件中加入spring-boot-starter-mail依赖,如果需要发送模板邮件,还...

    Spring boot 示例 官方 Demo

    spring-boot-mail:spring boot和邮件服务 spring-boot-mongodb:spring boot和mongodb的使用 spring-boot-multi-mongodb:spring boot和mongodb多数据源的使用 spring-boot-package-war:spring-boot打包成war包...

    spring-framework完整源代码(spring框架源码)

    aop,beans,cache,context,core,dao,ejb,instument,jca,jdbc,jms,jmx,jndi,mail,metadate,mock,orm,remoting,scheduling,scripting,stereotype,test,transcation,ui,util,validation,web 以上数十子模块源码全部为...

    Spring Boot Examples

    spring-boot-mail:spring boot和邮件服务 spring-boot-mongodb:spring boot和mongodb的使用 spring-boot-multi-mongodb:spring boot和mongodb多数据源的使用 spring-boot-package-war:spring-boot打包成war包...

    spring-framework-3.0.5.RELEASE-dependencies-5

    1号包: edu.emory.mathcs.backport edu.oswego.cs.concurrent com.bea.commonj com.caucho com.google.jarjar com.h2database com.ibm.websphere com.jamonapi com.lowagie.text com.mchange.c3p0 ...

    spring-framework-3.0.5.RELEASE-dependencies-4

    1号包: edu.emory.mathcs.backport edu.oswego.cs.concurrent com.bea.commonj com.caucho com.google.jarjar com.h2database com.ibm.websphere com.jamonapi com.lowagie.text com.mchange.c3p0 ...

    spring-framework-3.0.5.RELEASE-dependencies-3

    1号包: edu.emory.mathcs.backport edu.oswego.cs.concurrent com.bea.commonj com.caucho com.google.jarjar com.h2database com.ibm.websphere com.jamonapi com.lowagie.text com.mchange.c3p0 ...

    spring-framework-3.0.5.RELEASE-dependencies-2

    1号包: edu.emory.mathcs.backport edu.oswego.cs.concurrent com.bea.commonj com.caucho com.google.jarjar com.h2database com.ibm.websphere com.jamonapi com.lowagie.text com.mchange.c3p0 ...

    spring-framework-3.0.5.RELEASE-dependencies-8

    1号包: edu.emory.mathcs.backport edu.oswego.cs.concurrent com.bea.commonj com.caucho com.google.jarjar com.h2database com.ibm.websphere com.jamonapi com.lowagie.text com.mchange.c3p0 ...

    spring jar 包详解

    (10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 (11) spring-web.jar 这个jar文件包含Web应用...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    1) spring-core.jar需commons-collections.jar,spring-core.jar是以下其它各个的基本。 2) spring-beans.jar需spring-core.jar,cglib-nodep-2.1_3.jar 3) spring-aop.jar需spring-core.jar,spring-beans.jar,...

Global site tag (gtag.js) - Google Analytics