spring mail 邮件发送 附带 昵称

说明,本人使用maven构建项目.在网上很少有可以直接按照步骤就可以构建出来可运行的程序. 本人亲测整理如下:

1. maven依赖:

A.  mail/spring/activation/logging

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

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

2. spring: applicationContext-mail.xml配置
<bean id ="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
    <property name="host">
        <value>smtp.163.com</value>
    </property>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.timeout">25000</prop>
        </props>
    </property>
    <property name="username">
        <value>[email protected]</value>
    </property>
    <property name="password">
        <value></value>
    </property>
</bean>

3. 我是在Banner实体中测试的,代码如下:
public ApplicationContext context = null;
public Banner(){
    this.context = new ClassPathXmlApplicationContext("spring/applicationContext-mail.xml");
}

public static void main(String[] args) {
    Banner banner = new Banner();
    JavaMailSender mailSender = (JavaMailSender) banner.context.getBean("mailSender");
    JavaMailSenderImpl mail = new JavaMailSenderImpl();
    MimeMessage mimeMessage = mail.createMimeMessage();
    try {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setTo("[email protected]");
        String nick = javax.mail.internet.MimeUtility.encodeText("哔哩哔哩-旅游");
        mimeMessageHelper.setFrom(new InternetAddress(nick + "<[email protected]>"));
        mimeMessageHelper.setSubject("测试邮件主题");
        mimeMessageHelper.setText("测试邮件内容");
        mailSender.send(mimeMessage);
        System.out.println(" success ... ");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

你可能感兴趣的:(java,邮件)