前言:我目前总结的使用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);
}
}

Java发送邮件--web.xml配置,Java代码配置的更多相关文章

  1. WebApplicationInitializer究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用

    本文转自http://hitmit1314.iteye.com/blog/1315816 大家应该都已经知道Spring 3.1对无web.xml式基于代码配置的servlet3.0应用.通过spri ...

  2. Java 之 web.xml(Java之负基础实战)

    多个web.xml会同时生效,先加载tomcat下的web.xml,再加载网站目录下的web.xml. 1.tomcat自带的web.xml 在tomcat的conf目录下 2.自定义web.xml ...

  3. Java/javaEE/web/jsp/网站编程环境配置及其软件下载和网站路径

    Java/javaEE/web/jsp/网站编程环境配置及其软件下载和网站路径 (2015/07/08更新) JDK下载地址(JDK官网下载地址) 下载地址为:http://www.oracle.co ...

  4. web.xml servlet、servlet-mapping配置

    Servlet常称为服务器端小程序,即运行在服务器端的程序,用于处理及响应客户的请求. Servlet类是个特殊的java类,继承于HttpServlet. --------------------- ...

  5. web.xml中servlet-mapping的配置

    <servlet-mapping>元素在Servlet和URL样式之间定义一个映射.它包含了两个子元素<servlet- name>和<url-pattern>,& ...

  6. Java - web.xml文件中可以配置哪些内容?

    web.xml用于配置Web应用的相关信息,如:监听器(listener).过滤器(filter).Servlet.相关参数.会话超时时间.安全验证方式.错误页面等,下面是一些开发中常见的配置: ①配 ...

  7. java的web.xml中<url-pattern>配置[转]

    <servlet-mapping> <servlet-name>downLoadFile</servlet-name> <url-pattern>*.l ...

  8. Servlet容器Tomcat中web.xml中url-pattern的配置详解[附带源码分析]

    目录 前言 现象 源码分析 实战例子 总结 参考资料 前言 今天研究了一下tomcat上web.xml配置文件中url-pattern的问题. 这个问题其实毕业前就困扰着我,当时忙于找工作. 找到工作 ...

  9. web.xml的一份配置(备忘)

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java ...

随机推荐

  1. 25.VUE学习之-单击和双击事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 21.VUE学习之-操作data里的数组变异方法push&unshit&pop&shift的实例应用讲解

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 使用python3下载网易云音乐歌单歌曲,附源代码

    """ 用selenium+PhantomJS配合,不需要进行逆向工程 python 3下的selenium不能默认安装,需要指定版本2.48.0 "" ...

  4. The Moving Points - HDU - 4717 (模拟退火)

    题意 二维空间中有\(n\)个运动的点,每个点有一个初始坐标和速度向量.求出一个时间\(T\),使得此时任意两点之间的最大距离最小.输出\(T\)和最大距离. 题解 模拟退火. 这个题告诉了我,初始步 ...

  5. Centos7和Centos6防火墙开放端口配置方法(避坑教学)

    ▲这篇文章主要为大家详细介绍了Centos7防火墙开放端口的快速方法,感兴趣的小伙伴们可以参考一下! 一.CentOS 7快速开放端口: CentOS升级到7之后,发现无法使用iptables控制Li ...

  6. HDU 4005 The war 双连通分量 缩点

    题意: 有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值. 求敌人在最优的情况下,使图不连通的最小费用. 分析: 首先求出边双连通分量 ...

  7. windows服务安装卸载

    到C盘下找到对应的开发VS的installutil.exe文件,复制到程序的执行文件(*.exe)相同目录下在开始程序中找到VS命令提示工具 转到程序的执行文件(*.exe)目录下 C:\>cd ...

  8. leetcode 【 Linked List Cycle II 】 python 实现

    公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来. 题目: Given a linked list, return the node where the cycle begins. If the ...

  9. 使用 Item,ItemManager 在 XNA 中创建物品和道具(十六)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  10. Java开发微信公众号(四)---微信服务器post消息体的接收及消息的处理

    在前几节文章中我们讲述了微信公众号环境的搭建.如何接入微信公众平台.以及微信服务器请求消息,响应消息,事件消息以及工具处理类的封装:接下来我们重点说一下-微信服务器post消息体的接收及消息的处理,这 ...