关于自动发送邮件的常见错误解决方法
- 2020 年 4 月 2 日
- 筆記
关于自动发送邮件的com/sun/mail/util/LineInputStream解决方法
关于java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream 解决方法: 这里是myeclipse8.6自带的javaee.jar里面的mail.jar 和另外引入的mail.jar冲突 解决方法是找myeclipse自带的jar删除mail.jar和activition.jar 查看自己javaee.jar的位置: 找到本地文件 用winrar打开 找到 mail 和activition 文件夹,然后右键“删除文件” 。 重启服务,发送邮件就可以了。
——————————————–
收邮件: Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream 当出现以上错误时,恭喜您已经离接收邮件不远了,否则请您解决好所有的异常后再来看这个帖子。
javax.mail和javax.activation这两个包已经在javaEE5当中属于基础包了,就是JDK中自带了已经,但是里面的方法与现在外面的mail.jar和activation.jar有一些出入,所以初学者在直接copy别人代码的时候往往会出现上面的错误。 废话不多说下面是解决方法 进到
X:Program FilesMyEclipse 6.5myeclipseeclipsepluginscom.genuitec.eclipse.j2eedt.core_6.5.0.zmyeclipse650200806datalibrarysetEE_5
这个路径里,可以看到javaee.jar,用rar把这个文件打开,然后进到javax文件夹里,删除mail.jar和activation.jar(我的javaee.jar里,这两个东西是文件夹,总之删掉就OK,不过要注意备份一下)
删掉之后运行下面的代码,经行简单的修改以后就可以实现接收邮件的功能了!我已经测试过完全可行。
package com.oa.mail; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Properties; import javax.mail.BodyPart; import javax.mail.Folder; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Part; import javax.mail.Session; import javax.mail.Store; import sun.misc.BASE64Decoder; public class MailReceiver{ public static void main(String[] args){ MailReceiver receiver = new MailReceiver(); receiver.setHost("pop3.pchome.com.tw"); receiver.setUsername("xxxxxx");//您的邮箱账号 receiver.setPassword("yyyyyy");//您的邮箱密码 receiver.setAttachPath("f:\email");//您要存放附件在什么位置?绝对路径 try{ receiver.reveiveMail(); } catch (Exception e){ e.printStackTrace(); } } public void reveiveMail() throws Exception { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("pop3"); store.connect(getHost(), getUsername(), getPassword()); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); Message message[] = folder.getMessages(); System.out.println("Messages''s length: " + message.length); //FetchProfile profile = new FetchProfile(); //profile.add(FetchProfile.Item.ENVELOPE); //folder.fetch(message, profile); for (int i = 0; i < message.length; i++) { //message[i].setFlag(Flags.Flag.DELETED, true);//必须先设置:folder.open(Folder.READ_WRITE); handleMultipart(message[i]); } if (folder != null) { folder.close(true); } if (store != null) { store.close(); } } private void handleMultipart(Message msg) throws Exception { String disposition; Multipart mp = (Multipart) msg.getContent(); int mpCount = mp.getCount(); for (int m = 0; m < mpCount; m++) { handle(msg); BodyPart part = mp.getBodyPart(m); disposition = part.getDisposition(); if (disposition != null && disposition.equals(Part.ATTACHMENT)) { saveAttach(part, getAttachPath()); } else { System.out.println(part.getContent()); } } } private static void handle(Message msg) throws Exception { System.out.println("邮件主题:" + msg.getSubject());//测试 System.out.println("邮件作者:" + msg.getFrom()[0].toString()); System.out.println("发送日期:" + msg.getSentDate()); } private static void saveAttach(BodyPart part, String filePath) throws Exception { String temp = part.getFileName(); String s = temp.substring(8, temp.indexOf("?=")); String fileName = base64Decoder(s); System.out.println("有附件:" + fileName); InputStream in = part.getInputStream(); FileOutputStream writer = new FileOutputStream(new File(filePath+"\" + fileName)); byte[] content = new byte[255]; while ((in.read(content)) != -1) { writer.write(content); } writer.close(); in.close(); } private static String base64Decoder(String s) throws Exception { BASE64Decoder decoder = new BASE64Decoder(); byte[] b = decoder.decodeBuffer(s); return (new String(b)); } private String host = null; private String username = null; private String password = null; private String attachPath = null; public String getAttachPath() { return attachPath; } public void setAttachPath(String attachPath) { this.attachPath = attachPath; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }