前言:我目前总结的使用java发送邮件的方式有两种,分别是在spring框架xml配置文件使用bean标签,另一种方法是把发送功能封装成一个对象,废话不多说上代码, 边看代码边讲解,希望对需要的人能有帮助。

一、使用xml配置文件发送邮件

第一步:新建一个xml文件起名为emailSender.xml (名称是随意的,后面的代码和名称对应上就可以)

emailSender.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd"> <!-- ******************************************************************** -->
<!-- Spring 3.0 configuration -->
<!-- ******************************************************************** -->
<mvc:annotation-driven/> <!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
<mvc:default-servlet-handler/> <!-- 配置邮箱发射器 --> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.qq.com</value>
</property>
<property name="port">
<!-- Error using port 465, so use port 587 -->
<value>587</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">80000</prop>
<!--
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
-->
</props>
</property>
<property name="username">
<value>这里填写发送邮箱号</value>
</property>
<property name="password">
<value>这里填写密码(qq邮箱填写授权码)</value>
</property>
</bean>
</beans>

第二步:写java代码(可以发送html邮件哦,只需要修改一处代码就OK啦!!!)

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class SendEmailController { @RequestMapping(value = "/sendEmail", method = RequestMethod.POST)
@ResponseBody
public void sendEmail(){
try {
System.out.println("read to send");
//引入mailSender.xml配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("mailSender.xml");
//从配置文件中创建java邮件发射器
JavaMailSender sender = (JavaMailSender)ctx.getBean("javaMailSender");
//创建发送消息对象
MimeMessage msg = sender.createMimeMessage();
//创建发送消息帮助对象,utf-8编码
MimeMessageHelper helper = new MimeMessageHelper(msg,true,"utf-8");
//设置发送人邮箱
helper.setFrom("xxxxxx@qq.com");
//设置收件人邮箱
helper.setTo("xxxxxx@163.com");
//设置邮箱主题
helper.setSubject("测试邮件");
//设置邮箱内容--纯文本格式
helper.setText("tets this is a spring mvc email");
       //我想发送一封html邮件,把 helper.setText("") 修改成 msg.setContent("邮件内容", "text/html;charset = utf-8");
System.out.println("Is sending...");
//发送邮件
sender.send(msg);
System.out.println("email send ok");
} catch (MessagingException e) {
System.out.println("send fail");
e.printStackTrace();
}
}
}

 二、第二种方法:直接使用java代码配置

package com.ccpit.p4.dispatch.web;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType; public class sendMail {
public static void main(String[] args) throws MessagingException{
// 创建Properties 类用于记录邮箱的一些属性
final 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", "xxxxxx@qq.com");
// 此处的密码就是前面说的16位STMP口令
props.put("mail.password", "xxxxxx"); // 构建授权信息,用于进行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"));
try {
message.setFrom(form);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 设置收件人的邮箱
InternetAddress to = new InternetAddress("xxxxxx@163.com");
message.setRecipient(RecipientType.TO, to); // 设置邮件标题
message.setSubject("测试邮件"); // 设置邮件的内容体
message.setContent("这是一封测试邮件", "text/html;charset=UTF-8"); // 最后当然就是发送邮件啦
Transport.send(message);
}
}

最新文章

  1. Oracle创建用户设置权限
  2. 分布式监控系统Zabbix-3.0.3-完整安装记录(2)-添加mysql监控
  3. JDO持久 (jdbc ejb)
  4. 使用jenkins构建持续集成平台
  5. Android Service和Thread的关系
  6. javascript中的this与prototype,原型理解
  7. 总结PowerShell的常用命令
  8. keil优化论
  9. [刷题]算法竞赛入门经典(第2版) 5-1/UVa1593 - Alignment of Code
  10. Js作用域&amp;作用域链
  11. Day1 Python基础学习
  12. BZOJ 3038: 上帝造题的七分钟2【线段树区间开方问题】
  13. SpringMVC集成rabbitmq:优化秒杀下单环节
  14. react-native中的setNativeProps
  15. [Python 从入门到放弃] 5. 文件与异常(一)
  16. 带你走进EJB--将EJB发布为Webservice(3)
  17. 【linux】——ubuntu12.04 下安装wine和wine乱码解决方案
  18. JQuery fullcalender文档
  19. 使用和学习 ES2015
  20. XAF-如何修改内置的编辑器(Property Editor)

热门文章

  1. 解决Linux使用php命令 -base comment not found并安装composer
  2. JZOJ 5849 d
  3. django开发基础
  4. (转)RubyGems常用命令
  5. visual studio cl -d1reportSingleClassLayout查看内存f分布
  6. P1309 瑞士轮
  7. App 设计技巧
  8. python - 接口自动化测试 - TestLogin - 登录接口测试用例
  9. 【bzoj3944/bzoj4805】Sum/欧拉函数求和 杜教筛
  10. CentOS下Nginx部署React静态应用