java发送邮件-CSDN博客

package cn.hzh.demo.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class MailConfig {
    private static final String PROPERTIES_DEFAULT = "mailConfig.properties";
    public static String host;
    public static Integer port;
    public static String userName;
    public static String passWord;
    public static String emailForm;
    public static String timeout;
    public static String personal;
    public static String html;
    public static String subject;
    public static Properties properties;

    static{
        init();
    }

    /**
     * 初始化
     */
    private static void init() {
        properties = new Properties();
        try{
            //InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
            InputStream inputStream = MailConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
            //properties.load(inputStream);
            //inputStream.close();
            //解决中文乱码,采取reader把inputStream转换成reader用字符流来读取中文
            BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "GBK"));
            properties.load(bf);
            host = properties.getProperty("mailHost");
            port = Integer.parseInt(properties.getProperty("mailPort"));
            userName = properties.getProperty("mailUsername");
            passWord = properties.getProperty("mailPassword");
            emailForm = properties.getProperty("mailFrom");
            timeout = properties.getProperty("mailTimeout");
            personal = properties.getProperty("personal");
            //System.out.println("personal:"+personal);
            html = properties.getProperty("html");
            subject = properties.getProperty("subject");
            //System.out.println("subject:"+subject);
        } catch(IOException e){
            e.printStackTrace();
        }
    }
}
package cn.hzh.demo.utils;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailUtil {
    private static final String HOST = MailConfig.host;
    private static final Integer PORT = MailConfig.port;
    private static final String USERNAME = MailConfig.userName;
    private static final String PASSWORD = MailConfig.passWord;
    private static final String emailForm = MailConfig.emailForm;
    private static final String timeout = MailConfig.timeout;
    private static final String personal = MailConfig.personal;
    private static final String subject = MailConfig.subject;
    private static final String html = MailConfig.html;
    private static JavaMailSenderImpl mailSender = createMailSender();

    /**
     * 邮件发送器
     *
     * @return 配置好的工具
     */
    private static JavaMailSenderImpl createMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(HOST);
        sender.setPort(PORT);
        sender.setUsername(USERNAME);
        sender.setPassword(PASSWORD);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", timeout);
        p.setProperty("mail.smtp.auth", "true");
        sender.setJavaMailProperties(p);
        return sender;
    }

    //
    // 发送邮件
    //
    // @param to 接受人
    // @param subject 主题
    // @param html 发送内容
    // @throws MessagingException 异常
    // @throws UnsupportedEncodingException 异常
    // (15000 - 7500 = 7500 - 3000 = 4500 * 12 = 54000 + 60000 = 114000 + (10000 - 3000 = 7000 * 12 = 84000)) 200k

    public static void sendMail(String to) throws MessagingException,UnsupportedEncodingException{
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码,否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(emailForm,personal);
        messageHelper.setTo(to);
        //messageHelper.setSubject(subject);
        messageHelper.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B"));
        messageHelper.setText(html, true);
//      messageHelper.addAttachment("", new File(""));//附件
        mailSender.send(mimeMessage);

    }
}

#服务器
mailHost=smtp.163.com
#端口号
mailPort=25
#邮箱账号
[email protected]
#邮箱授权码
mailPassword=xxx
#时间延迟
mailTimeout=25000
#发送人
[email protected]
#发件人
personal=豪哥
#主题
subject=早上好
#内容模板
html=您的邮箱验证码为:1688

你可能感兴趣的:(功能实现)