问卷发邮箱

发布时间 2023-10-26 18:43:03作者: 董方凯
<!--questionnaire.html-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>董方凯的问卷调查</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 20px;
    }

    h1 {
      text-align: center;
      color: #333;
    }

    form {
      max-width: 400px;
      margin: 0 auto;
    }

    label {
      display: block;
      margin-bottom: 10px;
      color: #333;
      font-weight: bold;
    }

    input[type="text"],
    input[type="email"],
    select {
      width: 100%;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 4px;
      box-sizing: border-box;
      margin-bottom: 10px;
    }

    input[type="radio"],
    input[type="checkbox"] {
      margin-right: 5px;
    }

    p:last-child {
      text-align: center;
    }

    input[type="submit"],
    input[type="reset"] {
      padding: 10px 20px;
      background-color: #4CAF50;
      color: #fff;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    input[type="reset"] {
      background-color: #f44336;
    }
  </style>
</head>
<body>
<h1>个人信息调查</h1>
<form action="sendemail" method="post">
  <p><label for="name">姓名:</label><input type="text" name="name" id="name"></p>
  <p><label for="age">年龄:</label><input type="text" name="age" id="age"></p>
  <p>
    <label>性别:</label>
    <input type="radio" name="gender" value="男">男
    <input type="radio" name="gender" value="女">女
  </p>
  <p>
    <label>兴趣:</label>
    <input type="checkbox" name="hobby" value="阅读">阅读
    <input type="checkbox" name="hobby" value="运动">运动
    <input type="checkbox" name="hobby" value="电脑">电脑
  </p>
  <p>
    <label for="education">学历:</label>
    <select name="education" id="education">
      <option value="学士">学士</option>
      <option value="硕士">硕士</option>
      <option value="博士">博士</option>
    </select>
  </p>
  <p>
    <label for="email">邮箱地址:</label>
    <input type="email" name="email" id="email">
  </p>
  <p>
    <input type="submit" name="submit" value="提交">
    <input type="reset" name="reset" value="重置">
  </p>
</form>
</body>
</html>
//SendEmail.java
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.Socket;

@WebServlet("/sendemail")
public class SendEmail extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username="ZGZrdGVzdEAxNjMuY29t";
        String password="";    // 引号里面写发送邮箱的base64编码后的授权码
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String gender = request.getParameter("gender");
        String hobby = request.getParameter("hobby");
        String education = request.getParameter("education");
        String email = request.getParameter("email");
        try {
            Socket s = new Socket("smtp.163.com" , 25);
            //IO流
            BufferedReader in = new BufferedReader
                    (new InputStreamReader(s.getInputStream(),"UTF-8"));
            PrintWriter out = new PrintWriter
                    (new OutputStreamWriter(s.getOutputStream(),"gbk"),true);
            //connected mailServer or not
            read(in);
            //success or not
            send(in, out, "HELO theWorld");
            //validation
            send(in,out,"auth login");
            //handle data
            send(in,out,username);
            send(in,out,password);
            //where from
            send(in, out, "MAIL FROM: <dfktest@163.com>");
            //send to
            send(in, out, "RCPT TO: <2195963719@qq.com>");
            //mail content
            send(in, out, "DATA");
            send(out, "Subject:" + name +"的个人信息问卷");
            send(out, "From:董方凯的163测试邮箱 <dfktest@163.com>");
            send(out, "To:董方凯的qq邮箱 <2195963719@qq.com>");
            send (out, "\n");
            send(out, "姓名:"+name);
            send(out, "年龄:"+age);
            send(out, "性别:"+gender);
            send(out, "爱好:"+hobby);
            send(out, "学历:"+education);
            send(out, "邮箱:"+email);
            send(out, ".");
            //exit
            send(in, out, "QUIT");
            s.close();
            response.getWriter().println("Email sent successfully.");
        }
        catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println("Failed to send email.");
        }
    }
    //read info
    public void read(BufferedReader in){
        try {
            String msg=in.readLine();
            System.out.println(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //read info and input order
    public void send(BufferedReader in, PrintWriter out, String s) {
        try {
            out.write(s + "\n");
            out.flush();
            System.out.println("read : "+s);
            s = in.readLine();
            System.out.println("write : "+s);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    //input order
    public void send(PrintWriter out, String s) {
        try {
            out.write(s + "\n");
            out.flush();
            System.out.println("write: "+s);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

测试用的163邮箱 账号:dfktest@163.com 授权码:(保密)

Base64在线编码、解码网站:https://tool.chinaz.com/Tools/Base64.aspx

talnet过程:

220 163.com Anti-spam GT for Coremail System (163com[20141201])
read : HELO theWorld
write : 250 OK
read : auth login
write : 334 dXNlcm5hbWU6
read : ZGZrdGVzdEAxNjMuY29t
write : 334 UGFzc3dvcmQ6
read : (保密)
write : 235 Authentication successful
read : MAIL FROM: <dfktest@163.com>
write : 250 Mail OK
read : RCPT TO: <2195963719@qq.com>
write : 250 Mail OK
read : DATA
write : 354 End data with <CR><LF>.<CR><LF>
write: Subject:董方凯的个人信息问卷
write: From:董方凯的163测试邮箱 <dfktest@163.com>
write: To:董方凯的qq邮箱 <2195963719@qq.com>
write:

write: 姓名:董方凯
write: 年龄:21
write: 性别:男
write: 爱好:阅读
write: 学历:学士
write: 邮箱:2195963719@qq.com
write: .
read : QUIT
write : 250 Mail OK queued as zwqz-smtp-mta-g0-1,_____wDXD58PcTdlYWzVAA--.28706S2 1698132242