使用JavaMail接收邮件

本文提供了一个使用Java实现的邮件接收器类,能够从指定邮箱接收邮件,并处理邮件中的多部分内容,包括正文和附件。通过设置主机、用户名、密码及附件保存路径,该示例代码能够自动获取邮件箱中的邮件并解析附件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
package demo;

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.163.com");
        receiver.setUsername("xxxxxxxxx");
        receiver.setPassword("yyyyyyyyy");
        receiver.setAttachPath("C:/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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值