This time, only QQ mailbox is configured to send
Utility class
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Map; @Component public class MailService { @Autowired JavaMailSender javaMailSender ; public void sandAttachMail(String from, String to, String subject, String content, File file) { try { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content); helper.addAttachment(file.getName(),file); javaMailSender.send(message); }catch (MessagingException e) { e.printStackTrace(); }}}Copy the code
The test class
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; import java.io.File; @RunWith(SpringRunner.class) @SpringBootTest public class MailApplicationTests { @Autowired MailService mailService; @Test public void sendAttachMail() { mailService.sandAttachMail("[email protected]", "[email protected]"," test email subject "," test email content ",new File(" Attachment to send ")); }}Copy the code