| | |
| | | import org.springframework.mail.javamail.JavaMailSenderImpl; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.mail.*; |
| | | import java.security.Security; |
| | | import java.util.List; |
| | | import java.util.Properties; |
| | | |
| | | @Slf4j |
| | | @Component |
| | |
| | | //获取数据库中启用的邮件配置 |
| | | EmailEntity params = new EmailEntity(); |
| | | params.setStatus(2); |
| | | params.setEmailType("1"); |
| | | |
| | | EmailEntity email = emailMapper.selectOne(Condition.getQueryWrapper(params)); |
| | | JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @return |
| | | * @throws MessagingException |
| | | */ |
| | | public Store buildMailReceive() throws MessagingException { |
| | | log.info("初始化mailReceive"); |
| | | |
| | | //获取数据库中启用的邮件配置 |
| | | EmailEntity params = new EmailEntity(); |
| | | params.setStatus(2); |
| | | params.setEmailType("2"); |
| | | |
| | | EmailEntity email = emailMapper.selectOne(Condition.getQueryWrapper(params)); |
| | | |
| | | Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); |
| | | final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";//ssl加密 |
| | | |
| | | //有些参数可能不需要 |
| | | Properties props = new Properties(); |
| | | props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY); |
| | | props.setProperty("mail.imap.socketFactory.fallback", "false"); |
| | | props.setProperty("mail.transport.protocol", email.getProtocol()); // 使用的协议 |
| | | props.setProperty("mail.imap.port", email.getPort().toString()); |
| | | props.setProperty("mail.imap.socketFactory.port", email.getPort().toString()); |
| | | |
| | | //创建会话 |
| | | Session session = Session.getInstance(props, new Authenticator() { |
| | | @Override |
| | | protected PasswordAuthentication getPasswordAuthentication() { |
| | | return new PasswordAuthentication(email.getUsername(),email.getPassword()); |
| | | } |
| | | }); |
| | | //存储对象 |
| | | Store store = session.getStore("imap"); |
| | | //连接 |
| | | store.connect("imap.qq.com",email.getPort(),email.getUsername(),email.getPassword()); |
| | | return store; |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 获取MailSender |
| | | * |
| | | * @return CustomMailSender |
| | |
| | | public JavaMailSenderImpl getSender() { |
| | | return buildMailSender(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取MailSender |
| | | * |
| | | * @return CustomMailSender |
| | | */ |
| | | public Store getReceive() throws MessagingException { |
| | | return buildMailReceive(); |
| | | } |
| | | } |