java自定义异常处理类
在一个团队里面,一个公司里面总是统一规则让大家遵守,自定义异常处理类也是一种统一风格的体现,使用统一的异常编码,统一的消息格式。
邮件在工作中必不可少,很多系统都会用到邮件,企业内部发布通知,研发人员系统告警等都需要使用邮件。下面简单来了解一下使用java开放发邮件功能。
本文只是简单说明一下使用java实现邮件发送,具体业务具体封装。
废话不多说,直接上码。
1、使用spring cloud项目,引用依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、配置相关信息,具体配置参数取值,登录自己的邮件服务器查看,每家的配置都不一样,我这里使用的是腾讯的企业邮箱。
spring:
mail:
host: smtp.exmail.qq.com
port: 465
username: uud@nuosoon.com
password: 12smik
protocol: smtps
properties:
mail:
display:
sendname: 迈极集团
sendmail: 迈极集团
from: admin@nuosoon.com
app:
mail:
defaultFrom: uud@nuosoon.com
warningTo: weilikoo@qq.com
3、封装自己的发送邮件类。
package com.vlihub.common.helper;
import com.alibaba.fastjson.JSON;
import com.vlihub.common.configs.AppMailConfig;
import com.vlihub.common.exception.VlihubException;
import com.vlihub.common.model.vo.MailAttachment;
import com.vlihub.common.model.vo.MailInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.activation.DataSource;
import javax.activation.URLDataSource;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.net.URL;
/**
* 创建人: 537974(韦立)
* 创建时间: 2021-2-2 13:52
*/
@Service
@Slf4j
public class MailHelper {
@Resource
private JavaMailSender mailSender;
@Resource
private AppMailConfig appMailConfig;
/**
* 发送文本邮件
*/
public void sendTextMail(MailInfo mailInfo) {
try {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(mailInfo.getFrom());
message.setTo(mailInfo.getTo());
if (mailInfo.getBcc() != null) {
message.setBcc(mailInfo.getBcc());
}
if (mailInfo.getCc() != null) {
message.setCc(mailInfo.getCc());
}
message.setSubject(mailInfo.getSubject());
message.setText(mailInfo.getText());
mailSender.send(message);
} catch (Exception e) {
log.error("邮件发送失败,内容:" + JSON.toJSONString(mailInfo) + ";\r\n 异常信息:" + e.toString());
}
}
/**
* 发送邮件
*/
public void sendMail(MailInfo mailInfo) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
if (!StringUtils.isEmpty(mailInfo.getFrom())) {
helper.setFrom(mailInfo.getFrom());
} else {
helper.setFrom(appMailConfig.getDefaultFrom());
}
helper.setTo(mailInfo.getTo());
if (mailInfo.getBcc() != null) {
helper.setBcc(mailInfo.getBcc());
}
if (mailInfo.getCc() != null) {
helper.setCc(mailInfo.getCc());
}
helper.setSubject(mailInfo.getSubject());
helper.setText(mailInfo.getText(), mailInfo.getIsHtml());
setAttachment(mailInfo, helper);
mailSender.send(message);
} catch (Exception e) {
log.error("邮件发送失败,内容:" + JSON.toJSONString(mailInfo) + ";\r\n 异常信息:" + e.toString());
}
}
private void setAttachment(MailInfo mailInfo, MimeMessageHelper helper) {
try {
if (mailInfo.getAttachments() != null && mailInfo.getAttachments().size() > 0) {
for (MailAttachment att : mailInfo.getAttachments()) {
if (StringUtils.isEmpty(att.getPath()) && StringUtils.isEmpty(att.getUrl())) {
continue;
}
if (!StringUtils.isEmpty(att.getUrl())) {
String[] name = att.getUrl().split("/");
URL url = new URL(att.getUrl());
DataSource source = new URLDataSource(url);
if (StringUtils.isEmpty(att.getName())) {
helper.addAttachment(name[name.length - 1], source);
} else {
helper.addAttachment(att.getName(), source);
}
continue;
}
if (!StringUtils.isEmpty(att.getPath())) {
FileSystemResource file = new FileSystemResource(new File(att.getPath()));
String fileName = att.getPath().substring(att.getPath().lastIndexOf(File.separator));
if (StringUtils.isEmpty(att.getName())) {
helper.addAttachment(fileName, file);
} else {
helper.addAttachment(att.getName(), file);
}
}
}
}
} catch (Exception e) {
throw new VlihubException(e);
}
}
}
4、测试发送功能。
@Resource
private MailHelper mailHelper;
@Override
public BaseResult<String> sendMailTest() {
MailInfo mailInfo = new MailInfo();
List<MailAttachment> attachments = new ArrayList<>();
MailAttachment attachment1 = new MailAttachment();
attachment1.setName("PDF模板制作.png");
attachment1.setUrl("https://file.mainki.com/2021-01-19/1611063778634.png");
attachments.add(attachment1);
mailInfo.setIsHtml(false);
mailInfo.setTo(new String[]{"weilikoo@qq.com", "weili@giveu.cn"});
mailInfo.setSubject("迈极集团-研发邮件");
mailInfo.setText("请注意项目进度,注意代码质量,注意附件内容!");
mailInfo.setAttachments(attachments);
mailHelper.sendMail(mailInfo);
return BaseResult.success();
}
到这里就完成了邮件发送的开发。
vli
6 小时前