我尝试从Java2s.com获取附件文件名的教程。 我正在做的是从Outlook Web Access Light中读取电子邮件。 如果我把Outlook Web Access Light的URL地址,我有错误:
Exception in thread "main" javax.mail.NoSuchProviderException: No provider for http
at javax.mail.Session.getProvider(Session.java:455)
at javax.mail.Session.getStore(Session.java:530)
at javax.mail.Session.getFolder(Session.java:602)
at MainClass.main(MainClass.java:19)
Java Result: 1
我不明白这句话:
("protocol://username@host/foldername");
这是代码:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
public class MainClass {
public static void main(String[] args) throws Exception {
URLName server = new URLName("protocol://username@host/foldername");
Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
Folder folder = session.getFolder(server);
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println(messages[i].getSize() + " bytes long.");
System.out.println(messages[i].getLineCount() + " lines.");
String disposition = messages[i].getDisposition();
if (disposition == null){
; // do nothing
}else if (disposition.equals(Part.INLINE)) {
System.out.println("This part should be displayed inline");
} else if (disposition.equals(Part.ATTACHMENT)) {
System.out.println("This part is an attachment");
String fileName = messages[i].getFileName();
System.out.println("The file name of this attachment is " + fileName);
}
String description = messages[i].getDescription();
if (description != null) {
System.out.println("The description of this message is " + description);
}
}
folder.close(false);
}
}
class MailAuthenticator extends Authenticator {
public MailAuthenticator() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
感谢您的支持 祝你有愉快的一天
答案 0 :(得分:0)
我不确定,但"protocol://username@host/foldername"
似乎描述的是格式而不是真实的网址,即它告诉您添加协议,用户名,主机和文件夹,如"http://Thomas@stackoverflow.com/need-help-about-get-attachment-file-name-tutorial-from-java2s-com"
(只是一个如何看起来的例子,这个特殊的网址不太可能工作/存在/无论如何:))。
答案 1 :(得分:0)
鉴于错误消息,我认为javaMail期望“smtp:”或“imap:”或“pop3:”的协议之一,因为它是构建其会话的方式。 我不认为它会用于Web访问,您必须获取Web界面连接到的pop3 / imp / smtp服务器的地址。