spring-context.xml配置
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- 发送服务器,根据当前使用的邮件服务商决定 -->
<property name="host" value="smtp.exmail.qq.com"/>
<property name="defaultEncoding" value="UTF-8"/>
<!-- 发送邮件的用户名密码 -->
<property name="username" value="xxx@xxx.com"/>
<property name="password" value="*******"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<!-- 此处不建议设置超时时间,设置了可能导致邮件服务商返回消息的延迟而导致程序抛出异常,以下代码send方法返回false,但是实际上邮件已经发出去了的问题,如果程序需要性能考虑,又不着急需要邮件发送那个的结果,可以考虑使用异步发送邮件(本人就是这样做的,刚开始程序会卡这儿很久,异步之后就好了) -->
<!--<prop key="mail.smtp.timeout">1000</prop>-->
<!-- 使用SSL加密端口,基于安全考虑 -->
<prop key="mail.smtp.port">465</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
</props>
</property>
</bean>
<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="xxx@xxx.com"/>
<!-- 接收邮件的邮箱 -->
<property name="to" value="xxx@xxx.com"/>
<!-- subject是设置邮件标题,刚开始在这儿设置,发现到了服务器上就中文乱码,如果非要在这个地方配置,可以将配置文件中的中文转成unicode码,就不会乱码了(但是这样别人就看不懂了,不利于后期代码维护,个人不建议) -->
<!--<property name="subject" value="${mail.subject}"/>-->
</bean>
<!-- 从Java代码读取配置文件值的配置 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
<!-- 指定文件编码方式,防止出现中文乱码 -->
<property name="fileEncoding" value="UTF-8"/>
</bean>
Java代码
import com.jf.customer.dto.CustomerInformationDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
/**
* Create By Zany 2018/1/4 0004
*/
public class EmailUtil {
@Autowired
private MailSender mailSender;
@Autowired
private SimpleMailMessage simpleMailMessage;
@Value("#{configProperties['mail.head']}")
private String mailHead;
@Value("#{configProperties['mail.foot']}")
private String mailFoot;
@Value("#{configProperties['mail.subject']}")
private String mailSubject;
private Logger logger = LoggerFactory.getLogger(EmailUtil.class);
public Boolean send(CustomerInformationDto customerInformationDto){
StringBuffer sb = new StringBuffer();
sb.append(mailHead + "\n\n");
sb.append("业务类型:" + customerInformationDto.getBusinessTypeName() + "\n");
sb.append("用户类型:" + customerInformationDto.getCustomerTypeName() + "\n");
sb.append("企业名称:" + customerInformationDto.getCompanyName() + "\n");
sb.append("联系人 :" + customerInformationDto.getContactName() + "\n");
sb.append("手机号码:" + customerInformationDto.getPhoneNumber() + "\n");
sb.append("邮 箱:" + customerInformationDto.getEmail() + "\n");
sb.append("QQ 号码:" + customerInformationDto.getQqNumber() + "\n");
sb.append("备 注:" + customerInformationDto.getRemarks() + "\n\n\n");
sb.append(mailFoot);
simpleMailMessage.setText(sb.toString());
simpleMailMessage.setSubject(mailSubject);
try {
mailSender.send(simpleMailMessage);
logger.debug("\n[Zany] 邮件发送成功\n");
return true;
} catch (MailException e) {
logger.debug("\n[Zany] 邮件发送失败 | " + e.getMessage());
e.printStackTrace();
}
return false;
}
}
以下是异步的代码片段
private static final ExecutorService executorService = Executors.newFixedThreadPool(10);
//异步发送邮件,若发送成功,修改邮件发送状态
executorService.submit(()->{
if (emailUtil.send(customerInformationDto)){
customerInformation.setStatus(1);
customerInformationMapper.updateStatus(customerInformation);
}
});
以上是个人的记录,也供猿们参考,欢迎提改进意见!
注:个人原创,转载请注明出处,谢谢!