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,结果是未授权. 再后来听人说,有一个 ...
随机推荐
- IDEA热部署(一)---解析关键配置。
本编博客转载自:因为自己在研究热部署,包括热部署那些文件,部署实现的包括那些操作.这一块,所以这篇好博客. http://www.mamicode.com/info-detail-1699044.ht ...
- nginx转发tomcat请求转成https后页面不能下载apk文件而是直接打开
访问域名下面的apk文件 https://xxxx/xxx.apk 浏览器没有下载而是直接打开了文件 没有找到问题原因,可能是https的原因,要是用http就可以下载,转发https就有问题 后来是 ...
- Cenos 6.5上的subverion的yum配置笔记
Subversion在CenOS 6.5上的安装配置 1.安装 yum install subversion 2.配置 #创建目录 mkdir /opt/svn #创建版本库 svna ...
- iKcamp团队制作|基于Koa2搭建Node.js实战项目教学(含视频)☞ 环境准备
安装搭建项目的开发环境 视频地址:https://www.cctalk.com/v/15114357764004 文章 Koa 起手 - 环境准备 由于 koa2 已经开始使用 async/await ...
- IOS学习3——代理
本文转载自:你真的了解iOS代理设计模式吗? 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递一些参数.这篇文章会涵盖代理的使用技巧和原理,以及代理的内存 ...
- iOS 页面之间的专长动画控制器间的转换
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 24.0px; font: 14.0px "Heiti SC Light" ...
- 创建access数据库表demo的封装
1.创建类 public void CreateBiao(ADOX.Catalog catlog,ADOX.Table table) { //StuId Column(AutoIncrement ) ...
- Wincc flexable的局势视图的组态
1.趋势视图介绍 2.实时趋势视图的组态 1)创建连接和变量 2)开始组态局势视图 3)设置趋势视图的属性,添加一个趋势 3.模拟运行HMI,观察局势图
- python爬取大众点评
拖了好久的代码 1.首先进入页面确定自己要抓取的数据(我们要抓取的是左侧分类栏-----包括美食.火锅)先爬取第一级分类(美食.婚纱摄影.电影),之后根据第一级链接爬取第二层(火锅).要注意第二级的p ...
- css div 细边框
.item{ max-width:48%; float:left; padding:2px; border-top:1px solid #000; border-left:1px solid #000 ...