您的当前位置:首页正文

Maven项目实现简单发送邮件

2024-11-23 来源:个人技术集锦

Maven项目java后端实现发送邮件

需求

实现邮箱验证找回密码功能,本文介绍后端java实现

实现

pom.xml引入依赖包

<!-- 发送邮件 -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

工具类SendMailUtil

package com.xxx.common.utils;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.util.MailSSLSocketFactory;

public class SendMailUtil {
	    
	    //邮件服务器主机名
	    // QQ邮箱的 SMTP 服务器地址为: smtp.qq.com
	    private static String myEmailSMTPHost = "smtp.qq.com";
	    
	    //发件人邮箱
	    private static String myEmailAccount = "xxxxxxxxxxx@qq.com";
	    
	    //发件人邮箱密码(授权码)
	    //在开启SMTP服务时会获取到一个授权码,把授权码填在这里
	    private static String myEmailPassword = "xxxxxxxxxxxxxxxx";
	    
	    /**
	     * 邮件单发(自由编辑短信,并发送,适用于私信)
	     *
	     * @param toEmailAddress 收件箱地址
	     * @param emailTitle 邮件主题
	     * @param emailContent 邮件内容
	     * @throws Exception
	     */
	    public static void sendEmail(String toEmailAddress, String emailTitle, String emailContent) throws Exception{
	             
	        Properties props = new Properties();
	         
	        // 开启debug调试
	        props.setProperty("mail.debug", "true");
	                 
	        // 发送服务器需要身份验证
	        props.setProperty("mail.smtp.auth", "true");
	         
	        // 端口号
	        props.put("mail.smtp.port", 465);
	         
	        // 设置邮件服务器主机名
	        props.setProperty("mail.smtp.host", myEmailSMTPHost);
	         
	        // 发送邮件协议名称
	        props.setProperty("mail.transport.protocol", "smtp");
	         
	        /**SSL认证,注意腾讯邮箱是基于SSL加密的,所以需要开启才可以使用**/
	        MailSSLSocketFactory sf = new MailSSLSocketFactory();
	        sf.setTrustAllHosts(true);
	         
	        //设置是否使用ssl安全连接(一般都使用)
	        props.put("mail.smtp.ssl.enable", "true");
	        props.put("mail.smtp.ssl.socketFactory", sf);
	         
	        //创建会话
	        Session session = Session.getInstance(props);
	         
	        //获取邮件对象
	        //发送的消息,基于观察者模式进行设计的
	        Message msg = new MimeMessage(session);
	         
	        //设置邮件标题
	        msg.setSubject(emailTitle);
	         
	        //设置邮件内容
	        //使用StringBuilder,因为StringBuilder加载速度会比String快,而且线程安全性也不错
	        StringBuilder builder = new StringBuilder();
	         
	        //写入内容
	        builder.append("\n" + emailContent);
	         
	        //设置显示的发件时间
	        msg.setSentDate(new Date());
	         
	        //设置邮件内容
	        msg.setText(builder.toString());
	         
	        //设置发件人邮箱
	        // InternetAddress 的三个参数分别为: 发件人邮箱, 显示的昵称(只用于显示, 没有特别的要求), 昵称的字符集编码
	        msg.setFrom(new InternetAddress(myEmailAccount,"我的工作站", "UTF-8"));
	         
	        //得到邮差对象
	        Transport transport = session.getTransport();
	         
	        //连接自己的邮箱账户
	        //密码不是自己QQ邮箱的密码,而是在开启SMTP服务时所获取到的授权码
	        //connect(host, user, password)
	        transport.connect( myEmailSMTPHost, myEmailAccount, myEmailPassword);
	         
	        //发送邮件
	        transport.sendMessage(msg, new Address[] { new InternetAddress(toEmailAddress) });
	         
	        //将该邮件保存到本地
	        OutputStream out = new FileOutputStream("MyEmail.eml");
	        msg.writeTo(out);
	        out.flush();
	        out.close();
	 
	        transport.close();
	    }

}

调用

/*
 * 向邮箱发送验证码
 * */
@Override
public Map<String, Object> sendCodeToMail(DocRegisterCustom drUserCustom){
	Map<String, Object> responseData = new HashMap<String, Object>();
	if(drUserMapper.getDrUserId(drUserCustom) != null){		//判断用户是否存在
		//根据name获取保存的邮箱
		String mailAddress = drUserMapper.getMailByName(drUserCustom);
		if(mailAddress.equals(drUserCustom.getDoc_mail())){		//用户输入邮箱与绑定邮箱一致→发送验证码
	        try{
	            //生成验证码
	            String verifyCode = RandomUtil.getCode();
	            //邮件主题
	            String emailTitle = "邮箱验证";
	            //邮件内容
	            String emailContent = "您正在进行邮箱验证,您的验证码为:" + verifyCode + ",请于5分钟内完成验证!";
	            //发送邮件
	            SendMailUtil.sendEmail(mailAddress, emailTitle, emailContent);
	    		//缓存5分钟
	    	    redisTemplate.opsForValue().set(mailAddress,verifyCode,5,TimeUnit.MINUTES);
	            responseData = ResponseData.success("邮箱验证码发送成功");
	        }catch(Exception e){
	            responseData = ResponseData.error(e.getMessage());
	        }
		}else{
			//邮箱地址错误
			responseData=ResponseData.result(ResponseState.REP_PASSWORD_MAIL_ERROR.state, ResponseState.REP_PASSWORD_MAIL_ERROR.message, "");
		}
	}else{
		//用户不存在
		responseData=ResponseData.result(ResponseState.REP_USER_EXIST_ERROR.state, ResponseState.REP_USER_EXIST_ERROR.message, "");
	}
	return responseData;
}

RandomUtil类

public class RandomUtil {
	
	/**
	 * 生成6位验证码
	 * @return
	 */
	public synchronized static String getCode() {
		StringBuffer code=new StringBuffer();
		int num;
		for (int i=0;i<6;i++){
			num=(int)(Math.random()*10);
			code.append(String.valueOf(num));
		}
		return code.toString();
	}
}

其他


显示全文