于俊龙 1 month ago
parent
commit
5752aaeee8

+ 5 - 1
README.md

@@ -11,4 +11,8 @@
      --farm-sankin            // Sankin: フォアグラウンドインタフェース定義モジュール
      --farm-sankin-biz        // Sankin: ビジネスモジュール
      --fram-common            // 共通処理モジュール
-     --farm-quartz            // スケジュールタスクモジュール
+     --farm-quartz            // スケジュールタスクモジュール
+
+
+ds-yamoto-farm-crm-ui
+  .gitignore

+ 12 - 0
farm-common/pom.xml

@@ -30,6 +30,12 @@
             <artifactId>spring-boot-starter-aop</artifactId>
         </dependency>
 
+        <!-- Mail -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-mail</artifactId>
+        </dependency>
+
         <!-- druid -->
         <dependency>
             <groupId>com.alibaba</groupId>
@@ -156,6 +162,12 @@
             <artifactId>jakarta.servlet-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 55 - 0
farm-common/src/main/java/jp/yamoto/farm/common/config/MailConfig.java

@@ -0,0 +1,55 @@
+package jp.yamoto.farm.common.config;
+
+import jp.yamoto.farm.common.utils.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
+import org.springframework.mail.javamail.JavaMailSender;
+import org.springframework.mail.javamail.JavaMailSenderImpl;
+
+import java.util.Properties;
+
+/**
+ * Mail Config
+ *
+ * @author nextosd
+ */
+@Configuration
+public class MailConfig {
+
+    @Autowired
+    private Environment env;
+
+    @Bean
+    public JavaMailSender javaMailSender() {
+
+        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
+
+        String host = env.getProperty("spring.mail.host");
+
+        if(StringUtils.isNotEmpty(host)) {
+            int port = env.getProperty("spring.mail.port", Integer.class);
+            String username = env.getProperty("spring.mail.username");
+            String password = env.getProperty("spring.mail.password");
+
+            mailSender.setHost(host);
+            mailSender.setPort(port);
+            mailSender.setUsername(username);
+            mailSender.setPassword(password);
+            mailSender.setDefaultEncoding("UTF-8");
+
+            Properties props = mailSender.getJavaMailProperties();
+            props.put("mail.transport.protocol", "smtp");
+            props.put("mail.smtp.auth", env.getProperty("spring.mail.properties.mail.smtp.auth"));
+            props.put("mail.smtp.starttls.enable", env.getProperty("spring.mail.properties.mail.smtp.starttls.enable"));
+            props.put("mail.smtp.starttls.required", env.getProperty("spring.mail.properties.mail.smtp.starttls.required"));
+            props.put("mail.smtp.connectiontimeout", env.getProperty("spring.mail.properties.mail.smtp.connectiontimeout"));
+            props.put("mail.smtp.timeout", env.getProperty("spring.mail.properties.mail.smtp.timeout"));
+            props.put("mail.smtp.writetimeout", env.getProperty("spring.mail.properties.mail.smtp.writetimeout"));
+            props.put("mail.debug", env.getProperty("spring.mail.debug"));
+        }
+
+        return mailSender;
+    }
+}

+ 158 - 0
farm-common/src/main/java/jp/yamoto/farm/common/core/service/MailService.java

