maven项目之发送邮件

用idea使用maven开发javaMail并测试

github项目地址
项目总体结构:

maven项目之发送邮件_第1张图片
1.png

** 1. 异常信息类:AccountEmailException.java**

package com.juvenxu.mvnbook.account.email;

public class AccountEmailException
    extends Exception
{
    private static final long serialVersionUID = -4817386460334501672L;

    public AccountEmailException( String message )
    {
        super( message );
    }

    public AccountEmailException( String message, Throwable throwable )
    {
        super( message, throwable );
    }
}

** 2.发送邮件类的接口 **

package com.juvenxu.mvnbook.account.email;

public interface AccountEmailService
{
    void sendMail( String to, String subject, String htmlText )
        throws AccountEmailException;
}

** 3. 发送邮件的接口类AccountEmailServiceImpl.java**

package com.juvenxu.mvnbook.account.email;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class AccountEmailServiceImpl
        implements AccountEmailService {
    private JavaMailSender javaMailSender;

    private String systemEmail;

    public void sendMail(String to, String subject, String htmlText)
            throws AccountEmailException {
        try {
            MimeMessage msg = javaMailSender.createMimeMessage();
            MimeMessageHelper msgHelper = new MimeMessageHelper(msg);

            msgHelper.setFrom(systemEmail);
            msgHelper.setTo(to);
            msgHelper.setSubject(subject);
            msgHelper.setText(htmlText, true);

            javaMailSender.send(msg);
        } catch (MessagingException e) {
            throw new AccountEmailException("Faild to send mail.", e);
        }
    }

    public JavaMailSender getJavaMailSender() {
        return javaMailSender;
    }

    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    public String getSystemEmail() {
        return systemEmail;
    }

    public void setSystemEmail(String systemEmail) {
        this.systemEmail = systemEmail;
    }
}

** spring的配置文件,配置AccountEmailService的信息,使用了spring的javaMailSender **
account-email.xml




    
        
    

    
        
        
        
        
        
        
            
                ${email.auth}
            
        
    

    
        
        
    


service.properties配置文件,配置发送邮件的服务器:用的时候把email.username,email.password,email.systemEmail换成自己的:

email.protocol=smtp
email.host=smtp.163.com
email.port=25
[email protected]
email.password=//这里填邮箱密码
email.auth=true
email.systemEmail=//这里填邮箱名称

** 测试**

package com.juvenxu.mvnbook.account.email;

import static junit.framework.Assert.assertEquals;

import javax.mail.Message;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetup;

public class AccountEmailServiceTest
{
    private GreenMail greenMail;

    @Before
    public void startMailServer()
        throws Exception
    {
        greenMail = new GreenMail( ServerSetup.SMTP );
        greenMail.setUser( "[email protected]", "chen188290wei" );
        greenMail.start();
    }

    @Test
    public void testSendMail()
        throws Exception
    {
        ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-email.xml" );
        AccountEmailService accountEmailService = (AccountEmailService) ctx.getBean( "accountEmailService" );

        String subject = "***同学";
        String htmlText = "

I Love You !!!

"; accountEmailService.sendMail( "[email protected]", subject, htmlText ); greenMail.waitForIncomingEmail( 2000, 1 ); Message[] msgs = greenMail.getReceivedMessages(); assertEquals( 0, msgs.length ); assertEquals( subject, msgs[0].getSubject() ); assertEquals( htmlText, GreenMailUtil.getBody( msgs[0] ).trim() ); } @After public void stopMailServer() throws Exception { greenMail.stop(); } }

//运行 mvn test

你可能感兴趣的:(maven项目之发送邮件)