JavaMail 邮件发送服务-SSL加密满足云需求

JavaMail 邮件发送服务-SSL加密满足云需求

    • 1邮箱配置
      • 1.1注册邮箱
      • 1.2 设置:POP3/SMTP/IMAP
    • 2 应用开发
      • 2.1 pom.xml配置
      • 2.2 properties 配置
      • 2.3 邮箱信证管理
      • 2.4 邮箱信证管理
      • 2.5 邮件发送样例代码
    • 3 邮件调用样例
      • 3.1 调用URL样例
      • 3.2 发送结果展示
    • 参考

1邮箱配置

测试选取的126邮箱

1.1注册邮箱

自行注册

1.2 设置:POP3/SMTP/IMAP

JavaMail 邮件发送服务-SSL加密满足云需求_第1张图片
点击开启(我已经开启,需要手机扫码,发送邮件)
JavaMail 邮件发送服务-SSL加密满足云需求_第2张图片
注意:密码只显示一次,需要记录下来,如果忘记,可以在下面再增加一个

2 应用开发

2.1 pom.xml配置

    
		org.springframework.boot
		spring-boot-starter-mail
	

2.2 properties 配置

spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.126.com
spring.mail.password=CMXPAAXNSSHWWXOJS # 注意这里的密码是授权密码,不是Email的登陆密码
spring.mail.port=465  # 此为加密的端口,不是默认端口25
spring.mail.protocol=smtp
#spring.mail.properties.smtp.auth=true
spring.mail.properties.smtp.timeout=25000
[email protected]  #邮箱地址

spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.imap.ssl.socketFactory.fallback=false
spring.mail.properties.mail.smtp.ssl.socketFactory.class=com.yonyou.dmscloud.email.util.MailSocketFactory # SSL加密工厂类

2.3 邮箱信证管理

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager;
/**
 *  邮箱信证管理
 * @author billLiu
 * @date 2020-04-13
 */
public class MailTrustManager implements X509TrustManager {

	@Override
	public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

	}

	@Override
	public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

	}

	@Override
	public X509Certificate[] getAcceptedIssuers() {
		return new X509Certificate[0];
	}

}

2.4 邮箱信证管理

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

/**
 * 邮件ssl
 * @author billLiu
 * @date 2020-04-13
 */
public class MailSocketFactory extends SSLSocketFactory {
	private SSLSocketFactory factory;

	public MailSocketFactory() {
		try {
			SSLContext sslcontext = SSLContext.getInstance("TLS");
			sslcontext.init(null, new TrustManager[] { new MailTrustManager() }, null);
			factory = sslcontext.getSocketFactory();
		} catch (Exception ex) {
			// ignore
		}
	}

	public static SocketFactory getDefault() {
		return new MailSocketFactory();
	}

	@Override
	public Socket createSocket() throws IOException {
		return factory.createSocket();
	}

	@Override
	public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException {
		return factory.createSocket(socket, s, i, flag);
	}

	@Override
	public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException {
		return factory.createSocket(inaddr, i, inaddr1, j);
	}

	@Override
	public Socket createSocket(InetAddress inaddr, int i) throws IOException {
		return factory.createSocket(inaddr, i);
	}

	@Override
	public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
		return factory.createSocket(s, i, inaddr, j);
	}

	@Override
	public Socket createSocket(String s, int i) throws IOException {
		return factory.createSocket(s, i);
	}

	@Override
	public String[] getDefaultCipherSuites() {
		return factory.getDefaultCipherSuites();
	}

	@Override
	public String[] getSupportedCipherSuites() {
		return factory.getSupportedCipherSuites();
	}
}

2.5 邮件发送样例代码

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 邮件发送
 * @author billLiu
 *
 */
@Controller
@RequestMapping("/email")
public class EmailSendController {
	
	@Value("${spring.mail.username}")
	private String fromEmail;

    @Autowired
	JavaMailSender jms;
    /**
     * 发送邮件样例
     * @param toEmail
     * @param subject
     * @param html
     */
    @RequestMapping(value = "/send", method = RequestMethod.GET)
    @ResponseBody
    public void testSendEmail(@RequestParam String toEmail,@RequestParam String subject,
    		@RequestParam String html) {
    	//建立邮件消息
		SimpleMailMessage mainMessage = new SimpleMailMessage();
		//发送者
		mainMessage.setFrom(fromEmail);
		//接收者
		mainMessage.setTo(toEmail);
		//发送的标题
		mainMessage.setSubject(subject);
		//发送的内容
		mainMessage.setText(html);
		jms.send(mainMessage);
    }
}

3 邮件调用样例

3.1 调用URL样例

http://localhost:port/自己的服务上下文/email/[email protected]&subject=测试&html=这是一个测试样例2234

3.2 发送结果展示

JavaMail 邮件发送服务-SSL加密满足云需求_第3张图片

参考

https://blog.csdn.net/u013600907/article/details/84713032

你可能感兴趣的:(技术学习,javamail,ssl,云端)