Spring – Sending E-Mail Via Gmail SMTP Server With MailSender
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong.common</groupId> <artifactId>SpringExample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SpringExample</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>Java.Net</id> <url>http://download.java.net/maven/2/</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Java Mail API --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.3</version> </dependency> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> </dependencies> </project>
<iframe id="aswift_0" style="left: 0px; position: absolute; top: 0px;" name="aswift_0" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="728" height="90"></iframe>
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 – http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
Gmail configuration details – http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
File : Spring-Mail.xml
<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 id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <property name="port" value="587" /> <property name="username" value="username" /> <property name="password" value="password" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.starttls.enable">true</prop> </props> </property> </bean> <bean id="mailMail" class="com.mkyong.common.MailMail"> <property name="mailSender" ref="mailSender" /> </bean> </beans>
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"); } }
Download Source Code
Download it – Spring-Email-Gmail-Smtp-Example.zip
相关推荐
NULL 博文链接:https://delinghayiye-163-com.iteye.com/blog/1683812
java 发送邮件 spring发送邮件Mail
利用spring下的一个类进行邮件发送,内附具体用法
Spring实现各种邮件的发送 简单配置就可发送邮件
和上面对比,这次发送邮件使用 MimeMessageHelper 类。MimeMessageHelper 支持发送复杂邮件模板,支持文本、附件、HTML、图片等,接下来我们会继续使用。 (3)发送带附件的邮件 在 MailService 添加 ...
使用Spring Boot 开发支持多附件邮件发送微服务平台代码
Spring 封装的JavaMail,使邮件发送变的相当简单,我通过Spring的邮件发送实现了发纯文本形的内容,HTMl,带附件,带图片等示例代码,并自行测试过.希望对初学者有帮助
用spring发送邮件,该功能在一般系统监控中经常用到。
它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。 包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层...
Spring整合Quartz定时发送邮件
spring发送邮件所需jar包
基于spring mvc 框架,用freemarker创建邮件模板,包括详细邮件发送功能的spring配置和实现代码
本篇文章主要介绍了使用Spring发送邮件的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
使用Spring mail发送邮件的完整实例,包含代码与Spring mail的jar,可直接运行与使用。相关学习文档参考博客《Spring Mail邮件发送 http://blog.csdn.net/chenxiang0207/article/details/8256528》。
spring框架使用
spring+velocity发送邮件,spring+velocity发送邮件
NULL 博文链接:https://chaoyi.iteye.com/blog/2171158
Spring邮件发送服务(java邮件发送),支持多附件、多人发送/抄送/暗送
Spring mail发送邮件实例 Spring mail发送邮件实例