spring+maven实现邮件发送
时间:2020-10-13 10:06:45|栏目:JAVA代码|点击: 次
本文为大家分享了spring+maven实现邮件发送的具体代码,供大家参考,具体内容如下
今天想弄个邮件发送,随即百度了下,发现很多用到邮件发送的朋友都会遇到各种各样的问题,包括我也是,遇到了一些问题,下面把我整理好的发出来,按照步骤来,直接可以运行.
PS:以下源码来自百度,并非个人所写.话不多说,上图上代码
项目:
代码块
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>
测试:EmailTest.java 启动JUnit
如果发送不出去 ,就进入邮箱进行SMTP设置,如图:
发送一封简单的邮件 就是以上内容,希望可以给大家带来帮助.
栏 目:JAVA代码
下一篇:JAVA简单链接Oracle数据库 注册和登陆功能的实现代码
本文标题:spring+maven实现邮件发送
本文地址:http://www.codeinn.net/misctech/10562.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机