- 1.导入spring-support.jar mail.jar activtion.jar
- 2.配置Spring配置文件
- <beans>
- <bean id="mailSend" class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host" value="smtp.163.com"></property>
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.auth">true</prop>
- <prop key="mail.smtp.timeout">10000</prop>
- </props>
- </property>
- <property name="username" value="yu_zhang430@163.com"></property>
- <property name="password" value="25257758"></property>
- </bean>
- 3.编写发送类
- public class test {
- private ApplicationContext ac=null;
- public test()
- {
- ac=new ClassPathXmlApplicationContext("applicationContext.xml");
- }
- public void sendEmail(String to,String title,String text) {
- JavaMailSender jms=(JavaMailSender)ac.getBean("mailSend");
- SimpleMailMessage message=new SimpleMailMessage();
- message.setTo(to);
- message.setFrom("yu_zhang430@163.com");
- message.setText("测试邮件");
- message.setSubject(title);
- jms.send(message);
- }
- public static void main(String[] args) {
- test t=new test();
- t.sendEmail("aixinxiaodian123@163.com", "测试邮件", "测试邮件");
- }
- }
- 4.带附件的邮箱
- File file = new File("d:/中文文件.rar");
- messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
利用Spring 发送邮件
最新推荐文章于 2024-08-19 14:51:10 发布