作者微信 bishe2022

代码功能演示视频在页面下方,请先观看;如需定制开发,联系页面右侧客服

1、界面访问路径

http://localhost/mail/

使用的为tomcat的80端口

2、界面样式

    blob.png

3、文件和接收人多个时,使用逗号分隔


4、实现方式,前台使用bootstrap、jquery、ajax;后台使用spring、spring mvc


5、工程整体结构

blob.png


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


blob.png


7、本工程使用的是eclipse4.5 tomcat8.0 jdk1.8 如需下载请到公网下载


8、下载mail.rar 后将文件解压并将解压的文件导入到eclipse中


9、修改上图6中的配置文件


10、输入内容,成功发送后提示,发送成功

blob.png


否则提示发送失败

blob.png



11、如何申请一个163邮箱

blob.png



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


blob.png


点击设置先开通pop3

blob.png

blob.png


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

blob.png


点击重置,修改成自己设置的授权码(注授权码只显示一次,所以在设置时,一定要自己记住授权码)


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);
	}*/
}




Home