小弟才学JSP,看到JavaMail一部份.照着例子运行但是出错提示:
553 [email protected]: Sender address rejected: not logged in
书上例子是雅虎的,我改为我自己的21cn.com邮箱.Properties props = new Properties();
props.put("mail.smtp.host", "smtp.21cn.com");
Session s = Session.getInstance(props);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress("[email protected]");
message.setFrom(from);
InternetAddress to = new InternetAddress("[email protected]");
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Test from JavaMail.");
message.setText("Hello from JavaMail.");
Store store = s.getStore("pop3");
store.connect("pop.21cn.com", "xxxxx", "xxxxxxxxxx");
Transport.send(message);
store.close();以上那里有问题吗?21cn.com应该是提供客户端程序收发邮件的啊.
553 [email protected]: Sender address rejected: not logged in
书上例子是雅虎的,我改为我自己的21cn.com邮箱.Properties props = new Properties();
props.put("mail.smtp.host", "smtp.21cn.com");
Session s = Session.getInstance(props);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress("[email protected]");
message.setFrom(from);
InternetAddress to = new InternetAddress("[email protected]");
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Test from JavaMail.");
message.setText("Hello from JavaMail.");
Store store = s.getStore("pop3");
store.connect("pop.21cn.com", "xxxxx", "xxxxxxxxxx");
Transport.send(message);
store.close();以上那里有问题吗?21cn.com应该是提供客户端程序收发邮件的啊.
<%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
contentType="text/html; charset=gb2312"%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<table border="1"></table>
<%
//------------------------------
class MyAuthenticator
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
} protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}//-----------------------------
String smtphost = "smtp.126.com";
String from = "[email protected]";
String to = "[email protected]";
String subject = "JavaMail 测试邮件";
String content = "<table border='1'><tr><td><div align='center'><font color='red'>hello world</font></div></td></tr></table>";
try{
Properties props = new Properties();
Session sendMailSession;
Transport transport;
MyAuthenticator myauth = new MyAuthenticator("邮件服务器用户名","邮件服务器密码");
sendMailSession = Session.getInstance(props, myauth);
props.put("mail.smtp.host",smtphost);
props.put("mail.smtp.auth","true"); //这样才能通过验证Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
newMessage.setText(content); //给消息对象设置内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(content,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
newMessage.setContent(mm);//把mm作为消息对象的内容
newMessage.saveChanges();
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>
</HTML>