1.提供邮件信息发送接收,附件绑定功能.
1.配置spring-email.xml文件
<context:property-placeholder location="classpath:email.properties" ignore-unresolvable="true"/>
<bean id="webEmailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${demo-web.host}"/>
<property name="port" value="${demo-web.port}"/>
<property name="username" value="${bus-web.username}"/>
<property name="password" value="${demo-web.password}"/>
<property name="javaMailProperties">
<props>
<!--设置是否需要身份验证-->
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<bean id="smg" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${demo-web.from}"/>
<property name="to" value="${demo-web.to}"/>
</bean>
<bean id="emailBean" class="com.demo.service.email.WebEmail"/>
定义Email消息类
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
/**
* *********************************************************
* <p/>
* Author: XiJun.Gong
* Date: 2016-08-19 15:04
* Version: default 1.0.0
* Class description:
* <p/>
* *********************************************************
*/
@Component(value = "emailBean")
public class WebEmail {
private static Logger logger = LoggerFactory.getLogger(WebEmail.class);
@Resource(name = "webEmailSender")
private JavaMailSenderImpl mailSender;
@Resource(name = "smg")
private SimpleMailMessage MailMessage;
public void send(String subject, String content) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
messageHelper.setFrom(MailMessage.getFrom());
messageHelper.setSubject(subject); //主题
messageHelper.setText(content); //内容
messageHelper.setTo(MailMessage.getTo()); //发送给
/*
ClassPathResource file = new ClassPathResource("new file(path)");
messageHelper.addAttachment(file.getFilename(), file); //添加附件
*/
mailSender.send(mimeMessage); //发送邮件
} catch (Exception e) {
logger.error("the email send error ! {}", e);
}
}
}
3. email.properties配置文件
1 bus-web.subject=123
2 bus-web.content=123
3 bus-web.from=vipgxjun@163.com
4 bus-web.to=8888888@qq.com
5 bus-web.host=smtp.163.com
6 bus-web.port=25
7 bus-web.username=xijun.gong
8 bus-web.password=*******
4.运行即可