需要的jar包,spring相关的,mail-1.4.5.jar,activation-1.1.jar
1、spring的配置
<bean id="mailSenderImpl" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${email.host}" /> <property name="protocol" value="smtp"/> <property name="port" value="25"/> <property name="defaultEncoding" value="UTF-8"/> <property name="username" value="${email.username}" /> <property name="password" value="${email.password}" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <!-- 使用gmail smtp server的必须参数 --> <prop key="mail.smtp.starttls.enable">true</prop> </props> </property> </bean>
2、属性配置文件
email.host=smtp.126.com email.servermail=kefu@126.com email.tomail=zhongxf@126.com email.username=kefu@126.com email.password=aa876116063、java代码
@Autowired private JavaMailSender mailSenderImpl;
MimeMessage mimeMessage = mailSenderImpl.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); helper.setFrom("kefu@126.com"); helper.setTo("zhongxf@126.com"); mimeMessage.setSubject("kasdf好啊"); mimeMessage.setText("阿斯顿发斯蒂芬", "UTF-8"); //javaMailSenderImpl.send(mimeMessage); mailSenderImpl.send(mimeMessage);