@@ -0,0 +1,158 @@
+package jp.yamoto.farm.common.core.service;
+
+import jakarta.mail.MessagingException;
+import jakarta.mail.internet.MimeMessage;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
+import org.springframework.core.io.ByteArrayResource;
+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.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Date;
+
+@Service
+public class MailService {
+
+    @Autowired
+    private JavaMailSender mailSender;
+
+    @Autowired
+    private Environment env;
+
+    /**
+     * テキストメールの送信
+     */
+    public void sendSimpleEmail(String to, String subject, String text) {
+        SimpleMailMessage message = new SimpleMailMessage();
+        message.setFrom(this.getFromEmail());
+        message.setTo(to);
+        message.setSubject(subject);
+        message.setText(text);
+        message.setSentDate(new Date());
+
+        mailSender.send(message);
+    }
+
+    /**
+     * HTNLメールの送信
+     */
+    public void sendHtmlEmail(String to, String subject, String htmlContent)
+            throws MessagingException {
+        MimeMessage message = mailSender.createMimeMessage();
+        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
+
+        helper.setFrom(this.getFromEmail());
+        helper.setTo(to);
+        helper.setSubject(subject);
+        helper.setText(htmlContent, true);
+        helper.setSentDate(new Date());
+
+        mailSender.send(message);
+    }
+
+    /**
+     * 添付ファイル付きメールの送信
+     */
+    public void sendEmailWithAttachment(String to, String subject, String text,
+                                        File attachment) throws MessagingException {
+
+        MimeMessage message = mailSender.createMimeMessage();
+        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
+
+        helper.setFrom(this.getFromEmail());
+        helper.setTo(to);
+        helper.setSubject(subject);
+        helper.setText(text);
+        helper.setSentDate(new Date());
+
+        // 添付ファイルの追加
+        helper.addAttachment(attachment.getName(), attachment);
+
+        mailSender.send(message);
+    }
+
+    /**
+     * 複数の添付ファイル付きメールを送信
+     */
+    public void sendEmailWithAttachments(String to, String subject, String text,
+                                         MultipartFile[] attachments)
+            throws MessagingException, IOException {
+
+        MimeMessage message = mailSender.createMimeMessage();
+        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
+
+        helper.setFrom(this.getFromEmail());
+        helper.setTo(to);
+        helper.setSubject(subject);
+        helper.setText(text);
+        helper.setSentDate(new Date());
+
+        for (MultipartFile file : attachments) {
+            if (!file.isEmpty()) {
+                helper.addAttachment(file.getOriginalFilename(),
+                        new ByteArrayResource(file.getBytes()));
+            }
+        }
+
+        mailSender.send(message);
+    }
+
+    /**
+     * インラインリソース付きメールの送信(画像など)
+     */
+    public void sendEmailWithInlineResource(String to, String subject, String htmlContent,
+                                            String resourcePath, String contentId)
+            throws MessagingException {
+        MimeMessage message = mailSender.createMimeMessage();
+        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
+
+        helper.setFrom(this.getFromEmail());
+        helper.setTo(to);
+        helper.setSubject(subject);
+        helper.setText(htmlContent, true);
+        helper.setSentDate(new Date());
+
+        FileSystemResource res = new FileSystemResource(new File(resourcePath));
+        helper.addInline(contentId, res);
+
+        mailSender.send(message);
+    }
+
+    /**
+     * 一括送信メール
+     */
+    public void sendBatchEmail(String[] toList, String subject, String text) {
+        Arrays.stream(toList).forEach(to -> sendSimpleEmail(to, subject, text));
+    }
+
+    /**
+     * CCと密送付きのメールを送信する
+     */
+    public void sendEmailWithCcBcc(String to, String[] cc, String[] bcc,
+                                   String subject, String text) throws MessagingException {
+        MimeMessage message = mailSender.createMimeMessage();
+        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
+
+        helper.setFrom(this.getFromEmail());
+        helper.setTo(to);
+        if (cc != null) helper.setCc(cc);
+        if (bcc != null) helper.setBcc(bcc);
+        helper.setSubject(subject);
+        helper.setText(text);
+        helper.setSentDate(new Date());
+
+        mailSender.send(message);
+    }
+
+    private String getFromEmail(){
+        return env.getProperty("spring.mail.from");
+    }
+
+}

+ 19 - 1
farm-crm/src/main/resources/application.yml

@@ -88,7 +88,25 @@ spring:
           max-active: 8
           # 接続プールの最大ブロック待ち時間(制限なしの負の値を使用)
           max-wait: -1ms
-
+  mail:
+    host: smtp.gmail.com
+    port: 587
+    username: your-email@qq.com
+    password: your-authorization-code
+    from: your-email@qq.com
+    protocol: smtp
+    default-encoding: UTF-8
+    debug: true
+    properties:
+      mail:
+        smtp:
+          auth: true
+          starttls:
+            enable: true
+            required: true
+          connectiontimeout: 5000
+          timeout: 5000
+          writetimeout: 5000
 # token
 token:
   # トークンカスタムID