|
@@ -1,13 +1,142 @@
|
|
package com.hssx.website;
|
|
package com.hssx.website;
|
|
|
|
|
|
|
|
+import com.hssx.website.entity.Comment;
|
|
|
|
+import com.hssx.website.service.IMailService;
|
|
import org.junit.Test;
|
|
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.boot.test.context.SpringBootTest;
|
|
|
|
+import org.springframework.mail.MailSender;
|
|
|
|
+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.test.context.junit4.SpringRunner;
|
|
|
|
+
|
|
|
|
+import javax.mail.MessagingException;
|
|
|
|
+import javax.mail.internet.MimeMessage;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Date;
|
|
|
|
|
|
@SpringBootTest
|
|
@SpringBootTest
|
|
-class WebsiteApplicationTests {
|
|
|
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
|
+public class WebsiteApplicationTests {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 注入发送邮件的接口
|
|
|
|
+ */
|
|
|
|
+ @Autowired
|
|
|
|
+ private IMailService mailService;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 测试发送文本邮件
|
|
|
|
+ */
|
|
@Test
|
|
@Test
|
|
- void contextLoads() {
|
|
|
|
|
|
+ public void sendmail() {
|
|
|
|
+// mailService.sendSimpleMail("1069406348@qq.com","老弟给姐来邮件了","聪明伶俐、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "沉鱼落雁、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "闭月羞花、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "风华绝代、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "花容月貌、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "亭亭玉立、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "美如冠玉、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "眉目如画、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "风度翩翩、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "国色天香、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "倾国倾城、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "出水芙蓉、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "天生丽质、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "珠光宝气、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "明眸皓齿、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "冰肌玉骨、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "秀色可餐、\n" +
|
|
|
|
+// "\n" +
|
|
|
|
+// "金枝玉叶");
|
|
|
|
+// mailService.sendSimpleMail("976575821@qq.com","主题:你好普通邮件","内容:第一封邮件");
|
|
|
|
+ Comment comment = new Comment();
|
|
|
|
+ comment.setComment("你好呀小伙子");
|
|
|
|
+ comment.setName("小猴");
|
|
|
|
+ comment.setPhone("123456789");
|
|
|
|
+ String text = "<!DOCTYPE html>"
|
|
|
|
+ + "<html lang='en'>"
|
|
|
|
+ + "<head>"
|
|
|
|
+ + "<meta charset='UTF-8'>"
|
|
|
|
+ + "<title>邮件提醒</title>"
|
|
|
|
+ + "<meta name='viewport' content='width=device-width, initial-scale=1.0'/>"
|
|
|
|
+ + "</head>"
|
|
|
|
+ + "<body style=margin: 0; padding: 0;>"
|
|
|
|
+ + "<table align='center' border='0' cellpadding='0' cellspacing='0' width='600' style='border-collapse: collapse;'>"
|
|
|
|
+ + "<tr>"
|
|
|
|
+ + "<td>"
|
|
|
|
+ + "<div style='margin: 40px;text-align: center;margin-top: 50px;border-bottom:1px solid gray;padding:20px'>"
|
|
|
|
+ + "</div>"
|
|
|
|
+ + "</td>"
|
|
|
|
+ + "</tr>"
|
|
|
|
+ + "<tr>"
|
|
|
|
+ + "<td>"
|
|
|
|
+ + "<div style='margin: 40px'>"
|
|
|
|
+ + "<p style='font-size: 16px'>" + comment.getComment() + "</p>"
|
|
|
|
+ + "</div>"
|
|
|
|
+ + "</td>"
|
|
|
|
+ + "</tr>"
|
|
|
|
+ + "<tr>"
|
|
|
|
+ + "<td>"
|
|
|
|
+ + "<div align='right' style='margin: 40px;border-top: solid 1px gray' id='bottomTime'>"
|
|
|
|
+ + "<p style='margin-right: 20px'>留言人: " + comment.getName() + "</p>"
|
|
|
|
+ + "<p style='margin-right: 20px'>电话: " + comment.getPhone() + "</p>"
|
|
|
|
+ + "<label style='margin-right: 20px'>留言日期: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "</label>"
|
|
|
|
+ + "</div>"
|
|
|
|
+ + "</td>"
|
|
|
|
+ + "</tr>"
|
|
|
|
+ + "</table>"
|
|
|
|
+ + "</body>";
|
|
|
|
+
|
|
|
|
+// String text = "<html>\n"+"<body>\n"
|
|
|
|
+// + "<h3>hello world!测试发送html格式邮件</h3>\n"
|
|
|
|
+// +"</body>\n"+"</html>";
|
|
|
|
+// mailService.sendHtmlMail("18130408125@163.com","新留言通知",text);
|
|
|
|
+ mailService.sendHtmlMail("18130408125@163.com","新留言通知",text);
|
|
|
|
+ System.out.println("发送成功");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// @Test
|
|
|
|
+// public void sendmailHtml(){
|
|
|
|
+// mailService.sendHtmlMail("smfx1314@163.com","主题:你好html邮件","<h1>内容:第一封html邮件</h1>");
|
|
|
|
+// }
|
|
|
|
+// @Autowired
|
|
|
|
+// JavaMailSender mailSender; //添加依赖后可以直接引用
|
|
|
|
+//
|
|
|
|
+// public void sendMail(String emailForm,String[] emailTo,String title,String context){
|
|
|
|
+// System.setProperty("java.net.preferIPv4Stack", "true");
|
|
|
|
+// MimeMessage mimeMessage = mailSender.createMimeMessage();
|
|
|
|
+// MimeMessageHelper helper;
|
|
|
|
+// try {
|
|
|
|
+// helper = new MimeMessageHelper(mimeMessage, true);
|
|
|
|
+// helper.setFrom(emailForm);
|
|
|
|
+// helper.setTo(emailTo);
|
|
|
|
+// helper.setSubject(title);//主题
|
|
|
|
+// helper.setText(context);//正文
|
|
|
|
+// mailSender.send(mimeMessage);
|
|
|
|
+// } catch (MessagingException e1) {
|
|
|
|
+// // TODO Auto-generated catch block
|
|
|
|
+// e1.printStackTrace();
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+
|
|
}
|
|
}
|