MailUtils 发送邮件Email 工具类
package org.jeecg.modules.system.util; import org.jeecg.modules.system.entity.GaiaEmailNotifyConfig; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Properties; public class MailUtils { public static void sendEmail(GaiaEmailNotifyConfig emailNotify) throws MessagingException, UnsupportedEncodingException { Properties prop = new Properties(); prop.put("mail.smtp.auth", true); prop.put("mail.smtp.starttls.enable", emailNotify.getStarttls()==1?"true":"false" ); prop.put("mail.smtp.host", emailNotify.getHost()); prop.put("mail.smtp.port", emailNotify.getPort()); prop.put("mail.smtp.ssl.trust", emailNotify.getHost()); Session session = Session.getInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(emailNotify.getFromMail(), emailNotify.getFromPassword()); } }); String from=""; try { from= MimeUtility.encodeText(emailNotify.getFromUsername()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } from = from+ "<"+emailNotify.getFromAddress()+">"; Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailNotify.getToMail())); if(emailNotify.getCcMail() != null){ message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(emailNotify.getCcMail())); } if(emailNotify.getBccMail()!=null){ message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(emailNotify.getBccMail())); } System.getProperties().setProperty("mail.mime.splitlongparameters","false"); message.setSubject(emailNotify.getMailSubject()); message.setHeader("Content-Type","text/html; charset=UTF-8"); //添加邮件正文 MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(emailNotify.getMailMessage(), "text/html; charset=UTF-8"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); //添加附件 if(emailNotify.getAttachment()!=null){ for (String fileName: emailNotify.getAttachment()) { File file = new File(fileName); BodyPart attachmentBodyPart = new MimeBodyPart(); DataSource ds=new FileDataSource(file); attachmentBodyPart.setDataHandler(new DataHandler(ds)); //MimeUtility.encodeWord可以避免文件名乱码 attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName(),"UTF-8","B")); multipart.addBodyPart(attachmentBodyPart); } } message.setContent(multipart); Transport.send(message); } }
实体参数
package org.jeecg.modules.system.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; import org.jeecgframework.poi.excel.annotation.Excel; import org.springframework.format.annotation.DateTimeFormat; import java.io.File; import java.io.Serializable; import java.util.Date; import java.util.List; /** * @Description: 邮件通知配置 * @Author: jeecg-boot * @Date: 2021-03-10 * @Version: V1.0 */ @Data @Accessors(chain = true) @EqualsAndHashCode(callSuper = false) @ApiModel(value="email_config对象", description="邮件通知配置") public class GaiaEmailNotifyConfig implements Serializable { private static final long serialVersionUID = 1L; /**主键*/ @TableId(type = IdType.ID_WORKER_STR) @ApiModelProperty(value = "主键") private String id; /**创建人*/ @ApiModelProperty(value = "创建人") private String createBy; /**创建日期*/ @JsonFormat(timezone = "GMT+9",pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "创建日期") private Date createTime; /**更新人*/ @ApiModelProperty(value = "更新人") private String updateBy; /**更新日期*/ @JsonFormat(timezone = "GMT+9",pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "更新日期") private Date updateTime; /**所属部门*/ @ApiModelProperty(value = "所属部门") private String sysOrgCode; /**租户ID*/ @Excel(name = "租户ID", width = 15) @ApiModelProperty(value = "租户ID") private String tenantId; /**逻辑删除*/ @Excel(name = "逻辑删除", width = 15) @ApiModelProperty(value = "逻辑删除") @TableLogic private Integer delFlag; /**邮件服务器地址*/ @Excel(name = "邮件服务器地址", width = 15) @ApiModelProperty(value = "邮件服务器地址") private String host; /**邮件服务器端口*/ @Excel(name = "邮件服务器端口", width = 15) @ApiModelProperty(value = "邮件服务器端口") private Integer port; /**发信人邮箱*/ @Excel(name = "发信人邮箱", width = 15) @ApiModelProperty(value = "发信人邮箱") private String fromMail; /**发信人显示邮箱*/ @Excel(name = "发信人显示邮箱", width = 15) @ApiModelProperty(value = "发信人显示邮箱") private String fromAddress; /**发信人名称*/ @Excel(name = "发信人名称", width = 15) @ApiModelProperty(value = "发信人名称") private String fromUsername; /**发信人密码*/ @Excel(name = "发信人密码", width = 15) @ApiModelProperty(value = "发信人密码") private String fromPassword; /**邮件类型*/ @Excel(name = "邮件类型", width = 15) @ApiModelProperty(value = "邮件类型") private String mailType; /**邮件标题*/ @Excel(name = "邮件标题", width = 15) @ApiModelProperty(value = "邮件标题") private String mailSubject; /**邮件内容*/ @Excel(name = "邮件内容", width = 15) @ApiModelProperty(value = "邮件内容") private String mailMessage; /**收信人邮箱列表(逗号分隔)*/ @Excel(name = "收信人邮箱列表(逗号分隔)", width = 15) @ApiModelProperty(value = "收信人邮箱列表(逗号分隔)") private String toMail; /**抄送人邮箱列表(逗号分隔)*/ @Excel(name = "抄送人邮箱列表(逗号分隔)", width = 15) @ApiModelProperty(value = "抄送人邮箱列表(逗号分隔)") private String ccMail; /**密送人邮箱列表(逗号分隔)*/ @Excel(name = "密送人邮箱列表(逗号分隔)", width = 15) @ApiModelProperty(value = "密送人邮箱列表(逗号分隔)") private String bccMail; /**是否启用*/ @Excel(name = "是否启用", width = 15) @ApiModelProperty(value = "是否启用") private String validates; /**SSL加密*/ @Excel(name = "SSL加密", width = 15) @ApiModelProperty(value = "SSL加密") private Integer starttls; /**文件路径*/ @Excel(name = "文件路径", width = 15) @ApiModelProperty(value = "文件路径") private List<String> attachment; }