这是通过Spring 框架内置的功能完成简单邮件发送的测试用例。

  1. 导入相关的 jar 包.

Spring 邮件抽象层的主要包为 org.springframework.mail。

它包括了发送电子邮件的主要接口 MailSender,和值对象 SimpleMailMessage,它封装了简单邮件的属性。

如 from,to,cc, subject,text。

  2. 在邮箱设置中打开邮件的发送服务:

  

  3. 在src目录下建立mail.properties文件里边包含一下内容

mail.host=smtp.exmail.qq.com (邮箱的发送域名设置)
mail.username=你的邮箱名
mail.password=你的邮箱密码
mail.from=发送方的邮箱

  4. spring配置

 <!-- 加载Properties文件 -->
<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<value>${mail.from}</value>
</property>
<!-- 查看SimpleMailMessage源码还可以注入标题,内容等 -->
</bean>
<!-- 申明JavaMailSenderImpl对象 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="defaultEncoding" value="UTF-8" />
<property name="host" value="${mail.host}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<!-- 设置认证开关 -->
<prop key="mail.smtp.auth">true</prop>
<!-- 启动调试开关 -->
<prop key="mail.debug">true</prop>
<!-- 设置发送延时 -->
<prop key="mail.smtp.timeout">0</prop>
</props>
</property>
</bean>
</beans>

  5. action中添加简单的发送方法

 /**
* 本类测试邮件发送Html形式
*/
public class SingleMailSend {
static ApplicationContext actx =
new ClassPathXmlApplicationContext("applicationContext.xml");
static MailSender sender = (MailSender) actx.getBean("mailSender");
static SimpleMailMessage mailMessage =
(SimpleMailMessage) actx.getBean("mailMessage");
public static void main(String args[]) throws MessagingException {
mailMessage.setSubject("你好");
mailMessage.setText("这个是一个通过Spring框架来发送邮件的小程序");
mailMessage.setTo("******@qq.com");
sender.send(mailMessage);
}
}

关于使用 Spring 发送简单邮件的更多相关文章

  1. SpringBoot 发送简单邮件

    使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...

  2. JavaMail发送简单邮件

    非常简单的发送邮件实现,网上有很多啦,但还是自己写写记录下吧. package cn.jmail.test; import java.util.Properties; import javax.mai ...

  3. 使用javaMail发送简单邮件

    参考网页:http://blog.csdn.net/xietansheng/article/details/51673073package com.bfd.ftp.utils; import java ...

  4. (一)JavaMail发送简单邮件

    1,导入依赖 <dependency> <groupId>com.sun.mail</groupId> <artifactId>jakarta.mail ...

  5. Java Mail发送简单邮件,完整代码

    依赖javax.mail.jar,地址:https://java.net/projects/javamail/pages/Home 完整示例代码如下: package com.jadic.utils; ...

  6. Python通过yagmail和smtplib模块发送简单邮件

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...

  7. JavaMail(一):利用JavaMail发送简单邮件

    JavaMail,提供给开发者处理电子邮件相关的编程接口.它是Sun发布的用来处理email的API.它可以方便地执行一些常用的邮件传输.但它并没有包含在JDK中,要使用JavaMail首先要下载ja ...

  8. java 发送简单邮件(不带附件)

    引入依赖 邮件实体类 可用邮件服务器地址(网易为例) 邮件工具类 import com.me.beans.Mail; import lombok.extern.slf4j.Slf4j; import ...

  9. python——SMTP发送简单邮件

    [root@localhost python]# cat smtp.py import smtplib import string from email.mime.text import MIMETe ...

随机推荐

  1. goland激活码

    http://idea.youbbs.org      

  2. 何为编码 GBK 和 UTF8编码?GBK,GB2312与区位码有何关系?

    何为GBK,何为GB2312,与区位码有何渊源? 区位码是早些年(1980)中国制定的一个编码标准,如果有玩过小霸王学习机的话,应该会记得有个叫做“区位”的输入法(没记错的话是按F4选择).就是打四个 ...

  3. 【XMPP】Smack源码之消息接收与解析

    XmpPullParser 鉴于xmpp协议都是以xml格式来传输,因此源码中解析协议都是用到XmpPullParser来解析xml XmpPullParser很简单,先简单介绍几个比较常用的方法 / ...

  4. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  5. 完美解决"Encountered an NTFS Volume with a logfile ..."

    完美解决Ghost镜像文件时出现"Encountered an NTFS Volume with a logfile that has not been flushed(536)" ...

  6. MQTT 学习记录

    学习mqtt协议,从网上找demo验证一下. 参考链接 https://www.jianshu.com/p/ebbe25d1c4b2 https://blog.csdn.net/xxmonstor/a ...

  7. 跨浏览器的placeholder-jQuery版(jQuery插件EnPlaceholder)

    案例:整搜索框,需要默认占位符为"请输入关键词",获取焦点时,占位符消失或不可用(不影响正常输入),丢失焦点后,若用户无内容输入,占位符继续出现,继续占位.这种代码我想前端们已经很 ...

  8. Java8学习笔记(六)--Optional

    前言 身为一名Java程序员,大家可能都有这样的经历:调用一个方法得到了返回值却不能直接将返回值作为参数去调用别的方法.我们首先要判断这个返回值是否为null,只有在非空的前提下才能将其作为其他方法的 ...

  9. 一种消息和任务队列——beanstalkd

    beanstalkd 是一个轻量级消息中间件,其主要特性: 基于管道  (tube) 和任务 (job) 的工作队列 (work-queue):d 管道(tube),tube类似于消息主题(topic ...

  10. Python 字符串转JSON; 先装字典在转JSON; json.dumps(d)

    #-*- coding:UTF-8 -*- import os; import json class MysqlUtil(): def __init__(self): pass if __name__ ...