+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.messaging.mail.autoconfigure; + +import jakarta.annotation.PostConstruct; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.context.annotation.PropertySource; +import top.charles7c.continew.starter.core.handler.GeneralPropertySourceFactory; + +/** + * 邮件自动配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@AutoConfiguration +@PropertySource(value = "classpath:default-messaging-mail.yml", factory = GeneralPropertySourceFactory.class) +public class MailAutoConfiguration { + + @PostConstruct + public void postConstruct() { + log.info("[ContiNew Starter] - Auto Configuration 'Mail' completed initialization."); + } +} diff --git a/continew-starter-messaging/continew-starter-messaging-mail/src/main/java/top/charles7c/continew/starter/messaging/mail/util/MailUtils.java b/continew-starter-messaging/continew-starter-messaging-mail/src/main/java/top/charles7c/continew/starter/messaging/mail/util/MailUtils.java new file mode 100644 index 00000000..884b8e9b --- /dev/null +++ b/continew-starter-messaging/continew-starter-messaging-mail/src/main/java/top/charles7c/continew/starter/messaging/mail/util/MailUtils.java @@ -0,0 +1,241 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.messaging.mail.util;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.lang.Assert;
+import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.spring.SpringUtil;
+import jakarta.mail.MessagingException;
+import jakarta.mail.internet.MimeMessage;
+import lombok.AccessLevel;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.mail.javamail.JavaMailSender;
+import org.springframework.mail.javamail.MimeMessageHelper;
+import top.charles7c.continew.starter.core.constant.StringConstants;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * 邮件工具类
+ *
+ * @author Charles7c
+ * @since 1.0.0
+ */
+@Data
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class MailUtils {
+
+ private static final JavaMailSender MAIL_SENDER = SpringUtil.getBean(JavaMailSender.class);
+
+ /**
+ * 发送文本邮件给单个人
+ *
+ * @param subject
+ * 主题
+ * @param content
+ * 内容
+ * @param to
+ * 收件人
+ * @throws MessagingException
+ * /
+ */
+ public static void sendText(String to, String subject, String content) throws MessagingException {
+ send(splitAddress(to), null, null, subject, content, false);
+ }
+
+ /**
+ * 发送 HTML 邮件给单个人
+ *
+ * @param subject
+ * 主题
+ * @param content
+ * 内容
+ * @param to
+ * 收件人
+ * @throws MessagingException
+ * /
+ */
+ public static void sendHtml(String to, String subject, String content) throws MessagingException {
+ send(splitAddress(to), null, null, subject, content, true);
+ }
+
+ /**
+ * 发送 HTML 邮件给单个人
+ *
+ * @param subject
+ * 主题
+ * @param content
+ * 内容
+ * @param to
+ * 收件人
+ * @param files
+ * 附件列表
+ * @throws MessagingException
+ * /
+ */
+ public static void sendHtml(String to, String subject, String content, File... files) throws MessagingException {
+ send(splitAddress(to), null, null, subject, content, true, files);
+ }
+
+ /**
+ * 发送 HTML 邮件给多个人
+ *
+ * @param subject
+ * 主题
+ * @param content
+ * 内容
+ * @param tos
+ * 收件人列表
+ * @param files
+ * 附件列表
+ * @throws MessagingException
+ * /
+ */
+ public static void sendHtml(Collection