`
yingpengfei1215
  • 浏览: 39357 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

spring 的mail配置

 
阅读更多

spring + mail 

 

orderManagerImpl:

 import org.springframework.mail.MailException;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class OrderManagerImpl implements OrderManager {

    private MailSender        mailSender;

    private SimpleMailMessage templateMessage;

    public void placeOrder() {
        SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
        msg.setTo("330940840@qq.com");
        msg.setText("my first test mail.");

        //MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
        
        try {
            this.mailSender.send(msg);
        } catch (MailException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    /**
     * Setter method for property <tt>mailSender</tt>.
     * 
     * @param mailSender value to be assigned to property mailSender
     */
    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }

    /**
     * Setter method for property <tt>simpleMailMessage</tt>.
     * 
     * @param simpleMailMessage value to be assigned to property simpleMailMessage
     */
    public void setTemplateMessage(SimpleMailMessage templateMessage) {
        this.templateMessage = templateMessage;
    }

}

 

sendMailTest.java

 

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @author wb-yingpf
 * @version $Id: SendMailTest.java, v 0.1 2012-6-11 下午03:05:45 wb-yingpf Exp $
 */
public class SendMailTest {

    @Test
    public void testSendMail() {

        ApplicationContext ac = new ClassPathXmlApplicationContext("test.xml");

        OrderManagerImpl orderManager = (OrderManagerImpl) ac.getBean("orderManager");

        orderManager.placeOrder();
    }

}

 test.xml

 

<?xml version="1.0" encoding="GBK"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:sofa="http://img.alipay.net/dtd/schema/service" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:webflow="http://www.springframework.org/schema/webflow-config"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://img.alipay.net/dtd/schema/service http://img.alipay.net/dtd/schema/service/sofa-service.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
         http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd"
	default-autowire="byName">

		<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="defaultEncoding" value="utf-8"></property>
		<property name="host" value="smtp.163.com"></property>
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
			</props>
		</property>
		<property name="username" value="yingpengfei1215@163.com"></property>
		<property name="password" value="*****"></property>
	</bean>

	<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
		<property name="from" value="yingpengfei1215@163.com"></property>
		<property name="subject" value="yingpengfei1215@126.com"></property>
	</bean>

	<bean id="orderManager" class="com.alipay.OrderManagerImpl">
		<property name="mailSender" ref="mailSender" />
		<property name="templateMessage" ref="templateMessage" />
	</bean>

</beans>

 

需要导入的jar包:

 

Spring-2.5.4.jar

junit-4.7.jar

commons-logging-1.1.1.jar

mail-1.4.jar

 

分享到:
评论

相关推荐

    spring-boot-mail

    spring.mail.default-encoding=UTF-8 #电子邮件地址 spring.mail.host=smtp.126.com #Application spring.application.name=SEND-MAIL #授权密码 spring.mail.password=password #邮箱服务器默认端口 spring.mail....

    利用 spring mail 通过 gmail(SSL) 发邮件

    NULL 博文链接:https://bastengao.iteye.com/blog/1064392

    spring-boot-starter-mail-2.2.4.RELEASE.jar

    maven配置了阿里云的仓库,不能下载spring-boot-starter-mail的依赖,从官网下载的,希望对有同样问题的小伙伴有用

    Spring Boot整合邮件发送并保存历史发送邮箱

    引入加 spring-boot-starter-mail 依赖包: Spring Boot整合邮件发送并保存历史发送邮箱 2、配置文件 application.yml Spring Boot整合邮件发送并保存历史发送邮箱 注意:测试时需要将 spring.mail.username 和...

    基于Spring的注册登录系统

    该项目基于Spring 3.1.3, Spring MVC, Hibernate 4.1.7, EhCache, Spring Data JPA 1.1.0, MySQL 5, Spring Security 3.1.3, Spring Mail, Recapcha等技术实现了网站注册、登录系统。项目包含了详尽的配置信息。 ...

    Spring boot 示例 官方 Demo

    spring-boot-mail:spring boot和邮件服务 spring-boot-mongodb:spring boot和mongodb的使用 spring-boot-multi-mongodb:spring boot和mongodb多数据源的使用 spring-boot-package-war:spring-boot打包成war包...

    spring jar 包详解

    (10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 (11) spring-web.jar 这个jar文件包含Web应用...

    Spring Boot Examples

    spring-boot-mail:spring boot和邮件服务 spring-boot-mongodb:spring boot和mongodb的使用 spring-boot-multi-mongodb:spring boot和mongodb多数据源的使用 spring-boot-package-war:spring-boot打包成war包...

    springboot实现邮箱发送功能

    本案例是通过qq邮箱配置之后通过springboot结合JavaMailSenderIm 发送邮件内容.包含带有纯文本/带有附件文件的

    spring4.1核心包

    这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、...

    springcloud入门

    springcloud-connect:数据连接中心,包含DB、Redis、MQ、Mail等组件的配置入口,只需其他模块在pom中添加依赖并提供相关配置即可快速接入。 springcloud-producer:服务提供者,包含具体的业务逻辑实现等。 spring...

    精通spring--源代码

    Java消息服务集成,Java Mail集成,远程服务集成。Java管理扩展集成,Java EE连接器架构集成  专注于Spring2.5高级特性的研究。包括忘却的Spring高级话题,Spring最佳实践  对Spring2.5内置的各种XML Schema支持...

    精通Spring(书签)

    Java消息服务集成,Java Mail集成,远程服务集成。Java管理扩展集成,Java EE连接器架构集成  专注于Spring2.5高级特性的研究。包括忘却的Spring高级话题,Spring最佳实践  对Spring2.5内置的各种XML Schema支持...

    最新最全的spring开发包

     这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 (11) spring-web.jar  这个jar文件包含Web应用开发时,用到Spring框架...

    Spring Boot中利用JavaMailSender发送邮件的方法示例(附源码)

    主要介绍了Spring Boot中利用JavaMailSender发送邮件的方法示例, 相信使用过Spring的众多开发者都知道Spring提供了非常好用的...在Spring Boot的Starter模块中也为此提供了自动化配置。需要的朋友可以参考借鉴。

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

     这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 (11) spring-web.jar  这个jar文件包含Web应用开发时,用到Spring框架时...

    Spring配置的邮件发送

    用组件做的一个小应用,加了点spring的小功能,依赖注射 方便配置

    thymeleafexamples-springmail:SpringMail-Thymeleaf网站上的“带有Thymeleaf的Spring丰富HTML电子邮件”文章的配套应用程序

    Thymeleaf 3示例:Spring Mail 这是一个示例应用程序,显示了如何使用Spring和Thymeleaf编写和发送动态电子邮件。 使用Thymeleaf,您可以轻松编写文本和HTML电子邮件。 要了解有关Thymeleaf的更多信息并下载最新...

    spring-boot-邮件发送

    Spring Boot通过集成spring-boot-starter-mail依赖和相关配置,可以方便地实现邮件发送功能。具体的步骤: 添加依赖:在Spring Boot项目的pom.xml文件中加入spring-boot-starter-mail依赖,如果需要发送模板邮件,还...

    Spring mail 使用多个账号发送带有附件的HTML邮件

    NULL 博文链接:https://wisdombrave.iteye.com/blog/546508

Global site tag (gtag.js) - Google Analytics