下面是我写的程序,成功了,你看看吧。
<%@ page contentType="text/html; charset=GBK" %>
<%@ page session="true" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %><html>
<head>
<title>发送确认信</title>
</head><body><%
//邮件服务器信息和邮件的信息
String host = "*.*.*.*";
String to = (String) session.getAttribute("email");
String from = "[email protected]";
String subject = "用户注册的确认信";
String customer_id=(String) session.getAttribute("customer_id");
String messageText = "请点击下面的链接来激活您的账户:\n"+"http://localhost:8080/do_register.jsp?customer_id="+customer_id+"&email="+to+"\n";
boolean sessionDebug = false;//和邮件服务器建立session
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);//错误处理
mailSession.setDebug(sessionDebug);//创建消息体
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);//发送邮件
Transport.send(msg);%>
您的第一步注册已经完成,下面进行第二步注册:<br>
我们已经向你的邮箱发送了一封确认信,请点击信中的确认链接以激活账户。
</table>
</body>
</html>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page session="true" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %><html>
<head>
<title>发送确认信</title>
</head><body><%
//邮件服务器信息和邮件的信息
String host = "*.*.*.*";
String to = (String) session.getAttribute("email");
String from = "[email protected]";
String subject = "用户注册的确认信";
String customer_id=(String) session.getAttribute("customer_id");
String messageText = "请点击下面的链接来激活您的账户:\n"+"http://localhost:8080/do_register.jsp?customer_id="+customer_id+"&email="+to+"\n";
boolean sessionDebug = false;//和邮件服务器建立session
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);//错误处理
mailSession.setDebug(sessionDebug);//创建消息体
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);//发送邮件
Transport.send(msg);%>
您的第一步注册已经完成,下面进行第二步注册:<br>
我们已经向你的邮箱发送了一封确认信,请点击信中的确认链接以激活账户。
</table>
</body>
</html>
以,但当要身份验证时,就会出现错误:javax.mail.MessagingException: 553 You are not authorized to send mail as >, authentication is required
我在自己机上试可以,可一放到163.COM上面就不行了!!!
import java.util.*;
import java.util.Date;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;/**
* <p>Title: Email</p>
* <p>Description: 发送Email</p>
*/
public class Mail{
public boolean sendMail(String subject,String body,String to){
try {
SmtpAuth sa=new SmtpAuth(); // 密码验证
sa.getuserinfo(你的用户名,口令);
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
Session sess=Session.getInstance(props,sa);
sess.setDebug(true);//调试信息
Message msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
msg.setFrom(new InternetAddress(你的EMAIL地址,显示名字));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport.send(msg);
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args){
Mail m=new Mail();
m.sendMail("test","test javamail api",目标EMAIL地址);
}
}这是认证函数:
import java.util.Date;
import java.util.*;
import java.util.Hashtable;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;/**
* <p>Title: Email身份验证</p>
* <p>Description: Email身份验证</p>
*/ public class SmtpAuth extends javax.mail.Authenticator{ //SMTP身份验证
private String user,password;
public void getuserinfo(String getuser,String getpassword){
user=getuser;
password=getpassword;
} protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user,password);
}
}
DEBUG SMTP SENT:DATA
DEBUG SMTP RECVD:354 END DATA WITH <CF><LF>.<CF><LF>
JAVAX.ACTIVATION.UNSUPPORTEDDATATYPEEXCEPTION:NO OBJECT DCH FOR MIME
TYPE TEXT/PLAIN;CHARSET=US-ASCII 出错了.