javax.mail和spring mail的区别

如题所述

javax.mail:

package com.healist.email;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
* Created by Healist on 2017/1/29.
*/
public class Email {
public static void main(String[] args) throws MessagingException {
// 创建Properties 类用于记录邮箱的一些属性
Properties props = new Properties();
// 表示SMTP发送邮件,必须进行身份验证
props.put("mail.smtp.auth", "true");
//此处填写SMTP服务器
props.put("mail.smtp.host", "smtp.qq.com");
//端口号,QQ邮箱给出了两个端口,但是另一个不管用,这个587在这是管用的
props.put("mail.smtp.port", "587");
// 此处填写你的账号
props.put("mail.user", "[email protected]");
// QQ邮箱的授权码,去邮箱自己获取
props.put("mail.password", "dffajaehrb");

// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);

// 设置收件人的邮箱
InternetAddress to = new InternetAddress("[email protected]");
message.setRecipient(MimeMessage.RecipientType.TO, to);

// 设置邮件标题
message.setSubject("测试邮件");

// 设置邮件的内容体
message.setContent("这是一封测试邮件", "text/html;charset=UTF-8");

// 发送邮件
Transport.send(message);
}
}

Spring Mail:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

   <context:component-scan base-package="com.healist.*">
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
       <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
   </context:component-scan>

   <!-- 引入配置文件 -->
   <bean id="propertyConfigurer"
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations">
           <list>
               <value>classpath:dev.properties</value>
               <!--如果有多个配置文件,在这里继续添加 -->
           </list>
       </property>
   </bean>

   <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
       <property name="host" value="${mail.smtp.host}" />
       <property name="port" value="${mail.smtp.port}" />
       <property name="username" value="${mail.smtp.username}" />
       <property name="password" value="${mail.smtp.password}" />
       <property name="javaMailProperties">
           <props>
               <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
               <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
               <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
               <prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop>
               <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
               <prop key="mail.smtp.socketFactory.fallback">false</prop>
           </props>
       </property>
   </bean>

   <tx:annotation-driven />

</beans>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-09-30
.发送简单的文本邮件

[java] view plain copy
package net.xftzr.mail;

import java.util.Properties;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

/**
* 本类测试简单邮件 直接用邮件发送
*
* @author Administrator
*本回答被提问者采纳
相似回答