j2ee 开发web两种邮件发送方式

1、javax.mail支持发送

1-1、pom文件gav配置

<!--JavaMail基本包--><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version></dependency><!--邮件发送的扩展包--><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency>

1-2、邮件配置属性

163邮箱配置实例:

        Properties properties = new Properties();properties.setProperty("mail.transport.protocol", "smtp");properties.setProperty("mail.smtp.host", mailSmtpHost);properties.setProperty("mail.smtp.auth", "true");properties.setProperty("mail.smtp.port",port);

gmail邮箱配置实例:

        Properties properties = new Properties();properties.setProperty("mail.transport.protocol", "smtp");properties.setProperty("mail.smtp.host", mailSmtpHost);properties.setProperty("mail.smtp.auth", "true");properties.setProperty("mail.smtp.port",port);properties.setProperty("mail.smtp.starttls.enable", "true");properties.setProperty("mail.smtp.starttls.required", "true");properties.setProperty("mail.smtp.ssl.enable", "true");properties.setProperty("mail.smtp.auth", "true");

1-3、邮件发送代码片段

    public static final String MAIL_TEMPLATE_CONTENT = "<!DOCTYPE html><html><body><p>用户:%s</p><p>图片:</p><p>(1)、<img src='cid:picture_view1'/> </p><body><html>";Session session = Session.getInstance(properties);MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress(mailAccount));InternetAddress[] internetAddresses = InternetAddress.parse(receiveMaile);message.setRecipients(Message.RecipientType.TO, internetAddresses);message.setSubject("主题");//设置邮件发送内容message.setContent(createMimeMultipart(mailContentDTO));message.saveChanges();Transport transport = session.getTransport();transport.connect(mailAccount, mailAccountPassword);transport.sendMessage(message, message.getAllRecipients());transport.close();public MimeMultipart createMimeMultipart(MailContentDTO mailContentDTO) throws MessagingException, UnsupportedEncodingException {MimeMultipart mmTextImage = new MimeMultipart();int picNum = 1;for (MultipartFile multipartFile : mailContentDTO.getMultipartFiles()) {File file1 = new File(sendmailLocaldirPath);if (!file1.exists()) {file1.mkdirs();}String fileName = multipartFile.getOriginalFilename();if (fileName == null) {continue;}log.info("fileName={}", fileName);String fileNameEndPrefix = "";if (fileName.contains(".")) {fileNameEndPrefix = fileName.substring(fileName.lastIndexOf(".") - 1);}File file = new File(String.format(LOCAL_FILE_PATH, sendmailLocaldirPath, UUID.randomUUID().toString(), fileNameEndPrefix));try (InputStream inputStream = multipartFile.getInputStream();FileOutputStream fileOutputStream = new FileOutputStream(file);ByteArrayOutputStream output = new ByteArrayOutputStream();) {byte[] buffer = new byte[1024];int i = 0;while ((i = inputStream.read(buffer)) != -1) {output.write(buffer, 0, i);}fileOutputStream.write(output.toByteArray());} catch (IOException e) {log.error(e.getMessage(), e);continue;}MimeBodyPart image = new MimeBodyPart();image.setDataHandler(new DataHandler(new FileDataSource(file)));image.setContentID("picture_view" + picNum);mmTextImage.addBodyPart(image);picNum++;}MimeBodyPart text = new MimeBodyPart();text.setContent(mimeMessageHelper.setText(String.format(MAIL_TEMPLATE_CONTENT,"用户名"),  "text/html;charset=UTF-8");mmTextImage.addBodyPart(text);mmTextImage.setSubType("related");return mmTextImage;}

2、spring boot支持发送

2-1、pom文件gav配置

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2-1、邮件配置属性

163邮箱配置实例:

spring:mail:host: smtp.163.compassword: xxxxusername: xxx@163.comport: 25recive:mail: xx1@aaa.com,xxx2@qq.com

gmail邮箱配置实例:

spring:mail:username: xxx@gmail.compassword: xxxxxxxhost: smtp.gmail.comport: 465properties:mail:smtp:starttls:enable: truerequired: truessl:enable: trueauth: truerecive:mail: xxx1@aaa.com,xxx3@bbb.com,xx2@ccc.com

spring官方文档参考地址:
https://docs.spring.io/spring-boot/docs/current/reference/html/io.html#io.email
https://docs.spring.io/spring-framework/reference/integration/email.html
配置文件官方文档参考地址:
https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties.mail

2-3、邮件发送代码片段

public static final String MAIL_TEMPLATE_CONTENT = "<!DOCTYPE html><html><body><p>用户:%s</p><p>图片:</p><p>(1)、<img src='cid:picture_view1'/> </p><body><html>";MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);mimeMessageHelper.setFrom(username);mimeMessageHelper.setText(String.format(MAIL_TEMPLATE_CONTENT,"用户名"), true);mimeMessageHelper.setSubject("主题");//设置邮件发送内容createMimeMultipart(mailContentDTO, mimeMessageHelper);mimeMessageHelper.setTo(mailContentDTO.getUserMail());mailSender.send(message);mimeMessageHelper.setTo(receiveMaile.split(","));mailSender.send(message);public MimeMessageHelper createMimeMultipart(MailContentDTO mailContentDTO, MimeMessageHelper mimeMessageHelper)throws MessagingException, UnsupportedEncodingException {MimeMultipart mmTextImage = new MimeMultipart();int picNum = 1;for (MultipartFile multipartFile : mailContentDTO.getMultipartFiles()) {File file1 = new File(sendmailLocaldirPath);if (!file1.exists()) {file1.mkdirs();}String fileName = multipartFile.getOriginalFilename();if (fileName == null) {continue;}log.info("fileName={}", fileName);String fileNameEndPrefix = "";if (fileName.contains(".")) {fileNameEndPrefix = fileName.substring(fileName.lastIndexOf(".") - 1);}File file = new File(String.format(SendMailManager.LOCAL_FILE_PATH, sendmailLocaldirPath, UUID.randomUUID().toString(), fileNameEndPrefix));try (InputStream inputStream = multipartFile.getInputStream();FileOutputStream fileOutputStream = new FileOutputStream(file);ByteArrayOutputStream output = new ByteArrayOutputStream();) {byte[] buffer = new byte[1024];int i = 0;while ((i = inputStream.read(buffer)) != -1) {output.write(buffer, 0, i);}fileOutputStream.write(output.toByteArray());} catch (IOException e) {log.error(e.getMessage(), e);continue;}mimeMessageHelper.addInline("picture_view" + picNum, new FileDataSource(file));picNum++;}return mimeMessageHelper;}

本文链接:https://my.lmcjl.com/post/3767.html

展开阅读全文

4 评论

留下您的评论.