gznofeng 发表于 2013-1-29 23:36:22

关于javamail与j2ee5的一个BUG

我用JAVAMAIL写了一个简单的发邮件的程序,但是总有异常出现
javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:768)
at javax.mail.Session.getTransport(Session.java:708)
at javax.mail.Session.getTransport(Session.java:651)
at javax.mail.Session.getTransport(Session.java:631)
at org.conference.util.SendMail.send(SendMail.java:117)
at org.conference.util.SendMail.main(SendMail.java:146)


然后我下了一个javamail-1.4.1,把其中的mail包导入,异常不一样了,是下面的异常
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
at javax.mail.Session.loadProvidersFromStream(Session.java:928)
at javax.mail.Session.access$000(Session.java:174)
at javax.mail.Session$1.load(Session.java:870)
at javax.mail.Session.loadResource(Session.java:1084)
at javax.mail.Session.loadProviders(Session.java:889)
at javax.mail.Session. <init>(Session.java:210)
at javax.mail.Session.getDefaultInstance(Session.java:299)
at org.conference.util.SendMail.send(SendMail.java:101)
at org.conference.util.SendMail.main(SendMail.java:146)


结果是J2EE5跟javamail包有冲突 我将J2EE5包中的mail跟activation部分删掉就可以了
package com.javamail.test;import java.util.*;import javax.mail.*;import javax.mail.internet.*;public class TestJavaMail {    public static void main(String[] arge) throws Exception {      String from = "pizza@trendcom.com.cn";      String to = "pizza@trendcom.com.cn";      String subject = "Test mail";      String body = "A text mail";      Properties props = System.getProperties();      // 设置SMTP邮件服务器:      props.put("mail.smtp.host", "mail.trendcom.com.cn");      // SMTP服务器需要验证:      props.put("mail.smtp.auth", "true");      //设置邮件传输协议为:smtp         props.put( "mail.transpost.protocol", "smtp");         //设置邮件服务器端口         props.put("mail.smtp.port", "25");         // 传入用户名和口令:      Session session = Session.getDefaultInstance(props,                new PasswordAuthenticator("pizza@trendcom.com.cn", "pizza"));      // 创建新邮件:      Message msg = new MimeMessage(session);      msg.setFrom(new InternetAddress(from));      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));      msg.setSubject(subject);      msg.setText(body);      msg.setSentDate(new Date());      // 发送:      Transport.send(msg);    }}class PasswordAuthenticator extends Authenticator {    private String username;    private String password;    public PasswordAuthenticator(String username, String password) {      this.username = username;      this.password = password;    }    protected PasswordAuthentication getPasswordAuthentication() {      return new PasswordAuthentication(username, password);    }}
=============================================================
发送附件的javamail
package com.javamail.test;import java.util.*;import java.io.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;public class TestJavaMailFile {public static void main(String[] args) {TestJavaMailFile mailFile=new TestJavaMailFile();mailFile.setHost("mail.trendcom.com.cn");mailFile.setUser("pizza@trendcom.com.cn");mailFile.setPassword("pizza");mailFile.setFrom("pizza@trendcom.com.cn");mailFile.setTo("pizza@trendcom.com.cn");mailFile.setSubject("附件邮件");mailFile.setText("请主意查收附件");mailFile.attachfile("test.doc");System.out.println(mailFile.startSend());}class PasswordAuthenticator extends Authenticator {    private String username;    private String password;    public PasswordAuthenticator(String username, String password) {      this.username = username;      this.password = password;    }    protected PasswordAuthentication getPasswordAuthentication() {      return new PasswordAuthentication(username, password);    }}// 定义发件人、收件人、主题等String user="";String password="";String to = "";String from = "";String host = "";String filename = "";String subject = "";String text="";// 用于保存发送附件的文件名的集合Vector file = new Vector();public TestJavaMailFile(){}// 该方法用于收集附件名public void attachfile(String fname) {file.addElement(fname);}// 开始发送信件的方法public boolean startSend() {// 创建Properties对象Properties props = System.getProperties();// 创建信件服务器props.put("mail.smtp.host", host);   // SMTP服务器需要验证:      props.put("mail.smtp.auth", "true");      //设置邮件传输协议为:smtp         props.put( "mail.transpost.protocol", "smtp");         //设置邮件服务器端口         props.put("mail.smtp.port", "25"); // 得到默认的对话对象Session session = Session.getDefaultInstance(props,new PasswordAuthenticator(getUser(),getPassword()));try {// 创建一个消息,并初始化该消息的各项元素MimeMessage msg = new MimeMessage(session);msg.setFrom(new InternetAddress(from));InternetAddress[] address = { new InternetAddress(to) };msg.setRecipients(Message.RecipientType.TO, address);msg.setSubject(subject);// 后面的BodyPart将加入到此处创建的Multipart中Multipart mp = new MimeMultipart();// 利用枚举器方便的遍历集合Enumeration efile = file.elements();// 检查序列中是否还有更多的对象while (efile.hasMoreElements()) {MimeBodyPart mbp = new MimeBodyPart();// 选择出每一个附件名filename = efile.nextElement().toString();// 得到数据源FileDataSource fds = new FileDataSource(filename);// 得到附件本身并至入BodyPartmbp.setDataHandler(new DataHandler(fds));// 得到文件名同样至入BodyPartmbp.setFileName(fds.getName());mp.addBodyPart(mbp);}// 移走集合中的所有元素file.removeAllElements();//创建新的MimeBodyPartMimeBodyPart messageBodyPart = new MimeBodyPart();//把正文信息附加到MimeBodyPartmessageBodyPart.setText(text);//Multipart添加正文信息    mp.addBodyPart(messageBodyPart);// Multipart加入到信件msg.setContent(mp);// 设置信件头的发送日期msg.setSentDate(new Date());// 发送信件Transport.send(msg);} catch (MessagingException mex) {mex.printStackTrace();Exception ex = null;if ((ex = mex.getNextException()) != null) {ex.printStackTrace();}return false;}return true;}public String getUser() {return user;}public void setUser(String user) {this.user = user;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getTo() {return to;}public void setTo(String to) {this.to = to;}public String getFrom() {return from;}public void setFrom(String from) {this.from = from;}public String getHost() {return host;}public void setHost(String host) {this.host = host;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getText() {return text;}public void setText(String text) {this.text = text;}}
页: [1]
查看完整版本: 关于javamail与j2ee5的一个BUG