spring 发送电子邮件

  首先应导入的jar包:mail(javax.mail)、activation(javax.activation)和spring-context-support。(此处不是spring jar)。

   之后就是在spring的配置文件中添加发送电子邮件的相关配置信息

<!-- 发送注册邮件 -->
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.163.com" />
		<property name="username" value="yotionking" />
		<property name="password" value="xxxxxxxxxx" />
		<property name="defaultEncoding" value="UTF-8" />
	</bean>

    最后就是具体的业务代码

SimpleMailMessage message = new SimpleMailMessage();
	message.setFrom("[email protected]");
	message.setSubject("Hi,this is spring mail,just for test!");
	message.setTo(userInfo.getEmail());
	message.setText("just for test ");
	mailSender.send(message);

相关推荐