關於自動發送郵件的常見錯誤解決方法
- 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; } }