java 调用
/**
* 发送激活邮件*
* @param email
* @param subject TODO
* @return TODO
* @throws MessagingException
* @throws IOException
* @throws TemplateException
*/
public Boolean sendTextMail(final String email, String emailcontent,
final String subject) throws MessagingException, IOException,
TemplateException {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("mail.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
// ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");
org.springframework.core.io.Resource cr = new ClassPathResource("spring/applicationContext-mail.xml");
BeanFactory b = new XmlBeanFactory(cr);
JavaMailSender sender=(JavaMailSender) b.getBean("javamailsenderimpl");
// ApplicationContext ctx = (ApplicationContext) new FileSystemResource("applicationContext.xml");
// JavaMailSender sender = (JavaMailSender) actx.getBean("javamailsenderimpl");
MimeMessage msg = sender.createMimeMessage();
MimeMessageHelper helper = null;
helper = new MimeMessageHelper(msg, false, "UTF-8");
helper.setTo(email);
helper.setFrom(p.getProperty("email.username"));
helper.setSubject(subject);
helper.setText(emailcontent, true);
sender.send(msg);
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
/**
* 读取email.properties 里面的信息
*
* @return
*/
public String readProperties() {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("mail.properties");
Properties p = new Properties();
try {
p.load(inputStream);
inputStream.close();
sysSendmail = p.getProperty("email.username");
sysMailSmtp = p.getProperty("email.host");
sysMailPort = p.getProperty("email.port");
smtpusername = p.getProperty("email.smtpname");
smtppwd = p.getProperty("email.password");
return "json";
} catch (IOException e) {
e.printStackTrace();
return "json";
}
}
propertites配置:
email.defaultEncoding=UTF-8
email.host=smtp.163.com
email.port=25
email.username=renjitongjiaoyou@163.com
email.password=123456654321
email.smtpname=renjitongjiaoyou@163.com
email.timeout=25000
email.auth=true
spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<!-- 邮箱服务器配置 -->
<bean id="javamailsenderimpl" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="port" value="25"></property>
<property name="username" value="renjitongjiaoyou@163.com"></property>
<property name="password" value="123456654321"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
</beans>