MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » JavaMail有关问题

JavaMail有关问题

www.MyException.Cn  网友分享于:2015-08-27  浏览:31次
JavaMail问题.
小弟才学JSP,看到JavaMail一部份.照着例子运行但是出错提示:
553   xxxxx@21.com:   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( "xxxxx@21.com ");
message.setFrom(from);

InternetAddress   to   =   new   InternetAddress( "xxxxx@21.com ");
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 = "shinings_83@126.com ";
String to = "44708999@qq.com ";
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作为消息对象的内容

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有