消息推送之发送邮件

最近项目做一个功能是关于消息推送(站内信、短信、微信、邮件)。现在做的是邮件,我只是用了JavaMail,但是下面的博主写的更加的好,就转载了。

原文:https://blog.csdn.net/qq_32371887/article/details/72821291

要发送邮件就要用到java中的JavaMail,关于JavaMailAPI的详解呢在 
( http://blog.csdn.net/imain/article/details/1453677“)中有非常详尽的介绍,我就直接上代码了。 
1:使用JavaMail发送邮件
 // 1.创建一个程序与邮件服务器会话对象 Session
 Properties props = new Properties();
 props.setProperty("mail.transport.protocol", "SMTP");
 props.setProperty("mail.smtp.host", "smtp.163.com");
 props.setProperty("mail.smtp.port", "25");
 // 指定验证为true
 props.setProperty("mail.smtp.auth", "true");
 props.setProperty("mail.smtp.timeout","1000");
 // 验证账号及密码,密码需要是第三方授权码
 Authenticator auth = new Authenticator() {
 public PasswordAuthentication getPasswordAuthentication({
    return new PasswordAuthentication("*******@163.com", "*******");
            }
        };
 Session session = Session.getInstance(props, auth);

// 2.创建一个Message,它相当于是邮件内容
Message message = new MimeMessage(session);
// 设置发送者
message.setFrom(new InternetAddress("*******@163.com"));
// 设置发送方式与接收者
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));
// 设置主题
message.setSubject("邮件发送测试");
// 设置内容
message.setContent(emailMsg, "text/html;charset=utf-8");

// 3.创建 Transport用于将邮件发送
Transport.send(message);

2:我用的是spring框架,spring 封装了一个简单易用的关于邮件发送的工具类JavaMailSenderImpl ,所以可以用JavaMailSenderImpl 来实现邮件发送。

public class MailService {
    private static final String HOST = "smtp.163.com";
    private static final Integer PORT = 25;
    private static final String USERNAME = "*******@163.com";
    private static final String PASSWORD = "*******";
    private static final String EMAILFORM = "*******@163.com";
    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", "25000");
        p.setProperty("mail.smtp.auth", "false");
        sender.setJavaMailProperties(p);
        return sender;
    }

    /**
     * 发送邮件
     *
     * @param to 接受人
     * @param subject 主题
     * @param html 发送内容
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static void sendHtmlMail(String to, String subject, String html) throws MessagingException,UnsupportedEncodingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码,否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(EMAILFORM, "系统名称");
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(html, true);
        mailSender.send(mimeMessage);
    }
}

当然这种是硬编码的方式了。也可以写在资源文件中。

资源文件

mailConfig.properties
#服务器
mailHost=smtp.163.com
#端口号
mailPort=25
#邮箱账号
mailUsername=*******@163.com
#邮箱授权码
mailPassword=*******
#时间延迟
mailTimeout=25000
#发送人
mailFrom=*******@163.com

获得资源文件内容

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 Properties properties;
    static{
        init();
    }

    /**
     * 初始化
     */
    private static void init() {
        properties = new Properties();
        try{
            InputStream inputStream = MailConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
            properties.load(inputStream);
            inputStream.close();
            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 = "墨裔";
        } catch(IOException e){
            e.printStackTrace();
        }
    }
}

发送邮件

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 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", "false");
        sender.setJavaMailProperties(p);
        return sender;
    }

    /**
     * 发送邮件
     *
     * @param to 接受人
     * @param subject 主题
     * @param html 发送内容
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static void sendMail(String to, String subject, String html) 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.setText(html, true);
        mailSender.send(mimeMessage);
    }
}

源代码已经上传,可以下载 
https://github.com/wolf521/demo/tree/master/src/main/java/com/example/demo/mail

jar包下载 
http://download.csdn.net/download/qq_32371887/10123280

猜你喜欢

转载自blog.csdn.net/howinfun/article/details/80737366