春のブート練習(C)を達成するために、メッセージを送信

プロジェクトのためのメールサービスは、基本サービスの一部であり、春にメールの使用は、実装が本当にあまりにもシンプルですが、それでもダウンに注意することは少し問題を抱えている、メールサービスを合わせたパッケージスプリング・ブートの下に、次の用語。

実際には、以下のフォーム上の基本的なサービスパッケージ以外の何物でもツールの基礎利点:シンプルで実用的には、ショートカットの欠点を送っていない:高カップリングの適用は、分散システムへの現在のアプローチは、これは致命的です

ダボのサービスにはダボの例になることですので、私たちの会社は、ダボを使用しているため。RPC、HTTPおよび通信モードの他のタイプを表す以外の何物でもありません。長所:大規模な分散プロジェクトでは、APIを依存する必要があり、やや複雑:デカップリングの応用、展開は、既存のアプリケーションを動的にデメリット可能影響はありません

非同期通信MQ

長所:MQ通信コミュニケーションのための、簡単で高速かつ欠点を達成するための私のお気に入りのデカップリングの方法です。少し複雑達成するためには、信頼性の監視と処理能力のメッセージやその他の問題に注意を払います

以上の三つの方法のために私はMQにより傾いていますので、単純に次のMQメール処理下で包装します

図1に示すように、バネブートスタータAMQPばねブートスターターメールを依存関係

2、最初にあなたは、もはや物語ではありませんMQを設定する必要がありMQの構成は、以前の記事を見てすることができます見てみたいです

3は、私が直接その受信者の使用下で、

package com.ecej.nove.sms.mail;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.ecej.nove.base.mail.BaseMail;

/**
 * 接收邮件信息
 * 
 * @author QIANG
 *
 */
@Component
@RabbitListener(queues = "ecejmail")
public class MailReceiverService {

	private Logger LOG = LoggerFactory.getLogger(MailReceiverService.class);

	@Resource
	private MailSendService mailSendService;

	@Value("${mail.close.flag}")
	private boolean flag;

	@RabbitHandler
	public void ReceiverMessage(BaseMail mail) {
              //开关
		if (flag)
			return;

		if (MapUtils.isEmpty(mail.getPaths())) {
			mailSendService.sendMail(mail);
		} else {
			mailSendService.sendMailAndFile(mail);
		}
		LOG.info("Receiver Mail Obj !");
	}

	@RabbitHandler
	public void ReceiverMessage(String mail) {
		System.out.println(mail);
	}

}

复制代码

4、送信するために、非同期スレッドを使用してメールを送信する実際の方法を書きます

package com.ecej.nove.sms.mail;

import java.io.File;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.ecej.nove.base.mail.BaseMail;

@Service("mailSendService")
public class MailSendService {

	private Logger LOG = LoggerFactory.getLogger(MailSendService.class);

	@Value("${spring.mail.username}")
	private String from;

	@Resource
	private JavaMailSender mailSender;

	@Async("mailAsync")
	public void sendMail(BaseMail mail) {

		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from);
		message.setTo(mail.getTo());
		message.setSubject(mail.getSubject());
		message.setText(mail.getText());
		message.setBcc(mail.getBcc());
		message.setCc(mail.getCc());
		mailSender.send(message);
		LOG.info("发送邮件成功,邮件详情:{}", message.toString());
	}

	@Async("mailAsync")
	public void sendMailAndFile(BaseMail mail) {

		try {
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			helper.setFrom(from);
			helper.setTo(mail.getTo());
			helper.setSubject(mail.getSubject());
			helper.setText(mail.getText());
			helper.setBcc(mail.getBcc());
			helper.setCc(mail.getCc());
			mail.getPaths().entrySet().stream().forEach(set -> {
				FileSystemResource file = new FileSystemResource(new File(set.getValue()));
				try {
					helper.addAttachment(set.getKey(), file);

				} catch (MessagingException e) {
					LOG.error("SAVE MAIL FILE ERROR!", e);
				}

			});
			mailSender.send(mimeMessage);
			LOG.info("发送邮件成功,邮件详情:{}", mail.toString());
		} catch (Exception e) {
			LOG.error("SEND MAIL ERROR !", e);
		}
	}
}

```  
5、异步线程配置
复制代码

パッケージcom.ecej.nove.sms.config。

輸入java.util.concurrent.Executor。輸入java.util.concurrent.ThreadPoolExecutor。

輸入org.springframework.beans.factory.annotation.Value。輸入org.springframework.context.annotation.Bean。輸入org.springframework.context.annotation.Configuration。輸入org.springframework.scheduling.annotation.EnableAsync; 輸入org.springframework.scheduling.annotation.EnableScheduling。輸入org.springframework.scheduling.concurrent.ThreadPoolTask​​Executor。

/ **

  • 非同期タスクの分布を設定
  • @author QIANG

* / @Configuration @EnableScheduling @EnableAsyncパブリッククラスExecutorConfig {

@Value("${sms.executor.corePoolSize}")
private int corePoolSize;

@Value("${sms.executor.maxPoolSize}")
private int maxPoolSize;

@Value("${sms.executor.queueCapacity}")
private int queueCapacity;



@Bean(name = "mailAsync")
public Executor mailAsync() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	executor.setCorePoolSize(corePoolSize);
	executor.setMaxPoolSize(maxPoolSize);
	executor.setQueueCapacity(queueCapacity);
	executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
	executor.setThreadNamePrefix("MailExecutor-");
	executor.initialize();
	return executor;
}
复制代码

}


6、配置
复制代码

mail.close.flag = @ ecej.mail.close.flag @ spring.mail.host = @ ecej.mail.host @ spring.mail.username = @ ecej.mail.username @ spring.mail.password=@ecej。 mail.password @ spring.mail.properties.mail.smtp.auth = @ ecej.properties.mail.smtp.auth @ spring.mail.properties.mail.smtp.starttls.enable=@ecej.properties.mail.smtp。 starttls.enable @ spring.mail.properties.mail.smtp.starttls.required = @ ecej.properties.mail.smtp.starttls.required @


至此,通过MQ接收对象,然后发送邮件的需求就完成了,这个简单实用的接收端完成了,可以发送普通和带附件的邮件。当然,发送HTML之类的只是增加实例方法就行了。

然而,事情并没有这么简单,如果我要正常连接某邮件服务器这样就可以了。但是现在大多数,比喻集团邮件服务器是需要进行验证的。如果只有以上配置,不好意思,咔嚓给你来个异常。

![异常来咯](http://upload-images.jianshu.io/upload_images/4405663-c4926d8c13c49acc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

如此无奈,经各种官方打听,获得如下解决方案(网上什么加证书啊乱七八糟的,根本不靠谱)。
先来个官方版说法:
>When using SSL/TLS, it's important to ensure that the server you connect
to is actually the server you expected to connect to, to prevent "man in
the middle" attacks on your communication.  The recommended technique is
to configure the Java keystore using one of the methods described above.
If, for some reason, that approach is not workable, it's also possible
to configure the SSL/TLS implementation to use your own TrustManager
class to evaluate whether to trust the server you've connected to.

扯这么一大串,简单点说就是你可以自己搞个SSLSocketFactory自己去玩。
直接上菜:
复制代码

パッケージCom.achejknoveksmskmel;

インポートにjava.io.IOException; 輸入java.net.InetAddress; インポートのjava.net.Socket;

輸入javax.net.SocketFactory。輸入javax.net.ssl.SSLContext; 輸入javax.net.ssl.SSLSocketFactory。輸入javax.net.ssl.TrustManager。

パブリッククラスDummySSLSocketFactoryはのSSLSocketFactory {プライベートのSSLSocketFactory工場を拡張します。

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

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

@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();
}
复制代码

}

GO ON DOING...
复制代码

パッケージCom.achejknoveksmskmel;

輸入はjava.security.cert.X509Certificate。

輸入javax.net.ssl.X509TrustManager。

パブリッククラスDummyTrustManagerはのX509TrustManagerを実装{公共ボイドcheckClientTrustedメソッド(のX509Certificate [] CERT、文字列のauthType){//すべてが信頼されています}

public void checkServerTrusted(X509Certificate[] cert, String authType) {
	// everything is trusted
}

public X509Certificate[] getAcceptedIssuers() {
	return new X509Certificate[0];
}
复制代码

}

增加一点点配置文件
复制代码

spring.mail.properties.mail.imap.ssl.socketFactory.fallback = falseをspring.mail.properties.mail.smtp.ssl.socketFactory.class = com.ecej.nove.sms.mail.DummySSLSocketFactory

SO,这下这个恶心的小问题就解决了,附赠原版解读(有事还得找官方)
http://www.oracle.com/technetwork/java/javamail145sslnotes-1562622.html 

**小结**
spring 这一大群配置文件我还真记不住,所以给留一个全面的api
https://javaee.github.io/javamail/docs/api/com/sun/mail/smtp/package-summary.html
***
**关注公众号,获取最新推送**

![狍狍的日常生活](http://upload-images.jianshu.io/upload_images/4405663-ed916c1cdcfdbf15.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)复制代码

ます。https://juejin.im/post/5d0725dfe51d4556bc066f6eで再現

おすすめ

転載: blog.csdn.net/weixin_34117211/article/details/93176228