使用Java

时间:2018-03-30 09:23:14

标签: java pdf gmail pop3

如何在收到的邮件中打开PDF附件在使用Java的Gmail中。 我正在尝试从Gmail中邮件中附加的pdf中获取用户ID。 有人可以帮我解决这些问题。 以下是使用java从Gmail读取所有电子邮件的代码。 这里我使用Java邮件API从收件箱中获取所有邮件详细信息。

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

public class EmailRead {

    private static final String MAIL_POP_HOST = "pop.gmail.com";
    private static final String MAIL_STORE_TYPE = "pop3";
    private static final String POP_USER = "users email";
    private static final String POP_PASSWORD = "users password";
    private static final String POP_PORT = "995";

    public static void getMails(String user, String password) {
        user = "slautomation2018@gmail.com";
        password = "dontKnow";
        try {
            // create properties field
            Properties properties = new Properties();
            properties.put("mail.pop3.host", MAIL_POP_HOST);
            properties.put("mail.pop3.port", POP_PORT);
            properties.put("mail.pop3.starttls.enable", "true");
            properties.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            // Session emailSession = Session.getDefaultInstance(properties);
            Session emailSession = Session.getInstance(properties, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(POP_USER, POP_PASSWORD);
                }
            });
            // create the POP3 store object and connect with the pop server
            Store store = emailSession.getStore(MAIL_STORE_TYPE);
            store.connect(MAIL_POP_HOST, user, password);
            // create the folder object and open it
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);

            // retrieve the messages from the folder in an array and print it

            Message[] messages = emailFolder.getMessages();
            int m = messages.length-1;
            int y = messages.length-2;
            System.out.println(messages[m]);
            System.out.println(messages[y]);
            System.out.println("messages.length---" + messages.length);
            for (int i = 0, n = messages.length; i < n; i++) {
                Message message = messages[i];
                System.out.println("---------------------------------");
                System.out.println("Email Number " + (i + 1));
                System.out.println(message.getSubject());
                System.out.println(message.getFrom()[0]);
                System.out.println(message.getContent().toString());
                System.out.println(message.getReceivedDate());

            }



            // close the store and folder objects
            emailFolder.close(false);
            store.close();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        getMails(POP_USER, POP_PASSWORD);
    }
}

1 个答案:

答案 0 :(得分:0)

要获取您需要使用的附件数据&#34; message.getContent()&#34;函数,所以你需要在&#34;消息消息=消息[i];&#34;之后放置波纹线。线。

Multipart multipart = (Multipart) message.getContent();

List<File> attachments = new ArrayList<File>();

for (int i = 0; i < multipart.getCount(); i++) {
    BodyPart bodyPart = multipart.getBodyPart(i);

    if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) &&
               StringUtils.isBlank(bodyPart.getFileName())) {
           InputStream is = bodyPart.getInputStream();
        File f = new File("/tmp/" + bodyPart.getFileName());
        FileOutputStream fos = new FileOutputStream(f);
        byte[] buf = new byte[4096];
        int bytesRead;
        while((bytesRead = is.read(buf))!=-1) {
            fos.write(buf, 0, bytesRead);
        }
        fos.close();
        attachments.add(f);
        }
}

在此代码中,您将获得附件列表。