通过javamail,用免费阿里云邮箱账号发邮件

环境:Debian9, OpenJDK-8,tomcat-8.5,javamail-1.16.1

过程:

  1. apt update && apt upgrade && apt autoremove # 更新软件
  2. apt install openjdk-8-jdk # 安装 openjdk-8
    javamail-2.png
  3. apt install libservlet3.1-java # 安装 Java Servlet 和 JSP 库
  4. mkdir -p /var/lib/tomcat8/webapps/ROOT/WEB-INF/lib # 创建测试lib目录
  5. cd /var/lib/tomcat8/webapps/ROOT/WEB-INF/lib && wget https://github.com/javaee/javamail/releases/download/JAVAMAIL-1_6_1/javax.mail.jar # 下载javamail-1.6.1到lib目录中
  6. vi /var/lib/tomcat8/webapps/ROOT/javamail.jsp # 创建测试发邮件表单 javamail.jsp,内容如下,此例是通过连接smtp.aliyun.com 的 465端口,账户名是 anqunhe@aliyun.com

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
    <%
    //homepage:  http://cn.waterlin.org/
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html; charset=utf-8");
    %>
    <%@ page import="javax.mail.*"%>
    <%@ page import="javax.mail.internet.*"%>
    <%@ page import="javax.activation.*"%>
    <%@ page import="java.util.*,java.io.*"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>JavaMail 电子邮件发送系统</title>
    </head>
    
    <body>
    
    <form action="" method="post" OnSubmit="">
    收件人Email:<br /> <input type="text" name="recipients"><br />
    发件人Mail:<br /> <input name="frommail" type="text" size="30" /><br />
    邮件标题 <br /> <input name="subject" type="text" size="50" /><br />
    内容:<br /> <textarea name="contents" cols="50" rows="10"></textarea> 
     <br /> <input type="submit" name="Submit" value="发送邮件" />
    <form>
    
    <%!
    //相应的服务器信息
    String host = "smtp.aliyun.com";
    String user = "anqunhe@aliyun.com";
    String password = "密码";
    String contentType="text/html; charset=utf-8";
    
    private class Authenticator extends javax.mail.Authenticator
    {
     public PasswordAuthentication getPasswordAuthentication()
     {
         String un = user;
         String pw = password;
         return new PasswordAuthentication(un, pw);
     }
    }
    %>
    <%
    String touser = request.getParameter("recipients")!=null ? request.getParameter("recipients") : "";
    
    
    String fromuser = request.getParameter("frommail")!=null ? request.getParameter("frommail") : "";
    
    
    String subject = request.getParameter("subject")!=null ? request.getParameter("subject") : "";
    
    
    String contents = request.getParameter("contents")!=null ? request.getParameter("contents") : "";
    
    
    
    
    try {
     Properties props = new Properties();
     props.put("mail.smtp.auth","true"); //是否验证
     props.put("mail.smtp.host", host);
     props.put("mail.smtp.user", user);
     props.put("mail.smtp.password",password);
     props.put("mail.smtp.ssl.enable","true");
     props.put("mail.smtp.ssl.enable", "true");
     props.put("mail.smtp.port","465");//Gmail 的端口号
     
     //根据邮件服务器是否需要 ttl 验证选用
     //props.put("mail.smtp.starttls.enable","true");
     
    
     boolean sessionDebug = false;
     Authenticator auth = new Authenticator();
     //Session mailSession = Session.getDefaultInstance(props, auth); //有时可能被拒绝
     Session mailSession = Session.getInstance(props,auth); //用户验证;
     //Session mailSession = Session.getInstance(props);
     //mailSession.setDebug(sessionDebug);
    
    
     Message msg = new MimeMessage(mailSession);
     msg.setFrom(new InternetAddress( fromuser ));
     msg.setRecipient(Message.RecipientType.TO, new InternetAddress( touser ));
     msg.setSubject( "邮件标题:" +subject);
     //((MimeMessage)msg).setSubject(subject, "GBK"); //设置中文标题
     msg.setSentDate(new Date());
    
     String text =  contents + "<hr>javamail.jsp 发送认证邮件<b>测试</b>。";
    
     msg.setContent(text, contentType);
    
     Transport transport = mailSession.getTransport("smtp");
     transport.connect(host,user,password);
    
     transport.send( msg );
    %>
    
    <p>你的邮件已发送,<a href="./javamail.jsp">请返回。</></p>
    
    <%
    }
    catch (Exception m) { 
         
    }
    %>
    
    </body>
    </html>

    javamail-7.png

  7. 填写好表单,测试发邮件。成功(在垃圾箱中)。
    javamail-8.png

参考:

标签: javamail

添加新评论