io.github.dengchen2020.message.email.EmailClientImpl Maven / Gradle / Ivy
package io.github.dengchen2020.message.email;
import io.github.dengchen2020.message.config.MessageAutoConfiguration;
import jakarta.mail.internet.InternetAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import java.io.File;
import java.util.Date;
/**
* 邮件发送默认实现
*
* @author xiaochen
* @since 2024/6/4
*/
@Async(MessageAutoConfiguration.MESSAGE_SEND_ASYNC_EXECUTOR)
public class EmailClientImpl implements EmailClient {
private static final Logger log = LoggerFactory.getLogger(EmailClientImpl.class);
private final JavaMailSender javaMailSender;
private final String[] to;
public EmailClientImpl(JavaMailSender javaMailSender, String[] strings) {
this.javaMailSender = javaMailSender;
to = strings;
}
@Override
public void sendText(SimpleMailMessage mail) {
if (javaMailSender instanceof JavaMailSenderImpl javaMailSenderImpl) {
mail.setFrom(javaMailSenderImpl.getUsername());
}
if (mail.getTo() == null || mail.getTo().length == 0) {
mail.setTo(to);
}
try {
javaMailSender.send(mail);
} catch (MailException e) {
log.error("邮件发送失败:{}", e.toString());
}
}
@Override
public void sendText(String subject, String text, String... to) {
SimpleMailMessage mail = new SimpleMailMessage();
if (javaMailSender instanceof JavaMailSenderImpl javaMailSenderImpl) {
mail.setFrom(javaMailSenderImpl.getUsername());
}
if (to == null || to.length == 0) {
mail.setTo(this.to);
} else {
mail.setTo(to);
}
mail.setSentDate(new Date());
mail.setSubject(subject);
mail.setText(text);
try {
javaMailSender.send(mail);
} catch (MailException e) {
log.error("邮件发送失败:{}", e.toString());
}
}
public void sendMime(String subject, String html, File[] inlines, File[] attachments, String... to) {
try {
MimeMessageHelper mime = new MimeMessageHelper(javaMailSender.createMimeMessage(), true, "UTF-8");
if (javaMailSender instanceof JavaMailSenderImpl javaMailSenderImpl) {
mime.setFrom(javaMailSenderImpl.getUsername());
}
if (to == null || to.length == 0) {
to = this.to;
}
InternetAddress[] address = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
address[i] = new InternetAddress(to[i]);
}
mime.setTo(address);
mime.setSubject(subject);
mime.setText(html, true);
if (inlines != null) {
for (File inline : inlines) {
mime.addInline(inline.getName(), inline);
}
}
if (attachments != null) {
for (File attachment : attachments) {
mime.addAttachment(attachment.getName(), attachment);
}
}
javaMailSender.send(mime.getMimeMessage());
} catch (Exception e) {
log.error("mime邮件发送失败:{}", e.toString());
}
}
@Override
public void sendMime(String subject, String html, File[] inlines, String... to) {
sendMime(subject, html, inlines, null, to);
}
@Override
public void send(String subject, String html, File[] attachments, String... to) {
sendMime(subject, html, null, attachments, to);
}
@Override
public void send(String subject, String html, String... to) {
sendMime(subject, html, null, to);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy