java 收发邮件(带附件发送和附件解析)

java 收发邮件(带附件发送和附件解析)

1.java 发送邮件
收发邮件所需要的jar包。

	  <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>

1.1 发送不带附件的邮件

 /**
     * 发送邮件 不包含附件
     * @param toEmail 接受人邮箱
     * @param fromEmail 发送人邮箱
     * @param password 发送人密码
     */
    public static void sendEmaile(String toEmail,String fromEmail,String password) {
        try {
            Properties props = new Properties();
            //smtp保持不变,.后面改为邮箱后缀  eg:smtp.qq.com
            String host = "smtp.sea-level.com.cn";
            props.put("mail.smtp.host", host); // SMTP主机名
            props.put("mail.smtp.auth", "true"); // 是否需要用户认证
            props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密
            // 获取Session实例:
            Session session = Session.getInstance(props, new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(fromEmail, password);
                }
            });
            MimeMessage message = new MimeMessage(session);
            // 设置发送方地址:
            message.setFrom(new InternetAddress(fromEmail));
            // 设置接收方地址:
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            // 设置邮件主题:
            message.setSubject("Hello", "UTF-8");
            // 设置邮件正文:这是普通邮件
            message.setText("Hi 这是一份来自javamail 邮件", "UTF-8");
            // 发送:
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

注:host 一定要根据自己的真实邮箱修改哦。
1.2 发送带附件的邮件

/**
     * 发送邮件 包含附件
     * @param toEmail
     * @param fromEmail
     * @param password
     */
    public static void sendEmailForFile(String toEmail,String fromEmail,String password){
        try {
            Properties props = new Properties();
            String host = "smtp.sea-level.com.cn";
            props.put("mail.smtp.host", host); // SMTP主机名
            props.put("mail.smtp.auth", "true"); // 是否需要用户认证
            //props.put("mail.smtp.port", "587"); // 主机端口号
            props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密
            // 获取Session实例:
            Session session = Session.getInstance(props, new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(fromEmail, password);
                }
            });
            MimeMessage message = new MimeMessage(session);
            // 设置发送方地址:
            message.setFrom(new InternetAddress(fromEmail));
            // 设置接收方地址:
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            // 设置邮件主题:
            message.setSubject("Hello", "UTF-8");
            // 设置邮件正文:这是含有附件的邮件
            Multipart multipart = new MimeMultipart();
            // 添加text:
            BodyPart textpart = new MimeBodyPart();
            String body ="这是一个带有附件的邮件,请注意查收附件";
            textpart.setContent(body, "text/html;charset=utf-8");
            multipart.addBodyPart(textpart);
            // 添加附件: 直接发送内容
           /* BodyPart filepart = new MimeBodyPart();
            String fileName ="附件.txt";
            filepart.setFileName(fileName);
            //input 在附件中写入内容
            String input ="这里居然是文件的内容";
            try {
                filepart.setDataHandler(new DataHandler(new ByteArrayDataSource(input, "application/octet-stream")));
            } catch (IOException e) {
                e.printStackTrace();
            }
            multipart.addBodyPart(filepart);*/
           //发送邮件,发送本地文件
            BodyPart filepart = new MimeBodyPart();
            String fileName ="附件.txt";
            filepart.setFileName(fileName);
            //path 要发送的本地文件的路径
            String path ="d:\\file01.txt";
            FileDataSource fileDataSource = new FileDataSource(path);
            filepart.setDataHandler(new DataHandler(fileDataSource));
            multipart.addBodyPart(filepart);
            // 设置邮件内容为multipart:
            message.setContent(multipart);
            // 发送:
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

注:附件有两种方式,一种直接往附件里写内容,一种是发送本地存在的文件,按照提示进行放开相应的注释就可以了哦。
在本地使用main() 方法就可以测试。
2.接受邮件,并且读取附件内容

package com.hzm;

import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.util.Properties;

/**
 * @Author:huangzhimin
 * @Date:2020/7/30
 * @描述:这里只处理未读邮件,并能够获取邮件中的附件
 **/
public class ImapEmail {
    public static void main(String[] args) throws Exception{
        String user = "*******";// 邮箱的用户名
        String password = "****"; // 邮箱的密码

        Properties prop = System.getProperties();
        prop.put("mail.store.protocol", "imap");
        //此处也有修改为 内网邮箱后缀 eg: imap.qq.com
        prop.put("mail.imap.host", "imap.sea-level.com.cn");

        Session session = Session.getInstance(prop);
        // 使用imap会话机制,连接服务器
        IMAPStore store = (IMAPStore) session.getStore("imap");
        //链接邮箱
        store.connect(user, password);
        // 收件箱
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        //以只读的模式打开
        folder.open(Folder.READ_ONLY);
        // 获取总邮件数
        int  total = folder.getMessageCount();
        System.out.println("-----------------共有邮件:" + total + " 封--------------");
        System.out.println("未读邮件数:" + folder.getUnreadMessageCount());
        //获取收件箱中的所有邮件
        Message[] messages = folder.getMessages();
        for (Message message : messages) {
            //获取邮件的状态
            Flags flags = message.getFlags();
            //获取未读的邮件内容
            if (!flags.contains(Flags.Flag.SEEN)) {
                System.out.println("发送时间:" + message.getSentDate());
                System.out.println("主题:" + message.getSubject());
                String subject =  message.getSubject();
                System.out.println("内容: "+getBody((MimeMessage)message));
                System.out.println("附件详细信息:");
                getFileInputStream((MimeMessage)message);
                //System.out.println("附件:"+getFileInputStream((MimeMessage)message));
                //对指定的邮件进行解析 对指定主题的邮件设置已读
                if(subject.equals("READ_ONLY")){
                    System.out.println("开始执行解析程序");
                    //将邮件设置为已读
                    message.setFlag(Flags.Flag.SEEN,true);
                }
            }

        }
    }

    public static String getBody(Part part) throws MessagingException, IOException {
        if (part.isMimeType("text/*")) {
            return part.getContent().toString();
        }
        if (part.isMimeType("multipart/*")) {
            Multipart multipart = (Multipart) part.getContent();
            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                String body = getBody(bodyPart);
                if (!body.isEmpty()) {
                    return body;
                }
            }
        }
        return "";
    }

    public static void getFileInputStream(Part part) throws Exception{
        if (part.isMimeType("multipart/*")) {
            Multipart multipart = (Multipart) part.getContent();

            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                String disposition = bodyPart.getDisposition();
                String fileName = null;
                //说明有附件
                if((disposition != null) && ((disposition.equals(Part.ATTACHMENT)) || (disposition
                        .equals(Part.INLINE)))){
                    fileName = bodyPart.getFileName();
                    System.out.println("附件名称"+fileName);
                    //获取附件的流
                    System.out.println("大小为:" + bodyPart.getSize());
//                    InputStream inputStream = bodyPart.getInputStream();
//                    //全量读出
//                    int size = bodyPart.getSize();
//                    byte[] buffer = new byte[size];
//                    inputStream.read(buffer);
//                    System.out.println("附件内容是:"+new String (buffer));
                    //按行读出
                    BufferedReader in = new BufferedReader(new InputStreamReader(bodyPart.getInputStream()));
                    String line = null;
                    while (( line = in.readLine())!= null ){
                        System.out.println("this line:"+ line);
                        line = null;
                    }
                    //inputStream.close();
                    in.close();

                }
            }
        }
    }
}

本文只读取未读邮件的内容,和附件内容。如果想获取全部邮件的内容,修改if判断条件即可。
同时对指定主题的邮件进行读取,和解析后将邮件设置为已读状态。
注:邮件收取 有pop3 协议 和IMAP 协议,本文使用的是IMAP 协议。pop3协议无法区分邮件是否已读,同时无法将已读之类的操作提交到邮箱服务器,IMAP 使用更加便捷。

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用JavaMail API来模拟邮件系统,实现接收和解析邮件的功能。JavaMail API是一个标准的Java EE库,可以通过简单的步骤来配置和使用。 首先,需要建立一个与邮件服务器的连接。可以使用JavaMail提供的Session类来创建一个和邮件服务器的连接会话。通过设置合适的属性,比如邮件服务器的地址、端口号、用户名和密码等,建立与邮件服务器的连接。 接下来,可以使用JavaMail提供的Store类来接收邮件。Store类代表了邮件服务器的存储机制,可以通过协议(如POP3或IMAP)来接收邮件。使用Store的getFolder()方法获取邮件的文件夹,然后使用open()方法打开文件夹。 一旦成功打开文件夹,就可以通过Folder类提供的方法来读取邮件。可以使用getMessages()方法获取文件夹中的所有邮件,然后通过遍历每一封邮件,使用Message类提供的方法来获取邮件的各种信息,比如发送者、主题、正文内容等。 在解析邮件时,可以使用JavaMail提供的MimeMessage类来处理邮件的多媒体内容,比如附件、HTML格式的正文等。MimeMessage类提供了许多方法来获取和解析邮件的各种组成部分,可以使用getContent()方法来获取邮件的内容。 最后,可以根据需要进一步处理解析出的邮件内容,比如保存附件、过滤垃圾邮件、将重要邮件移动到特定文件夹等。使用Java的字符串处理、文件操作等功能,可以对解析出的邮件进行进一步的操作和处理。 总之,使用Java可以通过JavaMail API模拟邮件系统,实现接收和解析邮件的功能,从而实现更多复杂的应用,比如自动化邮件处理、邮件通知等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值