@ -0,0 +1,54 @@ |
|||||||
|
package cc.bnblogs.common; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.mail.javamail.JavaMailSender; |
||||||
|
import org.springframework.mail.javamail.MimeMessageHelper; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import javax.mail.MessagingException; |
||||||
|
import javax.mail.internet.MimeMessage; |
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author zfp@bnblogs.cc |
||||||
|
* @createTime: 2022/10/22 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
@Component |
||||||
|
public class MailHelper { |
||||||
|
@Value("${spring.mail.username}") |
||||||
|
private String from; |
||||||
|
private final JavaMailSender javaMailSender; |
||||||
|
|
||||||
|
public MailHelper(JavaMailSender javaMailSender) { |
||||||
|
this.javaMailSender = javaMailSender; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 发送邮件 |
||||||
|
* @param toSend 收件人(目标邮箱) |
||||||
|
* @param subject 主题 |
||||||
|
* @param text 内容 |
||||||
|
*/ |
||||||
|
public void sendMail(String toSend, String subject, String text) { |
||||||
|
MimeMessage message = javaMailSender.createMimeMessage(); |
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message); |
||||||
|
try { |
||||||
|
//设置发件时间
|
||||||
|
helper.setSentDate(new Date()); |
||||||
|
// 发件人(配置文件中的邮箱)
|
||||||
|
helper.setFrom(from); |
||||||
|
//设置收件人
|
||||||
|
helper.setTo(toSend); |
||||||
|
//设置标签
|
||||||
|
helper.setSubject(subject); |
||||||
|
//设置内容
|
||||||
|
helper.setText(text, true); |
||||||
|
//发邮件
|
||||||
|
javaMailSender.send(message); |
||||||
|
} catch (MessagingException e) { |
||||||
|
log.error(e.getMessage(), e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
After Width: | Height: | Size: 4.8 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 9.0 KiB |
After Width: | Height: | Size: 6.2 KiB |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 6.7 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 43 KiB |
After Width: | Height: | Size: 26 KiB |
@ -0,0 +1,25 @@ |
|||||||
|
package cc.bnblogs.test; |
||||||
|
|
||||||
|
import cc.bnblogs.Application; |
||||||
|
import cc.bnblogs.common.DefaultImages; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||||
|
import org.springframework.test.context.junit4.SpringRunner; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author zfp@bnblogs.cc |
||||||
|
* @createTime: 2022/10/22 |
||||||
|
*/ |
||||||
|
@RunWith(SpringRunner.class) |
||||||
|
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
||||||
|
public class AvatarTest { |
||||||
|
@Autowired |
||||||
|
private DefaultImages defaultImages; |
||||||
|
@Test |
||||||
|
public void testAvatar() { |
||||||
|
System.out.println(defaultImages.avatar("1337425156@qq.com")); |
||||||
|
System.out.println(defaultImages.avatar("1337425156@q1q.com")); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,25 @@ |
|||||||
|
package cc.bnblogs.test; |
||||||
|
|
||||||
|
import cc.bnblogs.Application; |
||||||
|
import cc.bnblogs.common.MailHelper; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||||
|
import org.springframework.test.context.junit4.SpringRunner; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author zfp@bnblogs.cc |
||||||
|
* @createTime: 2022/10/22 |
||||||
|
*/ |
||||||
|
@RunWith(SpringRunner.class) |
||||||
|
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
||||||
|
public class MailTest { |
||||||
|
@Autowired |
||||||
|
private MailHelper helper; |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testSend(){ |
||||||
|
helper.sendMail("1337425156@qq.com","测试邮件","<h1>测试邮件</h1><p>我是一个测试邮件</p>"); |
||||||
|
} |
||||||
|
} |