1. 程式人生 > 其它 >Java - Email 傳送郵件

Java - Email 傳送郵件

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;
}