通過javamailsenderimpl傳送郵件
阿新 • • 發佈:2018-11-05
參考文章:http://blog.csdn.net/qq_33556185/article/details/51028952
import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; importorg.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.io.File; import java.util.Properties; @Controller @RequestMapping("/api")
public class SendMailController { @RequestMapping(value = "/sendmsg", method = RequestMethod.GET) @ResponseBody public String sendMessage(@RequestParam String mail){ JavaMailSenderImpl senderImpl = newJavaMailSenderImpl(); // 設定mail server senderImpl.setHost("smtp.qq.com"); senderImpl.setUsername("*******@qq.com"); // 根據自己的情況,設定username senderImpl.setPassword("********"); // 根據自己的情況, 設定password,注意qq郵箱的話此處設定16位授權碼,不是郵箱密碼,切記! //加認證機制 Properties javaMailProperties = new Properties(); javaMailProperties.put("mail.smtp.auth", true); javaMailProperties.put("mail.smtp.starttls.enable", true); javaMailProperties.put("mail.smtp.timeout", 5000); senderImpl.setJavaMailProperties(javaMailProperties); // 建立郵件訊息 SimpleMailMessage mailMessage = new SimpleMailMessage(); // 設定收件人,寄件人 用陣列傳送多個郵件 // String[] array = new String[] {"[email protected]","[email protected]"}; // mailMessage.setTo(array); mailMessage.setTo("**********@qq.com"); mailMessage.setFrom("*********@qq.com "); mailMessage.setSubject(" 測試簡單文字郵件傳送! "); mailMessage.setText(" 測試我的簡單郵件傳送機制!! "); // 傳送郵件 senderImpl.send(mailMessage); System.out.println(" 郵件傳送成功.. "); return "haHa"; }
//有附件的郵件傳送 @RequestMapping(value = "/sendPht", method = RequestMethod.GET) @ResponseBody public String sendPht(@RequestParam String filePath)throws Exception{ JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost("smtp.qq.com"); javaMailSender.setUsername("**********@qq.com"); javaMailSender.setPassword("**********"); Properties javaMailProperties = new Properties(); javaMailProperties.put("mail.smtp.auth", true); javaMailProperties.put("mail.smtp.starttls.enable", true); javaMailProperties.put("mail.smtp.timeout", 5000); javaMailSender.setJavaMailProperties(javaMailProperties); //使用JavaMail的MimeMessage,支付更加複雜的郵件格式和內容 MimeMessage msg = javaMailSender.createMimeMessage(); //建立MimeMessageHelper物件,處理MimeMessage的輔助類 MimeMessageHelper helper = new MimeMessageHelper(msg, true); //使用輔助類MimeMessage設定引數 helper.setFrom(javaMailSender.getUsername()); helper.setTo("************@qq.com"); helper.setSubject("Hello Attachment"); helper.setText("This is a mail with attachment"); //載入檔案資源,作為附件 File file = new File(filePath); if(!file.exists()){ return "檔案不存在"; } //加入附件 helper.addAttachment(file.getName(), file); // 傳送郵件 javaMailSender.send(msg); return "HaHaHaHaHaHa"; } }