spring 实现邮箱发送
1、界面访问路径
2、界面样式

3、文件和接收人多个时,使用逗号分隔
4、实现方式,前台使用bootstrap、jquery、ajax;后台使用spring、spring mvc
5、工程整体结构

6、修改文件为自己的邮箱及授权码

7、本工程使用的是eclipse4.5 tomcat8.0 jdk1.8 如需下载请到公网下载
8、下载mail.rar 后将文件解压并将解压的文件导入到eclipse中
9、修改上图6中的配置文件
10、输入内容,成功发送后提示,发送成功

否则提示发送失败

11、如何申请一个163邮箱

点击注册邮箱,注册成功后需要修改授权码

点击设置先开通pop3


开通成功后点击客户端授权密码

点击重置,修改成自己设置的授权码(注授权码只显示一次,所以在设置时,一定要自己记住授权码)
12、代码实例
页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title>邮件发送</title>
<!-- 引入公共文件 -->
<link rel="stylesheet" href="bootstrap-3.3.0-dist/dist/css/bootstrap.min.css">
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript" src="bootstrap-3.3.0-dist/dist/js/bootstrap.min.js"></script>
<style type="text/css">
.form-horizontal .form-group {
margin-right: 0px !important;
margin-left: 0px !important;
}
</style>
<script>
$(function(){
$('#send').click(function(){
var people = $('#people').val();
var attachment = $('#attachment').val();
$.ajax({
type: "POST",
url: "http://localhost/mail/message/send/sendMail.action",
data: {
title:$('#title').val(),
content:$('#content').val(),
people:people.substring(0,people.length),
attachment:attachment.substring(0,attachment.length),
},
success: function(data){
if(data.status == 1){
alert(data.msg);
}else{
alert(data.msg);
}
},
error:function(){
alert("发送失败");
}
});
});
})
</script>
</head>
<body>
<div>
<form style="margin-top: 20px;">
<div>
<label for="inputEmail3" class="col-sm-2 control-label">标题</label>
<div>
<input type="text" id="title" placeholder="标题">
</div>
</div>
<div>
<label for="inputPassword3" class="col-sm-2 control-label">内容</label>
<div>
<textarea rows="3" id="content" placeholder="内容"></textarea>
</div>
</div>
<div>
<label for="inputPassword3" class="col-sm-2 control-label">附件</label>
<div>
<input type="text" id="attachment" placeholder="文件真实路径">
</div>
</div>
<div>
<label for="inputPassword3" class="col-sm-2 control-label">接收人</label>
<div>
<textarea rows="3" id="people" placeholder="123456789@qq.com"></textarea>
</div>
</div>
<div>
<div class="col-sm-offset-2 col-sm-10">
<button type="button" id="send" class="btn btn-default">发送</button>
</div>
</div>
</form>
</div><!-- pageContainer -->
</body>
</html>
后台代码controller
package com.messages.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
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;
import com.messages.ResponseData;
import com.messages.service.messageService;
/**
* 登录控制器
* @author xuchangcheng
*/
@Controller
@RequestMapping("/send")
public class messageController {
@Autowired
private messageService message;
/**
* 发送邮件
* @param request
* @return String
*/
@RequestMapping(value="sendMail", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public ResponseData save(HttpServletRequest request,String title,String content,String people,String attachment){
ResponseData responseData=new ResponseData();
try{
message.send(title,content, people,attachment);
responseData.setStatus(1);
responseData.setMsg("发送成功");
responseData.setData(1);
} catch (Exception e) {
e.printStackTrace();
responseData.setStatus(2);
responseData.setMsg("发送失败");
}
return responseData;
}
}
service端
package com.messages.service;
import org.springframework.stereotype.Service;
@Service
public class messageService{
/**
* 发送邮件
* @param
* @return
* @throws Exception
*/
public void send(String title,String content,String people,String attachment) throws Exception{
SpingMailSend sm = new SpingMailSend();
String [] pe = people.split(",");
if(attachment==""){
sm.send(title, content,pe);
}else{
String [] at = attachment.split(",");
sm.send(title, content,at, pe);
}
}
}
短息发送的核心代码
package com.messages.service;
import java.io.File;
import java.io.UnsupportedEncodingException;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.messages.WebContextListener;
public class SpingMailSend {
/************************************邮件核心***************************************/
/**
* 邮件发送 方法可设置发送,抄送,密送
*
* @param subject 邮件的标题
* @param content 邮件正文
* @param attachmenFiles[] 附件,需要真实位置
* @param receiver 接收人
* @param carbonCopyTo[] 抄送人邮箱地址(为数组)
* @param blindCarbonCopyTo[] 密送人邮箱地址(为数组)
* @return boolean 邮件是否发成功
* @throws MessagingException
*/
public boolean send(String subject,String content,String[] receiver) throws Exception {
boolean status = this.send(subject, content, null,receiver, null, null);
return status;
}
public boolean send(String subject,String content,String[] attachmenFiles,String[] receiver) throws Exception {
boolean status = this.send(subject, content, attachmenFiles,receiver, null, null);
return status;
}
public boolean send(String subject,String content,String[] attachmenFiles,String[] receiver,String[] carbonCopyTo,String[] blindCarbonCopyTo) throws Exception {
try {
/*ServletContext sc = request.getSession().getServletContext();
ApplicationContext ac2 = WebApplicationContextUtils .getWebApplicationContext(sc);
JavaMailSender mailSender = (JavaMailSender) ac2.getBean("mailSender");
SimpleMailMessage mailMessage = (SimpleMailMessage) ac2.getBean("mailMessage");*/
JavaMailSender mailSender = (JavaMailSender) WebContextListener.getBean("mailSender");
SimpleMailMessage mailMessage = (SimpleMailMessage) WebContextListener.getBean("mailMessage");
String from=mailMessage.getFrom();
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mime, true, "utf-8");
helper.setSubject(subject);// 邮件主题
helper.setText(content, true);// true表示设定html格式
for(int i=0;attachmenFiles!=null&&i<attachmenFiles.length;i++){
File file=new File(attachmenFiles[i]);
helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
}
helper.setFrom(from);// 发件人
helper.setTo(receiver);// 收件人
if(carbonCopyTo!=null)
helper.setCc(carbonCopyTo);
if(blindCarbonCopyTo!=null)
helper.setBcc(blindCarbonCopyTo);
mailSender.send(mime);
return true;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
/* 非web端测试发送(java)
public boolean send(String subject,String content,String[] attachmenFiles,String[] receiver,String[] carbonCopyTo,String[] blindCarbonCopyTo) throws Exception {
try {
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:spring-bean/spring-context-mail.xml");
JavaMailSender mailSender = (JavaMailSender) ac.getBean("mailSender");
SimpleMailMessage mailMessage = (SimpleMailMessage) ac.getBean("mailMessage");
String from=mailMessage.getFrom();
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mime, true, "utf-8");
helper.setSubject(subject);// 邮件主题
helper.setText(content, true);// true表示设定html格式
for(int i=0;attachmenFiles!=null&&i<attachmenFiles.length;i++){
File file=new File(attachmenFiles[i]);
helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
}
helper.setFrom(from);// 发件人
helper.setTo(receiver);// 收件人
if(carbonCopyTo!=null)
helper.setCc(carbonCopyTo);
if(blindCarbonCopyTo!=null)
helper.setBcc(blindCarbonCopyTo);
mailSender.send(mime);
return true;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}*/
/*public static void main(String[] args) throws Exception {
SpingMailSend s=new SpingMailSend();
String [] pe = new String[]{"123456789@qq.com"};
s.send("163测试", "163测试邮箱发送", pe);
}*/
}
demo下载:http://www.wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=703
spring 实现邮箱发送的更多相关文章
- Spring Boot 整合Spring Data以及rabbitmq,thymeleaf,向qq邮箱发送信息
首先得将自己的qq开启qq邮箱的POP3/SMTP服务 说明: p,e为路由key. 用户系统完成登录的时候,将{手机号-时间-IP}保存到队列Phone-queue中,msg-sys系统获得消息打印 ...
- JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)
JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架) 本来想实现 QQ 登录,有域名一直没用过,还得备案,好麻烦,只能过几天再更新啦. 先把实现的发送邮箱验证码更能更新了. 老规矩,更多内容在注释 ...
- SpringBoot中快速实现邮箱发送
前言 在许多企业级项目中,需要用到邮件发送的功能,如: 注册用户时需要邮箱发送验证 用户生日时发送邮件通知祝贺 发送邮件给用户等 创建工程导入依赖 <!-- 邮箱发送依赖 --> < ...
- Spring Boot邮箱链接注册验证
Spring Boot邮箱链接注册验证 简单介绍 注册流程 [1]前端提交注册信息 [2]后端接受数据 [3]后端生成一个UUID做为token,将token作为redis的key值,用户数据作为re ...
- Linux配置邮箱发送(MUTT/MSMTPQ)
配置邮箱发送 http://www.ilanni.com/?p=10589
- java邮件发送 qq与163邮箱互发和qq和163邮箱发送其他邮箱实例
研究了近一天的时间,通过查阅相关资料,终于对java发送邮件的机制,原理有了一点点的理解,希望能够帮到大家! 1.首先要向你的项目里导入1个jar包:mail-1.4.4.jar即可(实现qq和163 ...
- 通过邮箱发送html报表
前言 需求是发送邮件时, 可以将报表正文贴到邮件里, 可以正常复制选中报表内容. 目前的做法是简单粗暴的转成了一张图片, 这样效果显然是很糟糕的. 今天看到邮箱里可以预览Word, Excel, F1 ...
- java邮箱发送
一.为何要使用邮箱发送 相信大家在日常工作生活中少不了和邮件打交道,比如我们会用邮件进行信息交流,向上级汇报日常工作:邮件发送的原理是什么?邮件是如何发送的呢?本系列教程将会讲解邮件如何申请可用jav ...
- qq邮箱发送,mail from address must be same as authorization user
由于邮箱发送的邮箱账号更换,所以重新测试.结果一直出错,要不就是请求超时,要不就是未授权. 用smtp 开始的时候,端口使用495,结果是请求超时. 后来改成25,结果是未授权. 再后来听人说,有一个 ...
随机推荐
- redis的事务(简单介绍)
1.简单描述 redis对事务的支持目前还是比较简单.redis只能保证一个client发起的事务中的命令是可以连续的执行,而中间不会插入其他client的命令.由于redis是但现场来处理所有cli ...
- 在Eclipse中如何关联源代码
我们就以Struts2框架为例,展示在Eclipse中如何关联源代码.例如,在Struts2框架的学习中,我们有时需要查看ActionSupport这个类的源码,这个时候就要在Eclipse中关联源代 ...
- 「mysql优化专题」主从复制面试宝典!面试官都没你懂得多!(11)
内容较多,可先收藏,目录如下: 一.什么是主从复制 二.主从复制的作用(重点) 三.主从复制的原理(重中之重) 四.三步轻松构建主从 五.必问面试题干货分析(最最重要的点) 一.什么是主从复制(技术文 ...
- Eclipse 插件安装、升级和卸载的方法
Eclipse 的插件可以装在内部,也可以装在外部,装在内部的方法很简单:把插件的features和plugins目录copy到eclipse的安装目录即可. eclipse和其插件升级比较频繁,用过 ...
- atom添加eslint插件
在atom编辑器里添加插件,操作步骤如下:以atom-ide-vue插件为例 //切换到插件目录cd /Users/name/.atom/packages //将需要下载插件的源代码拉下来git cl ...
- Javascript中的Microtask和Macrotask——从一道很少有人能答对的题目说起
首先我们来看一道题目,如下javascript代码,执行后会在控制台打印出什么内容? async function async1() { console.log('async1 start'); aw ...
- JMeter获取CSV文件行数
import java.io.BufferedReader; import java.io.FileReader; BufferedReader br=new BufferedReader(new F ...
- 【java】HashMap、Map、Set、HashMap.put()、HashMap.keySet()、HashMap.entrySet()、Map.Entry内部类
package com.tn.hashMap; public class Student { private String id; private String name; public Studen ...
- JavaWeb 例子 JDBC+JSP登陆注册留言板
注册页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- 如何高效撤销Git管理的文件在各种状态下的更改
一.背景 企业中我们一般采用分布式版本管理工具git来进行版本管理,在团队协作的过程中,我们难免会遇到误操作,需要撤销更改的情况,那么我们怎么高效的进行撤销修改呢?对于还未提交到暂存区的代码怎么高效撤 ...