随笔-10  评论-0  文章-1  trackbacks-0

用myEclipse在进行javamail程序开发的时候,在准备阶段就遇到问题,导入了所需的jar包之后运行时可出现以下的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
原因是jar包版本不统一,解决方法如下:

删除Java EE 5 Libraries/javaee.jar/mail里的包有东西.

具体方法如下:
用rar打开X:/Program Files/MyEclipse 6.0/myeclipse/eclipse/plugins/com.genuitec.eclipse.j2eedt.core_6.0.1.zmyeclipse601200710/data/libraryset/EE_5/javaee.jar
,然后删除mail,就ok了.

ps:最好将原来的jar包备份


我一开始使用了merak邮件服务器,希望可以实现本地机的邮件收发,但是发现,其pop3和IMAP协议并非一直开启,如果不开启,则无法连接到邮件服务器。

查询到几个邮件服务器的设置:

163邮箱设置

接收邮件服务器(pop、IMAP或HTTP):pop.163.com、

发送邮件服务器(SMTP):smtp.163.com

sina设置

接收邮件服务器(pop、IMAP或HTTP):pop.sina.com.cn

发送邮件服务器(SMTP):smtp.sina.com.cn

tom设置

接收邮件服务器(pop、IMAP或HTTP):pop.tom.com

发送邮件服务器(SMTP)smtp.tom.com

sohu设置

接收邮件服务器(pop、IMAP或HTTP)pop3.sohu.com

发送邮件服务器(SMTP):smtp.sohu.com

126设置

接收邮件服务器(pop、IMAP或HTTP):pop3.126.com

发送邮件服务器(SMTP):smtp.126.com

QQ邮箱的POP3服务器:
pop.qq.com,SMTP服务器: smtp.qq.com

开发代码如下:

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.rjt.JavaMailWeb.Entity.User;

public class MailClientSina {
protected Session session;
protected Store store;
private String sendHost="smtp.sina.com.cn"; //发送邮件服务器
private String receiveHost="pop.sina.com.cn"; //接收邮件服务器
private String sendProtocol="smtp"; //发送邮件协议
private String receiveProtocol="pop3"; //接收邮件协议
private String username = "";
private String password = "";
private String fromAddr=""; //发送者地址
private String toAddr=""; //接收者地址

public void init()throws Exception{
//设置属性
Properties props = new Properties();
//props.put("mail.transport.protocol", sendProtocol);
//props.put("mail.store.protocol", receiveProtocol);
//props.put("mail.smtp.class", "com.sun.mail.smtp.SMTPTransport");
//props.put("mail.imap.class", "com.sun.mail.imap.IMAPStore");
//props.put("mail.pop3.class", "com.sun.mail.pop3.POP3Store");
props.put("mail.smtp.host", sendHost); //设置发送邮件服务器
props.put("mail.smtp.auth", "true");//需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
SmtpAuth smtpAuth=new SmtpAuth(username,password);
smtpAuth.getPasswordAuthentication();
// 创建邮件Session对象
session = Session.getDefaultInstance(props,smtpAuth);
session.setDebug(true); //输出跟踪日志
}

public void close()throws Exception{
store.close();
}
public void sendMessage(String fromAddr,String toAddr)throws Exception{
try
{
//创建一个邮件
Message msg = createSimpleMessage(fromAddr,toAddr);
//创建一个Transport对象
Transport transport=session.getTransport("smtp");
//连接SMTP服务器
transport.connect(sendHost,username, password);

//发送邮件
transport.send(msg,msg.getAllRecipients());
}catch(Exception e)
{
System.out.print(e);
}
}

public Message createSimpleMessage(String fromAddr,String toAddr)throws Exception{
//创建一封纯文本类型的邮件
Message msg = new MimeMessage(session);
InternetAddress[] toAddrs =InternetAddress.parse(toAddr, false);
msg.setRecipients(Message.RecipientType.TO, toAddrs);
msg.setSentDate(new Date());
msg.setSubject("Register Success!");
msg.setFrom(new InternetAddress(fromAddr));
msg.setText("Success!");
msg.saveChanges();
return msg;
}

public static void main(String[] args)throws Exception {
MailClientSina client=new MailClientSina();
client.init();
client.sendMessage(client.fromAddr,client.toAddr);
// client.receiveMessage();
}
}

//以下是SmtpAuth类的描述

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class SmtpAuth extends Authenticator {
String user,password;
//设置账号信息
public SmtpAuth(String user,String password){
this.user=user;
this.password=password;
}
//取得PasswordAuthentication对象
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user,password);
}
}

阅读全文
类别:Java 查看评论
文章来源:http://hi.baidu.com/ninky/blog/item/e2cd99caef3f158fc81768b7.html
posted on 2009-11-13 11:38 niuky 阅读(457) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: