|
@@ -0,0 +1,106 @@
|
|
|
+package com.ym.mec.thirdparty.message.provider;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.mail.DefaultAuthenticator;
|
|
|
+import org.apache.commons.mail.Email;
|
|
|
+import org.apache.commons.mail.EmailException;
|
|
|
+import org.apache.commons.mail.SimpleEmail;
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.ym.mec.thirdparty.message.MessageSenderPlugin;
|
|
|
+
|
|
|
+//@Service
|
|
|
+public class CommEmailPlugin implements MessageSenderPlugin, InitializingBean {
|
|
|
+
|
|
|
+ private String hostName;
|
|
|
+
|
|
|
+ private int smtpPort;
|
|
|
+
|
|
|
+ private String userName;
|
|
|
+
|
|
|
+ private String password;
|
|
|
+
|
|
|
+ private String from;
|
|
|
+
|
|
|
+ private String fromName;
|
|
|
+
|
|
|
+ public void setHostName(String hostName) {
|
|
|
+ this.hostName = hostName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setSmtpPort(int smtpPort) {
|
|
|
+ this.smtpPort = smtpPort;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUserName(String userName) {
|
|
|
+ this.userName = userName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setPassword(String password) {
|
|
|
+ this.password = password;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setFrom(String from) {
|
|
|
+ this.from = from;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setFromName(String fromName) {
|
|
|
+ this.fromName = fromName;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
+ if (StringUtils.isBlank(hostName)) {
|
|
|
+ throw new RuntimeException("SMTP服务器没有配置");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (smtpPort == 0) {
|
|
|
+ throw new RuntimeException("SMTP服务器端口没有配置");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(userName)) {
|
|
|
+ throw new RuntimeException("SMTP服务器认证用户名没有配置");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(password)) {
|
|
|
+ throw new RuntimeException("SMTP服务器认证密码没有配置");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(from)) {
|
|
|
+ throw new RuntimeException("发件人邮箱没有配置");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(fromName)) {
|
|
|
+ fromName = from;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean send(String subject, String content, String receiver, String url, String jpushType, String sound, String channelId) throws Exception {
|
|
|
+ Email email = new SimpleEmail();
|
|
|
+ email.setHostName(hostName);
|
|
|
+ email.setSslSmtpPort(smtpPort + "");
|
|
|
+ email.setAuthenticator(new DefaultAuthenticator(userName, password));
|
|
|
+ // email.setSSLOnConnect(true);
|
|
|
+ try {
|
|
|
+ email.setFrom(from, fromName);
|
|
|
+ email.setSubject(subject);
|
|
|
+ email.setMsg(content);
|
|
|
+ email.addTo(receiver);
|
|
|
+ email.send();
|
|
|
+ return true;
|
|
|
+ } catch (EmailException e) {
|
|
|
+ throw new RuntimeException("发送邮件出现异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean batchSend(String subject, String content, String[] receivers, String url, String jpushType, String sound, String channelId) throws Exception {
|
|
|
+ for (String rev : receivers) {
|
|
|
+ send(subject, content, rev, url, jpushType, sound, channelId);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|