利用spring-mail模块发送带附件邮件dome

本例为maven项目,直接撸代码吧。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>com.wengen</groupId>
<artifactId>mailTest</artifactId>
<packaging>war</packaging>
<version>0.0.-SNAPSHOT</version>
<name>mailTest Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<spring.amqp.version>1.2..RELEASE</spring.amqp.version>
<spring.version>4.2..RELEASE</spring.version>
<spring-core.version>4.2..RELEASE</spring-core.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.</version>
<scope>test</scope>
</dependency> <!-- spring mail --> <dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency> <dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency> <!-- Spring Config -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>${spring.amqp.version}</version>
</dependency> </dependencies>
<build>
<finalName>mailTest</finalName>
</build>
</project>

MailMail.java

package mailTest;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.MessagingException; import javax.mail.internet.MimeMessage; public class MailMail {
private JavaMailSender mailSender;
private SimpleMailMessage simpleMailMessage; public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
} public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
} public void sendMail(String dear, String content) { MimeMessage message = mailSender.createMimeMessage(); try{
MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(simpleMailMessage.getFrom());
helper.setTo(simpleMailMessage.getTo());
helper.setSubject(simpleMailMessage.getSubject());
helper.setText(String.format(
simpleMailMessage.getText(), dear, content));
//使用本地log.txt作为附件
FileSystemResource file = new FileSystemResource("D:\\log.txt");
helper.addAttachment(file.getFilename(), file); }catch (Exception e) {
throw new MailParseException(e);
}
mailSender.send(message);
}
}

Spring-Mail.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- 邮件发送传输协议 -->
<property name="protocol" value="smtp" />
<!-- 邮件发送服务器,本例使用QQ企业邮箱 -->
<property name="host" value="smtp.exmail.qq.com" />
<!-- 邮件发送端口 -->
<property name="port" value="" />
<!-- 登陆企业邮箱的用户名,用户自己注册 -->
<property name="username" value="user@qq.com" />
<!-- 登陆企业邮箱的密码,用户注册时候设置密码,本例用户名和密码都是假的 -->
<property name="password" value="password" /> <property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean> <bean id="mailMail" class="mailTest.MailMail">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean> <bean id="customeMailMessage"
class="org.springframework.mail.SimpleMailMessage">
<!-- 发送邮件地址 -->
<property name="from" value="from@qq.com" />
<!-- 接收邮件地址 -->
<property name="to" value="to@qq.com" />
<!-- 邮件主题 -->
<property name="subject" value="Testing Subject" />
<!-- 发送内容 -->
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean> </beans>

MailTest.java

package mailTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MailTest {
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:/Spring-Mail.xml"); MailMail mm = (MailMail) context.getBean("mailMail");
//发送时候写好主题和内容
mm.sendMail("Yiibai", "This is text content"); }
}

非常简单。注意Spring-Mail.xml里面需要配置的地方。还有可能出现包冲突异常:

利用spring-mail模块发送带附件邮件dome

此异常咱没有解决,所以新搞了个项目,实现本例。

上一篇:jenkins发送jemter邮件附件格式配置


下一篇:mybatis循环、mybatis传map