最近在学Struts2,就顺手写了个发送邮件的小Demo,代码里实现的是qq邮箱,其他邮箱也都差不多。

导包什么就不说了,上代码。

Struts2的Action类:

package com.Struts2Mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.opensymphony.xwork2.ActionSupport;

/**
* @author Lemon
* @version 创建时间:2019年5月12日 下午1:19:27
*/
public class MailAction extends ActionSupport {
    
    //发件人的邮箱
    private String from;
    //邮箱账户的密码
    private String password;
    //发送给谁
    private String to;
    //邮件主题
    private String subject;
    //邮件内容
    private String body;
    
    public String execute() {
        String ret = SUCCESS;
        try {
             Properties properties = new Properties();
                properties.put("mail.transport.protocol", "smtp");// 连接协议
                properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
                properties.put("mail.smtp.port", 465);// 端口号
                properties.put("mail.smtp.auth", "true");
                properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
                properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
                // 得到回话对象
                Session session = Session.getInstance(properties);
                // 获取邮件对象
                Message message = new MimeMessage(session);
                // 设置发件人邮箱地址
                message.setFrom(new InternetAddress(from));
                // 设置收件人邮箱地址 
                message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(to)});
                //message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@qq.com"));//一个收件人
                // 设置邮件标题
                message.setSubject(subject);
                // 设置邮件内容
                message.setText(body);
                // 得到邮件对象
                Transport transport = session.getTransport();
                // 连接自己的邮箱账户
                transport.connect(from, password);// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
                // 发送邮件
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
              }
              catch(Exception e)
              {
                 ret = ERROR;
                 e.printStackTrace();
              }
              return ret;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getSubject() {
        return subject;
    }
    
    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
    
}

 

 

创建主页,index.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>Insert title here</title>
</head>
<body>
<h2>发送邮件</h2>
   <form action="Sendemail" method="post">
           <label for="from">发件人</label><br/>
           <input type="text" name="from"/><br/>
           <label for="password">授权码</label><br/>
           <input type="password" name="password"/><br/>
           <label for="to">收件人</label><br/>
           <input type="text" name="to"/><br/>
           <label for="subject">主题</label><br/>
           <input type="text" name="subject"/><br/>
           <label for="body">内容</label><br/>
           <input type="text" name="body"/><br/>
           <input type="submit" value="Send Email"/>
   </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>Insert title here</title>
</head>
<body>
您的邮件成功发送给<s:property value="to"/>
</body>
</html>

 

 

发送失败页面,error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
发送失败
</body>
</html>

 

 

配置文件,web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>EmailStruts2</display-name>
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

配置文件,struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

   <constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">

      <action name="Sendemail" 
         class="com.Struts2Mail.MailAction"
         method="execute">
         <result name="success">/success.jsp</result>
         <result name="error">/error.jsp</result>
      </action>

   </package>

</struts>

 

 

主界面

发送成功

 

 posted on 2019-05-12 16:02  lemon_xu  阅读(189)  评论(0编辑  收藏  举报