基于spring框架
简单三步:
- 导包
- 配置XML
- 接口调用
xml配置:
<?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-2.5.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="simpMail" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="[email protected]"/> </bean> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <!--主机,用户名,密码等配置--> <property name="host"><value>smtp.163.com</value></property> <property name="username"><value>[email protected]</value></property> <property name="password"><value>8888</value></property> <property name="port"><value>25</value></property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.timeout">25000</prop> </props> </property> </bean> </beans>
主测试方法:
package mytools.framework.jmail;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender; public class JmailMain { public static void main( String[] args ) { String[] locatoins={"applicationContext.xml"}; ApplicationContext context=new ClassPathXmlApplicationContext(locatoins); JavaMailSender sender=(JavaMailSender) context.getBean("mailSender"); SimpleMailMessage simpMail=(SimpleMailMessage) context.getBean("simpMail"); simpMail.setTo("[email protected]"); //目的邮箱 simpMail.setSubject("主题"); //主题 simpMail.setText("内容"); //内容 sender.send(simpMail); }}
时间: 2025-01-04 03:55:27