参考了一些别人的代码,自己写了一下,希望以后发方便查找
针对发送邮件,我写了两个接口:一个是不带附件的,一个是带有附件的
pom.xml
其中有一些包暂时用不到,这是这个系列所用到的包,其中的greenmail包可以不用,我没有上传测试的代码.
下载这些包我有两个网址推荐:http://search.maven.org/ (我更喜欢这个) https://mvnrepository.com
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"2.
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.acount</groupId>
<artifactId>email</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>email Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b07</version>
</dependency>
<!-- <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId>
<version>1.7.25</version> </dependency> -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
</dependencies>
<build>
<!-- 不打包mail.properties -->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>mail.properties</exclude>
</excludes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<!-- 不打包依赖的jar,把依赖的jar copy到lib目录,和生成的jar放在同一级目录下 -->
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.test.acount.email.AcountEmailServiceImpl</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
package com.test.acount.email;public class AcountEmailException extends Exception {/** * */private static final long serialVersionUID = -2203542477608000360L;public AcountEmailException(){ super(); } public AcountEmailException(String msg){ super(msg); } public AcountEmailException(String msg,Throwable e){ super(msg, e); }}
3
package com.test.acount.email;
public interface AcountEmailService {
void sendEmail(String to,String subject,String htmlText) throws AcountEmailException;
public void sendEmail(String to, String subject, String htmlText,
String path) throws AcountEmailException;
}
4.MimeMessageHelper msgHelper = new MimeMessageHelper(msg,"UTF-8");这样就不会出现乱码问题,有需要可以专门看一下源码
package com.test.acount.email;
import java.io.File;
import javax.mail.internet.MimeMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
//import com.sprucetec.rdw.api.exception.AcountEmailException;
public class AcountEmailServiceImpl implements AcountEmailService{
private static Log log=LogFactory.getLog(AcountEmailServiceImpl.class);
private JavaMailSender javaMailSender;
private String systemEmail;
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;
}
@Override
public void sendEmail(String to, String subject, String htmlText)
throws AcountEmailException {
try{
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper msgHelper = new MimeMessageHelper(msg);
msgHelper.setFrom(systemEmail);
System.out.println(systemEmail);
msgHelper.setTo(to);
msgHelper.setSubject(subject);
msgHelper.setText(htmlText, true);
javaMailSender.send(msg);
log.info("邮件发送成功");
}catch(Exception e){
log.error("邮件发送失败");
throw new AcountEmailException("Faild to send email",e);
}
}
@Override
public void sendEmail(String to, String subject, String htmlText,
String path) throws AcountEmailException {
try{
MimeMessage msg = javaMailSender.createMimeMessage();
//MimeMessageHelper msgHelper = new MimeMessageHelper(msg);
MimeMessageHelper msgHelper =new MimeMessageHelper(msg,true);//想要传送附件就必须加true
msgHelper.setFrom(systemEmail);
msgHelper.setTo(to);
msgHelper.setSubject(subject);
msgHelper.setText(htmlText, true);
FileSystemResource file = new FileSystemResource(new File(path));
//ClassPathResource file = new ClassPathResource("path");
msgHelper.addAttachment(file.getFilename(), file);
javaMailSender.send(msg);
log.info("邮件发送成功");
}catch(Exception e){
log.error("邮件发送失败");
throw new AcountEmailException("Faild to send email",e);
}
}
public static void main(String[] args) {
ClassPathXmlApplicationContext cxa=new ClassPathXmlApplicationContext("acount-email.xml");
AcountEmailService aesi = (AcountEmailService) cxa.getBean("acountEmailService");
try{
aesi.sendEmail("XXX@XXX", "hello world", "<h3>Test</h3>");
}catch(AcountEmailException e){
e.printStackTrace();
}
}
}
6. mail.properties用qq给自己的另一个邮箱发送邮件,不好之处就是慢而且还需要把自己的qq的smtp/pop3打开,就在有邮箱设置里面,paaword为授权码port是端口号,需要查一下,不同的有495,20,25等等,也可以自己和自己发,这样更快一点
email.protocol=smtp
email.host=smtp.qq.com
email.port=*
email.username=fajianfang@xxx
email.password=*
email.auth=true
email.systemEmail=fajianfang@qq.com
email.timeout=10000
email.socketFactory=javax.net.ssl.SSLSocketFactory
7. acount-email.xml loaction 我注解掉的是相对路径,保留的是绝对路径,当启动服务器的时候应该使用相对路径注意一下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- <property name="location" value="classpath:mail.properties"/> -->
<property name="location" value="file:src/main/resources/mail.properties" />
</bean>
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="protocol" value="${email.protocol}" />
<property name="host" value="${email.host}" />
<property name="port" value="${email.port}" />
<property name="username" value="${email.username}" />
<property name="password" value="${email.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.host">${email.host}</prop>
<prop key="mail.transport.protocol">${email.protocol}</prop>
<prop key="mail.${email.protocol}.auth">${email.auth}</prop>
<prop key="mail.${email.protocol}.socketFactory.class">${email.socketFactory}</prop>
<prop key="mail.${email.protocol}.port">${email.port}</prop>
<prop key="mail.${email.protocol}.socketFactory.port">${email.port}</prop>
<prop key="mail.${email.protocol}.timeout">${email.timeout}</prop>
</props>
</property>
</bean>
<bean id="acountEmailService" class="com.test.acount.email.AcountEmailServiceImpl">
<property name="javaMailSender" ref="javaMailSender" />
<property name="systemEmail" value="${email.systemEmail}" />
</bean>
</beans>