Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java邮件邮件正文_Java_Jakarta Mail_Pop3 - Fatal编程技术网

Java邮件邮件正文

Java邮件邮件正文,java,jakarta-mail,pop3,Java,Jakarta Mail,Pop3,我正在尝试读取电子邮件,将电子邮件保存为对象,保存其附件,然后将对象作为JSON发布到API。除了主体仅作为com.sun.mail.util输出外,其他一切都正常工作。BASE64DecoderStream@1q9q9qj4. 我以为代码会处理带有附件的消息体,但它似乎不起作用 public class ReadEmails { private static String saveDirectory = "C://attachments"; public void setS

我正在尝试读取电子邮件,将电子邮件保存为对象,保存其附件,然后将对象作为JSON发布到API。除了主体仅作为com.sun.mail.util输出外,其他一切都正常工作。BASE64DecoderStream@1q9q9qj4. 我以为代码会处理带有附件的消息体,但它似乎不起作用

public class ReadEmails {
    private static String saveDirectory = "C://attachments"; 

    public void setSaveDirectory(String dir) {
        ReadEmails.saveDirectory = dir;
    }

    public static void downloadEmailAttachments(String host, String port, String userName, String password) {
        Properties properties = new Properties();

        properties.put("mail.pop3.host", host);
        properties.put("mail.pop3.port", port);
        properties.put("mail.pop3.starttls.enable", "true");
        properties.put("mail.pop3.ssl.trust", host);
        properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.pop3.socketFactory.fallback", "true");
        properties.setProperty("mail.pop3.socketFactory.port", String.valueOf(port));

        Session session = Session.getDefaultInstance(properties);

        try {
            Store store = session.getStore("pop3");
            store.connect(userName, password);

            Folder folderInbox = store.getFolder("INBOX");
            folderInbox.open(Folder.READ_ONLY);

            Message[] arrayMessages = folderInbox.getMessages();

            for (int i = 0; i < arrayMessages.length; i++) {
                Message message = arrayMessages[i];
                Address[] fromAddress = message.getFrom();
                String from = fromAddress[0].toString();
                String subject = message.getSubject();
                String sentDate = message.getSentDate().toString();

                String contentType = message.getContentType();
                String messageContent = "";

                String attachFiles = "";

                if (contentType.contains("multipart")) {
                    // content may contain attachments
                    Multipart multiPart = (Multipart) message.getContent();
                    int numberOfParts = multiPart.getCount();
                    for (int partCount = 0; partCount < numberOfParts; partCount++) {
                        MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
                        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                            // this part is attachment
                            String fileName = part.getFileName();
                            attachFiles += fileName + ", ";
                            part.saveFile(saveDirectory + File.separator + fileName);
                        } else {
                            // this part may be the message content
                            messageContent = part.getContent().toString();
                        }
                    }

                    if (attachFiles.length() > 1) {
                        attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
                    }
                } else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
                    Object content = message.getContent();
                    if (content != null) {
                        messageContent = content.toString();
                    }
                }


                NewCase nc = new NewCase();
                nc.setTitle(subject);
                nc.setDescription(messageContent);
                nc.setEmail(from);

                sendEmails(nc);
                System.out.print("email: "+ nc.getTitle() + nc.getDescription() + nc.getEmail());
            }

            folderInbox.close(false);
            store.close();
        } catch (NoSuchProviderException ex) {
            System.out.println("No provider for pop3.");
            ex.printStackTrace();
            System.out.println(ex);;
        } catch (MessagingException ex) {
            System.out.println("Could not connect to the message store");
            ex.printStackTrace();
            System.out.println(ex);;
        } catch (IOException ex) {
            ex.printStackTrace();
            System.out.println(ex);;
        }
    }
    static void sendEmails(NewCase nc) {
        final String url = "http://localhost:8080/email";

        String title = nc.getTitle();
        String description = nc.getDescription();
        String email = nc.getEmail();

        HttpClient httpClient = HttpClientBuilder.create().build();     
            HttpPost p = new HttpPost(url);        

            p.setEntity(new StringEntity("{\"title\":\"" + title + "\",\"description\":\"" + description + "\",\"email\":\""+ email +"\"}", 
                             ContentType.create("application/json")));

            try {
                HttpResponse r = httpClient.execute(p);
                System.out.println(r.getStatusLine().getStatusCode());

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
}
公共类读取电子邮件{
私有静态字符串saveDirectory=“C://attachments”;
公共void setSaveDirectory(字符串目录){
ReadEmails.saveDirectory=dir;
}
public static void downloadEmailAttachments(字符串主机、字符串端口、字符串用户名、字符串密码){
属性=新属性();
properties.put(“mail.pop3.host”,host);
properties.put(“mail.pop3.port”,port);
properties.put(“mail.pop3.starttls.enable”、“true”);
properties.put(“mail.pop3.ssl.trust”,主机);
setProperty(“mail.pop3.socketFactory.class”、“javax.net.ssl.SSLSocketFactory”);
properties.setProperty(“mail.pop3.socketFactory.fallback”、“true”);
properties.setProperty(“mail.pop3.socketFactory.port”,String.valueOf(port));
Session Session=Session.getDefaultInstance(属性);
试一试{
Store Store=session.getStore(“pop3”);
store.connect(用户名、密码);
Folder folderInbox=store.getFolder(“收件箱”);
folderInbox.open(文件夹只读);
Message[]arrayMessages=folderInbox.getMessages();
for(int i=0;i1){
attachFiles=attachFiles.substring(0,attachFiles.length()-2);
}
}else if(contentType.contains(“text/plain”)| | contentType.contains(“text/html”)){
对象内容=message.getContent();
如果(内容!=null){
messageContent=content.toString();
}
}
NewCase nc=newnewcase();
nc.设置标题(主题);
nc.setDescription(messageContent);
nc.setEmail(来自);
发送电子邮件(nc);
系统输出打印(“电子邮件:+nc.getTitle()+nc.getDescription()+nc.getEmail());
}
folderInbox.close(假);
store.close();
}捕获(无此提供异常例外){
System.out.println(“pop3没有提供程序”);
例如printStackTrace();
系统输出打印Ln(ex);;
}捕获(消息例外){
System.out.println(“无法连接到消息存储”);
例如printStackTrace();
系统输出打印Ln(ex);;
}捕获(IOEX异常){
例如printStackTrace();
系统输出打印Ln(ex);;
}
}
静态无效发送电子邮件(新案例nc){
最终字符串url=”http://localhost:8080/email";
字符串title=nc.getTitle();
String description=nc.getDescription();
字符串email=nc.getEmail();
HttpClient HttpClient=HttpClientBuilder.create().build();
HttpPost p=新的HttpPost(url);
p、 setEntity(新的StringEntity(“{\”title\”:\”+title+“\”,\”description\“:\”+”description+“\”,”email\“:\”+email+“\”}),
create(“application/json”);
试一试{
HttpResponse r=httpClient.execute(p);
System.out.println(r.getStatusLine().getStatusCode());
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}

您的问题不清楚<代码>正文仅输出为com.sun.mail.util。BASE64DecoderStream@1q9q9qj4输出在哪里?如果不清楚,很抱歉,当我将NewCase对象发布到API时,它会被输入到数据库中。在这里,保存消息的字段包含com.sun.mail.util。BASE64DecoderStream@1q9q9qj4而不是实际的消息。API和数据库在其他方面工作得很好,所以我认为在将NewCase对象转换为JSO对象时可能会出现问题。问题在于将
part.getContent()
转换为字符串时,因为这里的
part.getContent()
是一个实例