基于网易163邮箱服务器的发送邮件
相关依赖项

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>

Controller层代码

package com.nec.controller;
import com.nec.bean.Email;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import static javax.mail.internet.InternetAddress.*;

@Controller
public class EmailController {


@RequestMapping(value = "/emailsend")
public String emailsend(@RequestParam("file") MultipartFile file,String em
, HttpServletResponse response, HttpServletRequest request, Email email){
//发件箱账户
String fromAccount = "ujiuye_1@163.com";
//发件箱登录密码
String frompasswprd = "asdf123456";
//收件箱
String revAccount = em;
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol","smtp");
properties.setProperty("mail.smtp.host","smtp.163.com");
properties.setProperty("mail.smtp.auth","true");
Authenticator authenticator = new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("ujiuye_1","asdf111111");
}
};
//javaMail提供链接发送邮件的服务器
//properties配置链接服务器信息
//authenticator登录服务器的授权码
Session session = Session.getInstance(properties, authenticator);
session.setDebug(true);
//邮件消息内容
MimeMessage mimeMessage = new MimeMessage(session);
try {
//发件人
mimeMessage.setFrom(new InternetAddress(fromAccount));
//抄送给发件人
mimeMessage.addRecipients(Message.RecipientType.CC,InternetAddress.parse(fromAccount));
//收件人
mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(revAccount));
//消息头
mimeMessage.setSubject(email.getTitle(),"utf8");
//邮箱内容
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setText(email.getContent());

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart);
//添加附件
if(file.getSize()>0){
String path = request.getServletContext().getRealPath("upload");
String filename = file.getOriginalFilename();
String filePath = path+"/"+filename;
File file1 = new File(filePath);
try {
file.transferTo(file1);
//添加到multipart中
DataSource dataSource = new FileDataSource(file1);
bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(dataSource));
bodyPart.setFileName(MimeUtility.encodeText(filename));
multipart.addBodyPart(bodyPart);
} catch (IOException e) {
e.printStackTrace();
}
}
//发送邮件的内容
mimeMessage.setContent(multipart);
//发送时间
mimeMessage.setSentDate(new Date());
//保存设置
mimeMessage.saveChanges();
//邮件发送
Transport transport = session.getTransport();
//建立链接
transport.connect(fromAccount,frompasswprd);
//设置发送内容
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
return "email";
}
}

这个发送邮件的方法,可以添加附件,向后台的传过来的值有个em,是收件人的邮箱地址,通过页面的一个隐藏域来传过来
前端代码

<tr >
<td align="right" bgcolor="#FAFAF1" height="22">收件人:</td>
<td align='left' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='#FCFDEE';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<select name="ename" id="ename">
<option value="-1">-选取收件人-</option>
</select>
</td>
</tr>

//然后是script中的代码
//因为想让下拉选框比较好看,拼接了一下,导致后面还得切掉
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(function () {
$.post("${pageContext.request.contextPath}/selectEmployeeEmail",function (obj) {
$(obj).each(function (index,ele) {
$("#ename").append('<option value="'+ele.eid+'">'+ele.ename+'---'+ele.archives.email+'</option>')
})
},"json")
$("#ename").change(function () {
var value = $("#ename")[0].selectedOptions["0"].innerText
var value = value.split("---")
$("#email").val(value[1]);
})

})

</script>