代码: <%@ page import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*" %> <html> <head> <TITLE>JSP meets JavaMail, what a sweet combo.</TITLE> </HEAD> <BODY> <% try{ Properties props = new Properties(); Session sendMailSession; Store store; Transport transport; sendMailSession = Session.getInstance(props, null);//props.put("mail.smtp.host", "smtp.jspinsider.com"); props.put("mail.smtp.host","smtp.sina.com.cn"); props.put("mail.smtp.auth", "true");Message newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress(request.getParameter("from"))); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to"))); newMessage.setSubject(request.getParameter("subject")); newMessage.setSentDate(new Date()); newMessage.setText(request.getParameter("text"));newMessage.saveChanges(); //transport = sendMailSession.getTransport("smtp"); transport = sendMailSession.getTransport("smtp"); transport.connect("smtp.sina.com.cn", "username","password");// transport.send(newMessage);%> <P>Your mail has been sent.</P> <%} catch(MessagingException m) { out.println(m.toString()); }%> </BODY> </HTML> 错误如下: javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.AuthenticationFailedException
i wrote a smtpclient class, if you are interested in it, i can send it to you!some code like :...... msg.saveChanges(); trans = session.getTransport(receiveAddress[0]); // register ourselves as listener for ConnectionEvents // and TransportEvents trans.addConnectionListener(this); trans.addTransportListener(this);
<%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<%
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
sendMailSession = Session.getInstance(props, null);//props.put("mail.smtp.host", "smtp.jspinsider.com");
props.put("mail.smtp.host","smtp.sina.com.cn");
props.put("mail.smtp.auth", "true");Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));newMessage.saveChanges();
//transport = sendMailSession.getTransport("smtp");
transport = sendMailSession.getTransport("smtp");
transport.connect("smtp.sina.com.cn", "username","password");//
transport.send(newMessage);%>
<P>Your mail has been sent.</P>
<%}
catch(MessagingException m)
{
out.println(m.toString());
}%>
</BODY>
</HTML>
错误如下:
javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.AuthenticationFailedException
msg.saveChanges();
trans = session.getTransport(receiveAddress[0]);
// register ourselves as listener for ConnectionEvents
// and TransportEvents
trans.addConnectionListener(this);
trans.addTransportListener(this);
if(username != null && password != null) {
trans.connect(host, username, password);
msg.saveChanges();
} else {
trans.connect();
}
//msg.saveChanges();
trans.sendMessage(msg, receiveAddress);......
[email protected]谢谢
设置了认证为true后,你的用户名和密码一定要是新浪的合法用户,否则就会认证失败,发不出去。