前言
我是通过这个网站学习的
https://www.w3cschool.cn/struts_2/struts_sending_email.html
项目搭建
1、初始化项目以及jar包拉取
2、修改web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssh_learn_mail</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、创建struts.xml
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="email" extends="struts-default">
<action name="send" class="com.czx.mail.EmailAction" method="execute">
<result name="true">/success.jsp</result>
<result name="false">/error.jsp</result>
</action>
</package>
</struts>
4、创建EmailAction
EmailAction
package com.czx.mail;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import com.opensymphony.xwork2.ActionSupport;
public class EmailAction extends ActionSupport {
public static Properties getProperties() {
return properties;
}
public static void setProperties(Properties properties) {
EmailAction.properties = properties;
}
//发邮件的人账号
private String fromEmail;
//发邮件的人账号密码
private String password;
//收邮件的人账号
private String toEmail;
//发的邮件主题
private String topic;
//发的邮件本体
private String body;
//创建属性配置类
public static Properties properties = new Properties();
//进行初始化属性配置
static {
properties.put("mail.smtp.host", "smtp.163.com");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.stmp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.stmp.port", "465");
}
@Override
public String execute() throws Exception {
//设置标志位,默认为false
String ret = "false";
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(fromEmail, password);
}
});
//通过session创建消息
MimeMessage message = new MimeMessage(session);
try {
//消息设置发送者
message.setFrom(new InternetAddress(fromEmail));
//设置主题
message.setSubject(topic);
//设置内容
message.setText(body);
//设置发送到的目的邮箱名以及发送类型
message.setRecipients(RecipientType.TO, InternetAddress.parse(toEmail));
//发送邮件
Transport.send(message);
ret = "true";
}catch(Exception e) {
//出现异常的话置为false
ret = "false";
e.printStackTrace();
}
return ret;
}
public String getFromEmail() {
return fromEmail;
}
public void setFromEmail(String fromEmail) {
this.fromEmail = fromEmail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToEmail() {
return toEmail;
}
public void setToEmail(String toEmail) {
this.toEmail = toEmail;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
5、创建访问页面
index.jsp,默认访问页,记住在这里密码不是你本人密码而是授权码,我这里使用的是163邮箱作为发件人,如果你使用了其他类型邮箱,那么action里面的properties属性需要变动一下。(如何开启163邮箱授权码:http://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2cda80145a1742516)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>邮件发送</title>
</head>
<body>
<form action="send" method="get">
发件人: <input type="text" name="fromEmail"><br/>
发件人密码: <input type="password" name="password"/><br/>
收件人: <input type="text" name="toEmail"/><br/>
邮件标题: <input type="text" name="topic"/><br/>
邮件内容: <input type="text" name="body"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
success.jsp,成功发送后跳转页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>邮件发送成功</title>
</head>
<body>
<s:property value="fromEmail"/> 发送给 <s:property value="toEmail"/>的邮件成功
</body>
</html>
error.jsp,发送失败后跳转页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>发送失败</title>
</head>
<body>
<s:property value="fromEmail"/> 发送给 <s:property value="toEmail"/>的邮件失败
</body>
</html>
可能遇到的异常
1.ognl.MethodFailedException: Method “execute” failed for object com.czx.mail.EmailAction@2e1a5368 [java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger]
上网搜了查了一下外加百度翻译可以得知,这里缺少了一个mail的相关依赖。所以问题应该出在自己使用的jar包上面。
参考自:
https://blog.csdn.net/sswqzx/article/details/84351468
2.配置属性问题
后面运行似乎又出了一点问题,发现是没有get/set这个properties属性,加上就好了。
3.javax.mail.AuthenticationFailedException: 550 User has no permission
因为163邮箱使用的是授权认证不能直接用账号密码来测试。要使用授权码
开启步骤如下:
http://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2cda80145a1742516
测试
访问http://localhost:8080/ssh_learn_mail/
结果
查看邮箱情况
项目地址
这个仓库里面的ssh_learn_mail
https://gitee.com/mrchen13427566118/ssh_learn.git
如果不会怎么弄到eclipse的话,就看这里
https://blog.csdn.net/weixin_43987277/article/details/116936221