net.bafeimao.umbrella.web.test.MailTests.java Source code

Java tutorial

Introduction

Here is the source code for net.bafeimao.umbrella.web.test.MailTests.java

Source

/*
 * Copyright 2002-2015 by bafeimao.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.bafeimao.umbrella.web.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Properties;

/**
 * Created by ktgu on 15/6/27.
 */
public class MailTests {
    private String imagePath = "/Users/ktgu/Pictures/mmexport1434293832573.jpg";
    private JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

    @Before
    public void before() {
        senderImpl.setHost("smtp.qq.com");
        senderImpl.setUsername("29283212"); // ?,username
        senderImpl.setPassword("QQ*!!***32"); // ?, password

        Properties prop = new Properties();
        prop.put("mail.smtp.auth", "true"); // ?true??,?????
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);
    }

    /**
     * SpringMailSender???
     */
    @Test
    public void testSendMail() {
        // ?
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        //  ??
        // String[] array = new String[] {"sun111@163.com","sun222@sohu.com"};
        // mailMessage.setTo(array);
        mailMessage.setTo(" 29283212@qq.com");
        mailMessage.setFrom("29283212@qq.com");
        mailMessage.setSubject("???!");
        mailMessage.setText(" ????");

        // ??
        senderImpl.send(mailMessage);

        System.out.println(" ???.. ");
    }

    @Test
    public void testSendHtmlMail() throws Exception {
        // ?,???html
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);

        // 
        messageHelper.setTo("29283212@qq.com");
        messageHelper.setFrom("29283212@qq.com");
        messageHelper.setSubject("HTML?");
        // true ?HTML?
        messageHelper.setText("<html><head></head><body><h1>hello!!spring html Mail</h1></body></html>", true);

        // ??
        senderImpl.send(mailMessage);

        System.out.println("???..");
    }

    @Test
    public void testSendMailWithInlineImage() throws MessagingException {
        // ?,???html
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        // ?boolean,?MimeMessageHelpertrue?
        // multipart?
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);

        // 
        messageHelper.setTo("29283212@qq.com");
        messageHelper.setFrom("29283212@qq.com");
        messageHelper.setSubject("!?");
        // true ?HTML?
        messageHelper.setText("<html><head></head><body><h1>hello!!spring image html mail</h1>"
                + "<img src=\"cid:aaa\"/></body></html>", true);

        FileSystemResource img = new FileSystemResource(new File(imagePath));

        messageHelper.addInline("aaa", img);

        // ??
        senderImpl.send(mailMessage);

        System.out.println("???..");
    }

    @Test
    public void testSendMailWithAttachedImage() throws MessagingException {
        // ?,???html
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        // ?boolean,?MimeMessageHelpertrue?
        // multipart? true?? ?html?
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");

        // 
        messageHelper.setTo("29283212@qq.com");
        messageHelper.setFrom("29283212@qq.com");
        messageHelper.setSubject("!?");
        // true ?HTML?
        messageHelper.setText(
                "<html><head></head><body><h1>?</h1></body></html>", true);

        FileSystemResource file = new FileSystemResource(new File(imagePath));
        // ???
        messageHelper.addAttachment("image", file);

        // ??
        senderImpl.send(mailMessage);

        System.out.println("???..");
    }
}