通过javamail,用免费阿里云邮箱账号发邮件|云分享 - 开发者论坛

186 阅读2分钟
原文链接: click.aliyun.com
  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
  2. <%
  3. //homepage:  http://cn.waterlin.org/
  4. request.setCharacterEncoding("utf-8");
  5. response.setCharacterEncoding("utf-8");
  6. response.setContentType("text/html; charset=utf-8");
  7. %>
  8. <%@ page import="javax.mail.*"%>
  9. <%@ page import="javax.mail.internet.*"%>
  10. <%@ page import="javax.activation.*"%>
  11. <%@ page import="java.util.*,java.io.*"%>
  12. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  13. <html>
  14. <head>
  15. <title>JavaMail 电子邮件发送系统</title>
  16. </head>
  17. <body>
  18. <form action="" method="post" OnSubmit="">
  19. 收件人Email:<br /> <input type="text" name="recipients"><br />
  20. 发件人Mail:<br /> <input name="frommail" type="text" size="30" /><br />
  21. 邮件标题 <br /> <input name="subject" type="text" size="50" /><br />
  22. 内容:<br /> <textarea name="contents" cols="50" rows="10"></textarea>
  23. <br /> <input type="submit" name="Submit" value="发送邮件" />
  24. <form>
  25. <%!
  26. //相应的服务器信息
  27. String host = "smtp.aliyun.com";
  28. String user = "anqunhe@aliyun.com";
  29. String password = "密码";
  30. String contentType="text/html; charset=utf-8";
  31.    private class Authenticator extends javax.mail.Authenticator
  32.    {
  33.     public PasswordAuthentication getPasswordAuthentication()
  34.     {
  35.         String un = user;
  36.         String pw = password;
  37.         return new PasswordAuthentication(un, pw);
  38.     }
  39. }
  40. %>
  41. <%
  42. String touser = request.getParameter("recipients")!=null ? request.getParameter("recipients") : "";
  43. String fromuser = request.getParameter("frommail")!=null ? request.getParameter("frommail") : "";
  44. String subject = request.getParameter("subject")!=null ? request.getParameter("subject") : "";
  45. String contents = request.getParameter("contents")!=null ? request.getParameter("contents") : "";
  46.    try {
  47.     Properties props = new Properties();
  48.     props.put("mail.smtp.auth","true"); //是否验证
  49.     props.put("mail.smtp.host", host);
  50.     props.put("mail.smtp.user", user);
  51.     props.put("mail.smtp.password",password);
  52.     props.put("mail.smtp.ssl.enable","true");
  53.     props.put("mail.smtp.ssl.enable", "true");
  54.     props.put("mail.smtp.port","465");//Gmail 的端口号
  55.     
  56.     //根据邮件服务器是否需要 ttl 验证选用
  57.     //props.put("mail.smtp.starttls.enable","true");
  58.     
  59.     boolean sessionDebug = false;
  60.     Authenticator auth = new Authenticator();
  61.     //Session mailSession = Session.getDefaultInstance(props, auth); //有时可能被拒绝
  62.     Session mailSession = Session.getInstance(props,auth); //用户验证;
  63.     //Session mailSession = Session.getInstance(props);
  64.     //mailSession.setDebug(sessionDebug);
  65.     Message msg = new Mime2Message(mailSession);
  66.     msg.setFrom(new InternetAddress( fromuser ));
  67.     msg.setRecipient(Message.RecipientType.TO, new InternetAddress( touser ));
  68.     msg.setSubject( "邮件标题:" +subject);
  69.     //((Mime2Message)msg).setSubject(subject, "GBK"); //设置中文标题
  70.     msg.setSentDate(new Date());
  71.     String text =  contents + "<hr>javamail.jsp 发送认证邮件<b>测试</b>。";
  72.     msg.setContent(text, contentType);
  73.     Transport transport = mailSession.getTransport("smtp");
  74.     transport.connect(host,user,password);
  75.     transport.send( msg );
  76. %>
  77.    <p>你的邮件已发送,<a href="./javamail.jsp">请返回。</></p>
  78.    <%
  79.    }
  80.    catch (Exception m) {
  81.         
  82.    }
  83.    %>
  84.    </body>
  85.    </html>