需要有关“获取附件文件名”的帮助来自 Java2s.com 的教程

发布于 2024-10-21 18:17:05 字数 2507 浏览 2 评论 0原文

我尝试从 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");
  }
}

谢谢您的支持 祝你今天过得愉快

i try the tutorial Get Attachment File Name from Java2s.com.
What i'm doing is to read email from the Outlook Web Access Light.
If i put the url address of the Outlook Web Access Light, i have the error:

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

I don't understand the line :

("protocol://username@host/foldername");

here is the code:

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");
  }
}

Thank you for your support
have a nice day

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分開簡單 2024-10-28 18:17:05

鉴于错误消息,我确实认为 javaMail 期望使用“smtp:”或“imap:”或“pop3:”协议之一,因为这是它构建会话的方式。
我不认为它可以与 Web 访问一起使用,您必须获取 Web 接口连接到的 pop3/imp/smtp 服务器的地址。

Given the error message I really think javaMail is expecting for protocol one of "smtp:" or "imap:" or "pop3:", because it is the way it constructs its session.
I don't think it will ever work with a web access, you have to get the address of the pop3/imp/smtp server the web interface connects to.

绮筵 2024-10-28 18:17:05

我不确定,但 "protocol://username@host/foldername" 似乎描述了一种格式而不是真实的网址,即它告诉您添加协议、用户名、主机以及类似 "http://[电子邮件受保护] 的文件夹/need-help-about-get-attachment-file-name-tutorial-from-java2s-com" (只是一个示例,这个特定的 url 不太可能工作/存在/无论如何:) )。

I'm not sure but "protocol://username@host/foldername" seems to describe a format rather than a real url, i.e. it tells you to add a protocol, a user name, a host and a folder like "http://[email protected]/need-help-about-get-attachment-file-name-tutorial-from-java2s-com" (just an example of how it might look like, this particular url is not likely to work/exist/whatever :) ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文