这个问题已经问过无数多遍了片断 if(auth){
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", userId);
props.put("mail.smtp.password",password);
session = javax.mail.Session.getInstance(props,new MyAuthenticator(userId, password));
}MyAuthenticator.javaimport javax.mail.*;public class MyAuthenticator extends Authenticator{
String userId;
String password;
public MyAuthenticator(String userId, String password){
this.userId = userId;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userId, password);
}
}
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", userId);
props.put("mail.smtp.password",password);
session = javax.mail.Session.getInstance(props,new MyAuthenticator(userId, password));
}MyAuthenticator.javaimport javax.mail.*;public class MyAuthenticator extends Authenticator{
String userId;
String password;
public MyAuthenticator(String userId, String password){
this.userId = userId;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userId, password);
}
}
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="javax.mail.*, javax.mail.internet.*, javax.activation.*,"%><%@ page import="java.net.*"%>
<%@ page import="java.util.*, java.text.*" %>
<%@ page session="true"%>
<% Properties props = new Properties();
Session sendMailSession;
Transport transport;
sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host", "smtp.sina.com.cn"); //"202.106.187.180" 是“smtp.sina.com.cn”的IP!
props.put("mail.smtp.auth", "true"); //允许smtp校验 transport = sendMailSession.getTransport("smtp");
transport.connect("smtp.sina.com.cn","你的用户名","你的密码"); //你在sina的用户名,密码...........
Message newMessage = new MimeMessage(sendMailSession); //设置mail主题
String mail_subject="图书过期通知";
newMessage.setSubject(mail_subject);
//设置发信人地址
String strFrom="管理员";
strFrom=new String(strFrom.getBytes(),"8859_1");
newMessage.setFrom(new InternetAddress(strFrom));
//设置收件人地址
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); //设置mail正文
newMessage.setSentDate(new java.util.Date());
String mail_text= "尊敬的读者,您好!\r\n"+
"\r\n "+
" 您在本馆所借的书《123》应该于 归还,现在已经过期,请尽快将图书归还!\r\n "+
"\r\n "+
" 管理员\r\n";
newMessage.setText(mail_text); newMessage.saveChanges(); //保存发送信息
transport.sendMessage(newMessage,newMessage.getRecipients(Message.RecipientType.TO)); //发送邮件 transport.close();
%>