赞
踩
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-mail</artifactId>
- </dependency>
- spring:
- mail:
- host: smtp.qq.com #协议,163邮箱就是smtp.163.com
- username: 123456@qq.com #发送端的邮箱地址
- password: gmsgycitwulgdifd #发送端的邮箱的smtp的密码
- port: 465 #腾讯qq的邮箱端口
- default-encoding: utf-8
- protocol: smtps #与端口绑定的协议,必须设置,而不是采用默认的smtp。要不然抛异常
- @Autowired
- private JavaMailSender javaMailSender; //发送邮件的类
- @Override
- public void sendMail(Mail mail) {
- SimpleMailMessage mailMessage = new SimpleMailMessage(); //邮件的类
- mailMessage.setFrom(mail.getFrom()); //发送源的邮箱地址 "123456@qq.com"
- mailMessage.setTo(mail.getTo()); //目的邮箱的地址 "1234567@163.com"
- mailMessage.setSubject(mail.getSubject()); //邮件的标题
- mailMessage.setText(mail.getText()); //邮件内容
- javaMailSender.send(mailMessage); //发送邮件
- }
可以是超链接,图片等
- @Autowired
- private JavaMailSender javaMailSender;
- @Override
- public void sendMail(Mail mail) {
-
- try {
- MimeMessage message = javaMailSender.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(message);
- helper.setFrom(mail.getFrom());
- List<String> to = mail.getTo();
- helper.setTo(to.toArray(new String[to.size()])); //发送给多个邮箱
- helper.setSubject(mail.getSubject());
- //"<img src='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.lemeitu.com%2Fm00%2F19%2F17%2Ff9bbd26a791748cb501443a8527a8f44__w.jpg&refer=http%3A%2F%2Fimg.lemeitu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1670850728&t=33c72749872d1b0d60e1bab456fcfa29'/><a href='https://www.baidu.com/'>点开有惊喜</a>"
- helper.setText(mail.getText(),true);//开启html,内容可以是超链接、图片等
- javaMailSender.send(message);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart) throws MessagingException {
- this(mimeMessage, multipart, (String)null);
- }
- @Autowired
- private JavaMailSender javaMailSender;
- @Override
- public void sendMail(Mail mail) {
-
- try {
- MimeMessage message = javaMailSender.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(message,true); //开启附件传输
- helper.setFrom(mail.getFrom());
- List<String> to = mail.getTo();
- helper.setTo(to.toArray(new String[to.size()])); //发送给多个邮箱
- helper.setSubject(mail.getSubject());
- helper.setText(mail.getText(),true);
- //添加附件
- File f1 = new File(mail.getFileName());
- helper.addAttachment(f1.getName(),f1);//文件名,文件句柄
-
- javaMailSender.send(message);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。