<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送邮件的结果</title>
</head>
<body>
<fmt:requestEncoding value="gb2312"/>
<%
try{
//从html表单中接收邮件信息
String to_mail=request.getParameter("to");
String to_title=request.getParameter("title");
String to_content=request.getParameter("content");
//建立邮件会话
Properties props=new Properties();
props.put("mail.smtp.host","smtp.qq.com");//存储发送邮件服务器的信息
//props.put("mail.smtp.auth","true");//同时通过验证
Session s=Session.getInstance(props);//根据属性新建一个邮件会话
s.setDebug(true);
//由邮件会话新建一个消息对象
MimeMessage message=new MimeMessage(s);
//设置邮件
InternetAddress from=new InternetAddress("763905070@qq.com");
message.setFrom(from);//设置发件人
InternetAddress to=new InternetAddress(to_mail);
message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
message.setSubject(to_title);//设置主题
message.setText(to_content);//设置信件内容
message.setSentDate(new Date());//设置发信时间
//发送邮件
message.saveChanges();//存储邮件信息
Transport transport=s.getTransport("smtp");//邮件所使用的协议;
//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect("smtp.qq.com","763905070","密码");
transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.close();
%>
<div align="center">
<p>邮件发送成功!</p>
</div>
<%
}catch(MessagingException e){
out.println("邮件发送失败!");
}
%>
</body>
</html>
运行出现这们的错误:
EHLO 210.26.25.219
250-smtp.qq.com
250-PIPELINING
250-SIZE 52428800
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN
250-MAILCOMPRESS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "MAILCOMPRESS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
我的目的是用这个往我的另一个QQ邮箱发一封邮件。
jsp页面:
<html>
<head>
<title>写邮件内容</title>
</head>
<body>
<form name="form1" method="post" action="sendMail.jsp">
<table width="100" border="1" align="center" cellspacing="1">
<tr >
<td width="34%">收信人地址:</td>
<td width="66%"><input name="to" type="text" id="to"></td>
</tr>
<tr >
<td>主题:</td>
<td><input name="title" type="text" id="title"></td>
</tr>
<tr>
<td height="107" colspan="2">
<textarea name="content" cols="50" rows="5" id="content"></textarea>
</td>
</tr>