博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring – Sending E-Mail Via Gmail SMTP Server With MailSender--reference
阅读量:7169 次
发布时间:2019-06-29

本文共 3061 字,大约阅读时间需要 10 分钟。

Spring comes with a useful ‘org.springframework.mail.javamail.JavaMailSenderImpl‘ class to simplify the e-mail sending process via JavaMail API. Here’s a Maven build project to use Spring’s ‘JavaMailSenderImpl‘ to send an email via Gmail SMTP server.

1. Project dependency

Add the JavaMail and Spring’s dependency.

File : pom.xml

4.0.0
com.mkyong.common
SpringExample
jar
1.0-SNAPSHOT
SpringExample
http://maven.apache.org
 
Java.Net
http://download.java.net/maven/2/
 
 
junit
junit
3.8.1
test
 
javax.mail
mail
1.4.3
 
org.springframework
spring
2.5.6
 
 

2. Spring’s Mail Sender

A Java class to send email with the Spring’s MailSender interface.

File : MailMail.java

package com.mkyong.common; import org.springframework.mail.MailSender;import org.springframework.mail.SimpleMailMessage; public class MailMail{	private MailSender mailSender; 	public void setMailSender(MailSender mailSender) {		this.mailSender = mailSender;	} 	public void sendMail(String from, String to, String subject, String msg) { 		SimpleMailMessage message = new SimpleMailMessage(); 		message.setFrom(from);		message.setTo(to);		message.setSubject(subject);		message.setText(msg);		mailSender.send(message);		}}

3. Bean configuration file

Configure the mailSender bean and specify the email details for the Gmail SMTP server.

Note
Gmail configuration details – 

File : Spring-Mail.xml

 
 
true
true
 
 

4. Run it

package com.mkyong.common; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; public class App {    public static void main( String[] args )    {    	ApplicationContext context =              new ClassPathXmlApplicationContext("Spring-Mail.xml");     	MailMail mm = (MailMail) context.getBean("mailMail");        mm.sendMail("from@no-spam.com",    		   "to@no-spam.com",    		   "Testing123",     		   "Testing only \n\n Hello Spring Email Sender");     }}

referenc from:http://www.mkyong.com/spring/spring-sending-e-mail-via-gmail-smtp-server-with-mailsender/

 

转载地址:http://zimwm.baihongyu.com/

你可能感兴趣的文章
poj 1850 code
查看>>
我的友情链接
查看>>
ASP.NET WebApi 基于OAuth2.0实现Token签名认证
查看>>
MR程序的几种提交运行模式
查看>>
下拉框树形
查看>>
mysql自动备份脚本
查看>>
VMware中安装RHEL 7.1后出现锁屏的解决方法
查看>>
我的友情链接
查看>>
[示例代码]爱车加油记
查看>>
android 点击屏幕让软件盘消失
查看>>
关于linux特殊重定向的理解
查看>>
MonkeyRunner_Examples(1)
查看>>
关于华为交换机quideway s5700的vlan的一些配置命令
查看>>
Android LogUtils打印日志工具类
查看>>
什么是真正的APM(一)
查看>>
c33中的transform属性
查看>>
sql 子查询部分使用简介
查看>>
SCCM部署(一)---环境介绍
查看>>
点击隐藏
查看>>
display:inline-block的深入理解
查看>>