您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关如何在spring中使用maven发送邮件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
项目:
代码块
package com.baidu.action; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; /** * ----------------------------------------- * 文件: Email.java * 邮箱: fengemail04@sina.com * 描述: 发送Email工具类 * ----------------------------------------- */ public class Email { private MailSender mailSender; private SimpleMailMessage simpleMailMessage; /** * 方法名: sendMail * 参数名:@param subject 邮件主题 * 参数名:@param content 邮件主题内容 * 参数名:@param to 收件人Email地址 * 描述语: 发送邮件 */ public void sendMail(String subject, String content, String to) { simpleMailMessage.setSubject(subject); //设置邮件主题 simpleMailMessage.setTo(to); //设定收件人 simpleMailMessage.setText(content); //设置邮件主题内容 mailSender.send(simpleMailMessage); //发送邮件 } //Spring 依赖注入 public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) { this.simpleMailMessage = simpleMailMessage; } //Spring 依赖注入 public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } }
spring-smtp-mail.xml
<?xml version="1.0" encoding="UTF-8"?> <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-3.1.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <!-- 服务器 --> <property name="host" value="smtp.sina.com" /> <!-- 端口号 --> <property name="port" value="25" /> <!-- 用户名 --> <property name="username" value="fengemail04@sina.com" /> <!-- 密码 --> <property name="password" value="密码需要在邮箱里设置,或者给邮箱权限开启" /> <!-- SMTP服务器验证 --> <property name="javaMailProperties"> <props> <!-- 验证身份 --> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <!-- 目前我用过的EMAIL账号都是网易的,下面列出网易的SMTP服务器名和端口号: 网易邮箱 SMTP服务器 SMTP端口 POP3服务器 POP3端口 @126.com smtp.126.com 25 pop3.126.com 110 @163.com smtp.163.com 25 pop3.163.com 110 @yeah.net smtp.yeah.net 25 pop3.yeah.net 110 --> <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"> <!-- 发件人email --> <property name="from" value="fengemail04@sina.com" /> </bean> <bean id="simpleMail" class="com.baidu.action.Email"> <property name="mailSender" ref="mailSender" /> <property name="simpleMailMessage" ref="simpleMailMessage" /> </bean> </beans>
EmailTest.java
package com.baidu.test; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.baidu.action.Email; /** * ----------------------------------------- * 文件: EmailTest.java * 描述: Junit测试,运行将发送一封email * ----------------------------------------- */ public class EmailTest extends TestCase { public void testSendMail() { ApplicationContext context = new ClassPathXmlApplicationContext("spring-smtp-mail.xml"); Email mail = (Email)context.getBean("simpleMail"); mail.sendMail("标题", "内容", "fengemail05@sina.com"); //mail.sendMail("标题", "内容", "收件人邮箱"); } }
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.baidu.maven</groupId> <artifactId>mailtest02</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mailtest02 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.13.RELEASE</version> </dependency> </dependencies> <build> <finalName>mailtest02</finalName> </build> </project>
看完上述内容,你们对如何在spring中使用maven发送邮件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。