spring+maven实现邮件发送
程序员文章站
2023-12-21 22:13:46
本文为大家分享了spring+maven实现邮件发送的具体代码,供大家参考,具体内容如下
今天想弄个邮件发送,随即百度了下,发现很多用到邮件发送的朋友都会遇到各种各样的问...
本文为大家分享了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设置,如图:
发送一封简单的邮件 就是以上内容,希望可以给大家带来帮助.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。