适用版本:v3.3.6+;

dependency

<dependency>
   <groupId>com.lc.ibps.components</groupId>
   <artifactId>ibps-component-mail</artifactId>
</dependency>

配置说明

配置文件在application-common.yml,参考如下:

##---------邮箱配置---------
mail:
  notify-to: ${MAIL_NOTIFY_TO:}
  from-address: ${MAIL_FROM_ADDRESS:}
  host: ${MAIL_HOST:smtp.163.com}
  mail-address: ${MAIL_ADDRESS:}
  username: ${MAIL_USERNAME:}
  password: ${MAIL_PASSWORD:}
  ## 端口号 如果启用 163用465/994
  port: ${MAIL_PORT:465}
  default-encoding: utf-8
  ## 尝试使用AUTH命令认证用户
  smtp:
    auth: true
    ssl:
      enable: true
    starttls:
      enable: false
  ## 服务协议
  transport:
    protocol: ${MAIL_TRANSPORT_PROTOCOL:smtps}  

API说明

  • MailUtil

    // 连接Smtp和接收器
    public void connectSmtpAndReceiver()
    
    // 发送邮件
    public void send(Mail mail)
    
    // 接收邮件
    public List<Mail> receive(AttacheHandler handler)
    public List<Mail> receive(AttacheHandler handler, String lastHandleMessageId)
    
    // 根据messageID获取邮件
    public Mail getByMessageID(AttacheHandler handler, String messageID)

使用示例

public class MailUtilTest  {

    @Test
    public  void receive() throws Exception{
        MailSetting setting = getMailSettingEntity();

        MailUtil mailUtil = new MailUtil(setting);
        final List<String> finalList = new ArrayList<String>();
        String latestEmailId = "";
        List<Mail> list = mailUtil.receive(new AttacheHandler() {
            @Override
            public Boolean isDownlad(String UID) {
                return !finalList.contains(UID);
            }

            @Override
            public void handle(Part part, Mail mail) {
                try {
                    //TODO
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, latestEmailId);
        System.out.println(list);
    }

    private MailSetting getMailSettingEntity() {
        MailSetting setting = new MailSetting();
        setting.setMailAddress("test@163.com");
        setting.setPassword("1234567");
        setting.setProtocal(MailProtocol.POP3.getName());
        setting.setReceiveHost("pop.163.com");
        setting.setReceivePort("110");
        return setting;
    }
}
文档更新时间: 2020-12-31 17:25   作者:Eddy