JavaMail发送邮件(基于Struts2框架)

最近在学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>

 

 

主界面

发送成功

 

转载于:https://www.cnblogs.com/lemonxu/p/10852588.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答关于 JavaMail 发送邮件带附件的问题。 在 JavaMail 中发送带附件的邮件,可以通过以下步骤实现: 1. 创建一个 Session 对象,用于与邮件服务器进行通信。 ``` Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.example.com"); props.setProperty("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your_username", "your_password"); } }); ``` 2. 创建一个 MimeMessage 对象,设置邮件的基本信息,如发件人、收件人、主题等。 ``` MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("[email protected]")); message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); message.setSubject("Test email with attachment"); ``` 3. 创建一个 Multipart 对象,用于包含邮件正文和附件。 ``` Multipart multipart = new MimeMultipart(); ``` 4. 创建一个 MimeBodyPart 对象,用于包含邮件正文,并将其添加到 Multipart 对象中。 ``` MimeBodyPart bodyPart = new MimeBodyPart(); bodyPart.setText("This is the email body"); multipart.addBodyPart(bodyPart); ``` 5. 创建一个 MimeBodyPart 对象,用于包含附件,并将其添加到 Multipart 对象中。 ``` MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.attachFile(new File("path/to/attachment")); attachmentPart.setFileName("attachment.txt"); multipart.addBodyPart(attachmentPart); ``` 6. 将 Multipart 对象设置为 MimeMessage 对象的内容,并发送邮件。 ``` message.setContent(multipart); Transport.send(message); ``` 以上是 JavaMail 发送带附件邮件的基本步骤,你可以根据实际需求进行修改和扩展。希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值