- <%@ 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 Mime2Message(mailSession);
- msg.setFrom(new InternetAddress( fromuser ));
- msg.setRecipient(Message.RecipientType.TO, new InternetAddress( touser ));
- msg.setSubject( "邮件标题:" +subject);
- //((Mime2Message)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,用免费阿里云邮箱账号发邮件|云分享 - 开发者论坛
原文链接:
click.aliyun.com