zoukankan      html  css  js  c++  java
  • spring 发送email

    此篇非原创,代码忘了从哪篇帖子下学习的了,时间太久了,写这篇主要是为了存一下

    package com.cn.test.mailConfig;
    import java.security.GeneralSecurityException;  
    import java.util.Properties;  
      
    import javax.mail.MessagingException;  
    import javax.mail.internet.MimeMessage;  
      
    import org.springframework.core.io.FileSystemResource;  
    import org.springframework.mail.SimpleMailMessage;  
    import org.springframework.mail.javamail.JavaMailSenderImpl;  
    import org.springframework.mail.javamail.MimeMessageHelper;  
      
    import com.sun.mail.util.MailSSLSocketFactory;  
    public class Mail {
    public static void sendSimpleMail() throws GeneralSecurityException{  
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();    
            senderImpl.setHost("smtp.163.com");    
            senderImpl.setUsername("**@163.com");  
            senderImpl.setPassword("**");  
            Properties prop = new Properties();    
            MailSSLSocketFactory sf = new MailSSLSocketFactory();  
            sf.setTrustAllHosts(true);  
            prop.put("mail.smtp.ssl.enable", "true");  
            prop.put("mail.smtp.ssl.socketFactory", sf);  
            prop.put("mail.smtp.auth", "true");  
            prop.put("mail.smtp.timeout", "20000");  
            senderImpl.setJavaMailProperties(prop);  
            SimpleMailMessage mail = new SimpleMailMessage();    
            mail.setFrom("**@163.com");  
            mail.setTo("**@163.com");  
            mail.setSubject("测试标题");  
            mail.setText("测试");  
            senderImpl.send(mail);  
            System.out.println("SIMPLEMAIL SENDED");  
        }  
        public static void sendMimeMail() throws GeneralSecurityException, MessagingException{  
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  
            senderImpl.setHost("smtp.163.com");    
            senderImpl.setUsername("**@163.com");  
            senderImpl.setPassword("**"); 
            Properties prop = new Properties();  
            MailSSLSocketFactory sf = new MailSSLSocketFactory();  
            sf.setTrustAllHosts(true);  
            prop.put("mail.smtp.ssl.enable", "true");  
            prop.put("mail.smtp.ssl.socketFactory", sf);  
            prop.put("mail.smtp.auth", "true");  
            prop.put("mail.smtp.timeout", "20000");   
            senderImpl.setJavaMailProperties(prop);  
            MimeMessage mail = senderImpl.createMimeMessage();  
            MimeMessageHelper helper = new MimeMessageHelper(mail,true);  
            helper.setFrom("**@163.com");  
            helper.setTo("**@163.com");  
            helper.setSubject("测试标题MIME");  
            helper.setText("测试MIME");  
            FileSystemResource mailImage = new FileSystemResource("src/main/webapp/files/images/0.jpg");  
            helper.addAttachment("mail.png", mailImage);  
            senderImpl.send(mail);  
            System.out.println("MIMEMAIL SENDED");  
        }  
        public static void sendHTMLMail() throws GeneralSecurityException, MessagingException{  
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  
            senderImpl.setHost("smtp.163.com");    
            senderImpl.setUsername("**@163.com");  
            senderImpl.setPassword("**");
            senderImpl.setDefaultEncoding("UTF-8");  
            Properties prop = new Properties();  
            MailSSLSocketFactory sf = new MailSSLSocketFactory();  
            sf.setTrustAllHosts(true);  
            prop.put("mail.smtp.ssl.enable", "true");  
            prop.put("mail.smtp.ssl.socketFactory", sf);  
            prop.put("mail.smtp.auth", "true");  
            prop.put("mail.smtp.timeout", "20000");   
            senderImpl.setJavaMailProperties(prop);  
            MimeMessage mail = senderImpl.createMimeMessage();  
            MimeMessageHelper helper = new MimeMessageHelper(mail,true);  
            helper.setFrom("**@163.com");  
            helper.setTo("**@163.com");  
            helper.setSubject("测试标题HTML");  
            StringBuilder html = new StringBuilder();  
            html.append("<html>")  
                .append("<body>")  
                .append("<h2>Goser,你好</h2>")  
                .append("<p>这是一个测试。</p>")  
                .append("<img src='cid:mailImage'/>")  
                .append("<p>THANKS</p>")  
                .append("</body>")  
                .append("</html>");  
            helper.setText(html.toString(),true);  
            FileSystemResource mailImage = new FileSystemResource("src/main/webapp/files/images/0.jpg");  
            helper.addInline("mailImage", mailImage);  
            senderImpl.send(mail);  
            System.out.println("HTMLMAIL SENDED");  
        }  
        public static void main(String[] args) throws GeneralSecurityException, MessagingException{  
            //sendHTMLMail();  
            //sendSimpleMail();  
            sendMimeMail();  
        }  
    }

    <dependency>
             <groupId>javax.mail</groupId>
             <artifactId>mail</artifactId>
             <version>1.4.7</version>
        </dependency>

  • 相关阅读:
    多线程
    ERP概念介绍
    Servlet生命周期
    springmvc工作流程
    spring事务管理的接口
    解决主从备份Slave_SQL_Running:No
    实现Mysql主从备份
    springboot集成mybatis进行开发
    SpringBoot 入门第一章
    Hibernate 关联关系映射
  • 原文地址:https://www.cnblogs.com/leavesss/p/9955433.html
Copyright © 2011-2022 走看看