精华内容
下载资源
问答
  • Java读取邮件的方法

    2020-09-03 23:09:45
    主要介绍了Java读取邮件的方法,以163邮件服务器为例说明了Java读取邮件的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
  • java读取腾讯邮箱邮件1,邮箱协议设置2,java实现3,参考文档 1,邮箱协议设置 1,qq邮箱读取需要先配置邮箱协议,主要有两种,第一个是pop3,第二个是imap协议,两者之间的区别在于imap是可以区分邮件是否已读取...

    1,邮箱协议设置

    1,邮箱的读取需要先配置邮箱协议,主要有两种,第一个是pop3协议,第二个是imap协议,两者之间的区别在于imap是可以区分邮件是否已读取,而pop可以通过SearchTerm查询条件过滤邮件,关于两者的配置QQ邮箱可以直接点下方QQ官方链接设置,
    https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

    2,腾讯企业邮箱到不需要配置,因为他默认是已经开启了pop3、imap协议的,可以直接通过账号以及密码登录连接的,但是你也可以配置专用密码进行连接,方法如下图
    1,开启前
    开启前
    2,开启后
    开启后

    2,java代码实现

    1,两者之前的代码实际上都差不多,区别在于邮箱服务器的配置

    imap
    1,QQ邮箱服务器:imap.qq.com
    2,腾讯企业邮箱服务器:imap.exmail.qq.com
    
    pop
    1,QQ邮箱服务器:pop.qq.com
    2,腾讯企业邮箱服务器:pop.exmail.qq.com
    
    //涉及pom jar
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
    </dependency>
    
    package com.example.demo.utils;
    
    import org.apache.commons.lang3.StringUtils;
    
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import javax.mail.internet.MimeUtility;
    import java.io.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Properties;
    
    /**
     * @author:Deng
     * @date: 2021-04-14 17:30
     * @remark:  腾讯邮件读取
     */
    public class EmailUtils {
    
        public static void readEmail() {
            Properties props = new Properties();
            props.put("mail.imap.host", "imap.exmail.qq.com");//QQ邮箱为:imap.qq.com
            props.put("mail.imap.auth", "true");
            props.setProperty("mail.store.protocol", "imap");
            props.put("mail.imap.starttls.enable", "true");
            Session session = Session.getInstance(props);
            try {
                Store store = session.getStore();
                 //企业邮箱如果开启了安全登录,密码需要使用专用密码,没有开启使用账号密码即可
               store.connect("imap.exmail.qq.com", "xxx@xx.com", "xxxxx");
               //store.connect("imap.qq.com", "xxx@xx.com", "授权码");//QQ邮箱需要设置授权码
                Folder folder = store.getFolder("Inbox");
                //邮箱打开方式
                folder.open(Folder.READ_WRITE);
                //收取未读邮件
                Message[] messages = folder.getMessages(folder.getMessageCount() - folder.getUnreadMessageCount() + 1, folder.getMessageCount());
    
                System.out.println("邮件总数: " + folder.getMessageCount());
                //解析的邮件的方法,就粘贴了网上的了,有需要的直接点击参考文档的链接
                parseFileMessage(messages);
                folder.setFlags(messages, new Flags(Flags.Flag.SEEN), true);
            }catch (Exception e){
                System.out.println("异常: " + e);
            }
        }
    
      public static void readEmailPop() throws Exception {
            Properties props = new Properties();
            props.put("mail.pop3.host", "pop.exmail.qq.com");
            props.put("mail.pop3.auth", "true");
            props.setProperty("mail.store.protocol", "pop3");
            props.put("mail.pop3.starttls.enable", "true");
            Session session = Session.getInstance(props);
            Store store = session.getStore("pop3");
            store.connect("pop.exmail.qq.com", "xxx@xxx.net", "xxxx");
            Folder folder = store.getFolder("Inbox");
            //邮箱打开方式
            folder.open(Folder.READ_WRITE);
            //昨天零点时间
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -1);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Date mondayDate = cal.getTime();
            //昨天零点到当前时间的邮件
            SearchTerm comparisonTermGe = new SentDateTerm(ComparisonTerm.GE, mondayDate);
            SearchTerm comparisonTermLe = new SentDateTerm(ComparisonTerm.LE, new Date());
            SearchTerm comparisonAndTerm = new AndTerm(comparisonTermGe, comparisonTermLe);
            //条件查询只能通过pop协议的方式才生效
            Message[] messages = folder.search(comparisonAndTerm);
            //  Message[] messages = folder.getMessages();
            System.out.println("读取的邮件总数: " + messages.length);
            parseFileMessage(messages);
            // folder.setFlags(messages, new Flags(Flags.Flag.SEEN), true);
            System.out.println("邮件解析任务执行完毕");
        }
    
        public static void parseFileMessage(Message... messages) throws Exception {
            if (messages == null || messages.length < 1){
                System.out.println("没有可读取邮件");
                return;
            }
            // 解析读取到的邮件
            for (Message message : messages) {
                MimeMessage msg = (MimeMessage) message;
                System.out.println("------------------解析第" + msg.getMessageNumber() + "封邮件-------------------- ");
                System.out.println("主题: " + MimeUtility.decodeText(msg.getSubject()));
                System.out.println("发件人: " + getFrom(msg));
                System.out.println("收件人:" + getReceiveAddress(msg, null));
                System.out.println("发送时间:" + getSentDate(msg, null));
                System.out.println("是否已读:" + isSeen(msg));
                System.out.println("邮件优先级:" + getPriority(msg));
                System.out.println("是否需要回执:" + isReplySign(msg));
                System.out.println("邮件大小:" + msg.getSize() * 1024 + "kb");
                StringBuffer content = new StringBuffer(30);
                getMailTextContent(msg, content);
                System.out.println("邮件正文:" + (content.length() > 100 ? content.substring(0,100) + "..." : content));
            
                System.out.println();
                boolean isContainerAttachment = isContainAttachment(msg);
                System.out.println("是否包含附件:" + isContainerAttachment);
                if (isContainerAttachment) {
                    saveAttachment(msg, "D:\\data\\emailFile\\", msg.getFileName()); //保存附件
                }
                System.out.println("------------------第" + msg.getMessageNumber() + "封邮件解析结束-------------------- ");
            }
        }
        /**
         * 获得邮件发件人
         * @param msg 邮件内容
         * @return 姓名 <Email地址>
         * @throws MessagingException
         * @throws UnsupportedEncodingException
         */
        public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException {
            String from = "";
            Address[] froms = msg.getFrom();
            if (froms.length < 1)
                throw new MessagingException("没有发件人!");
            InternetAddress address = (InternetAddress) froms[0];
            String person = address.getPersonal();
            if (person != null) {
                person = MimeUtility.decodeText(person) + " ";
            } else {
                person = "";
            }
            from = person + "<" + address.getAddress() + ">";
    
            return from;
        }
        /**
         * 获得邮件发送时间
         * @param msg 邮件内容
         * @return yyyy年mm月dd日 星期X HH:mm
         * @throws MessagingException
         */
        public static String getSentDate(MimeMessage msg, String pattern) throws MessagingException {
            Date receivedDate = msg.getSentDate();
            if (receivedDate == null)
                return "";
    
            if (pattern == null || "".equals(pattern))
                pattern = "yyyy年MM月dd日 E HH:mm ";
    
            return new SimpleDateFormat(pattern).format(receivedDate);
        }
        /**
         * 判断邮件是否已读
         * @param msg 邮件内容
         * @return 如果邮件已读返回true,否则返回false
         * @throws MessagingException
         */
        public static boolean isSeen(MimeMessage msg) throws MessagingException {
            return msg.getFlags().contains(Flags.Flag.SEEN);
        }
        /**
         * 判断邮件是否需要阅读回执
         * @param msg 邮件内容
         * @return 需要回执返回true,否则返回false
         * @throws MessagingException
         */
        public static boolean isReplySign(MimeMessage msg) throws MessagingException {
            boolean replySign = false;
            String[] headers = msg.getHeader("Disposition-Notification-To");
            if (headers != null)
                replySign = true;
            return replySign;
        }
        /**
         * 获得邮件的优先级
         * @param msg 邮件内容
         * @return 1(High):紧急  3:普通(Normal)  5:低(Low)
         * @throws MessagingException
         */
        public static String getPriority(MimeMessage msg) throws MessagingException {
            String priority = "普通";
            String[] headers = msg.getHeader("X-Priority");
            if (headers != null) {
                String headerPriority = headers[0];
                if (headerPriority.contains("1") || headerPriority.contains("High"))
                    priority = "紧急";
                else if (headerPriority.contains("5") || headerPriority.contains("Low"))
                    priority = "低";
                else
                    priority = "普通";
            }
            return priority;
        }
        /**
         * 根据收件人类型,获取邮件收件人、抄送和密送地址。如果收件人类型为空,则获得所有的收件人
         * <p>Message.RecipientType.TO  收件人</p>
         * <p>Message.RecipientType.CC  抄送</p>
         * <p>Message.RecipientType.BCC 密送</p>
         * @param msg 邮件内容
         * @param type 收件人类型
         * @return 收件人1 <邮件地址1>, 收件人2 <邮件地址2>, ...
         * @throws MessagingException
         */
        public static String getReceiveAddress(MimeMessage msg, Message.RecipientType type) throws MessagingException {
            StringBuilder receiveAddress = new StringBuilder();
            Address[] addresss;
            if (type == null) {
                addresss = msg.getAllRecipients();
            } else {
                addresss = msg.getRecipients(type);
            }
    
            if (addresss == null || addresss.length < 1)
                throw new MessagingException("没有收件人!");
            for (Address address : addresss) {
                InternetAddress internetAddress = (InternetAddress)address;
                receiveAddress.append(internetAddress.toUnicodeString()).append(",");
            }
            receiveAddress.deleteCharAt(receiveAddress.length()-1); //删除最后一个逗号
            return receiveAddress.toString();
        }
    
        /**
         * 判断邮件中是否包含附件
         *
         * @return 存在附件返回true,不存在返回false
         */
        public static boolean isContainAttachment(Part part) throws Exception {
            boolean flag = false;
            if (part.isMimeType("multipart/*")) {
                MimeMultipart multipart = (MimeMultipart) part.getContent();
                int partCount = multipart.getCount();
                for (int i = 0; i < partCount; i++) {
                    BodyPart bodyPart = multipart.getBodyPart(i);
                    String disp = bodyPart.getDisposition();
                    if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
                        flag = true;
                    } else if (bodyPart.isMimeType("multipart/*")) {
                        flag = isContainAttachment(bodyPart);
                    } else {
                        String contentType = bodyPart.getContentType();
                        if (contentType.contains("application")) {
                            flag = true;
                        }
    
                        if (contentType.contains("name")) {
                            flag = true;
                        }
                    }
    
                    if (flag) break;
                }
            } else if (part.isMimeType("message/rfc822")) {
                flag = isContainAttachment((Part) part.getContent());
            }
            return flag;
        }
    
        /**
         * 保存文件
         *
         * @param destDir  文件目录
         * @param fileName 文件名
         * @throws Exception 异常
         */
        public static void saveAttachment(Part part, String destDir, String fileName) throws Exception {
            if (part.isMimeType("multipart/*")) {
                //复杂体邮件
                Multipart multipart = (Multipart) part.getContent();
                //复杂体邮件包含多个邮件体
                int partCount = multipart.getCount();
                for (int i = 0; i < partCount; i++) {
                    //获得复杂体邮件中其中一个邮件体
                    BodyPart bodyPart = multipart.getBodyPart(i);
                    //迭代处理邮件体,直到附件为止
                    String disp = bodyPart.getDisposition();
                    String decodeName = decodeText(bodyPart.getFileName());
                    decodeName = StringUtils.isEmpty(decodeName) ? fileName : decodeName;
                    if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
                        saveFile(bodyPart.getInputStream(), destDir, decodeName);
                    } else if (bodyPart.isMimeType("multipart/*")) {
                        saveAttachment(bodyPart, destDir, fileName);
                    } else {
                        String contentType = bodyPart.getContentType();
                        if (contentType.contains("name") || contentType.contains("application")) {
                            saveFile(bodyPart.getInputStream(), destDir, decodeName);
                        }
                    }
                }
            } else if (part.isMimeType("message/rfc822")) {
                saveAttachment((Part) part.getContent(), destDir, fileName);
            }
        }
        /**
         * 获得邮件文本内容
         * @param part 邮件体
         * @param content 存储邮件文本内容的字符串
         * @throws MessagingException
         * @throws IOException
         */
        public static void getMailTextContent(Part part, StringBuffer content) throws MessagingException, IOException {
            //如果是文本类型的附件,通过getContent方法可以取到文本内容,但这不是我们需要的结果,所以在这里要做判断
            boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
            if (part.isMimeType("text/*") && !isContainTextAttach) {
                content.append(part.getContent().toString());
            } else if (part.isMimeType("message/rfc822")) {
                getMailTextContent((Part)part.getContent(),content);
            } else if (part.isMimeType("multipart/*")) {
                Multipart multipart = (Multipart) part.getContent();
                int partCount = multipart.getCount();
                for (int i = 0; i < partCount; i++) {
                    BodyPart bodyPart = multipart.getBodyPart(i);
                    getMailTextContent(bodyPart,content);
                }
            }
        }
    
        /**
         * 读取输入流中的数据保存至指定目录
         *
         * @param is       输入流
         * @param fileName 文件名
         * @param destDir  文件存储目录
         */
        private static void saveFile(InputStream is, String destDir, String fileName)
                throws Exception {
            createEmptyDirectory(destDir);
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(destDir + fileName)));
            int len;
            while ((len = bis.read()) != -1) {
                bos.write(len);
                bos.flush();
            }
            bos.close();
            bis.close();
        }
    
        /**
         * 创建一个空目录
         */
        public static void createEmptyDirectory(String directoryPath) {
            File file = new File(directoryPath);
            if (!file.exists()) {
                file.mkdirs();
            }
        }
    
        /**
         * 文本解码
         */
        public static String decodeText(String encodeText) throws Exception {
            if (encodeText == null || "".equals(encodeText)) {
                return "";
            } else {
                return MimeUtility.decodeText(encodeText);
            }
        }
    }
    
    
    

    3,测试结果

    imap测试结果:

    在这里插入图片描述
    pop测试结果

    没限制查询条件结果
    在这里插入图片描述
    限制条件查询结果
    在这里插入图片描述

    4,存在问题

    存在一个问题,就是在邮箱中已经设置只接收近30天的邮件,但是在imap方式中并没有生效,而pop方式是生效了,为什么imap的方式不生效呢?待解决
    在这里插入图片描述

    在这里插入图片描述

    5,参考文档

    [https://blog.csdn.net/qq_39006919/article/details/109115343]
    https://blog.csdn.net/hj7jay/article/details/84062650

    展开全文
  • java读取邮件

    2021-02-12 18:19:51
    importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.UnsupportedEncodingException;importjava.util.Pro...

    packagecom.zz.mail;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.UnsupportedEncodingException;importjava.util.Properties;importjavax.mail.Address;importjavax.mail.Folder;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.Multipart;importjavax.mail.NoSuchProviderException;importjavax.mail.Part;importjavax.mail.Session;importjavax.mail.Store;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeUtility;/*** 邮件接受测试

    **/

    public classPOP3MailReceiverTest {publicPOP3MailReceiverTest() {try{//1. 设置连接信息, 生成一个 Session

    String protocol = "pop3";boolean isSSL = true;

    String host= "pop.163.com";int port = 995;

    String username= "xxxx@163.com";

    String password= "****";

    Properties props= newProperties();

    props.put("mail.pop3.ssl.enable", isSSL);

    props.put("mail.pop3.host", host);

    props.put("mail.pop3.port", port);

    Session session=Session.getDefaultInstance(props);//2. 获取 Store 并连接到服务器

    Store store =session.getStore(protocol);

    store.connect(username, password);

    Folder folder= store.getDefaultFolder();//默认父目录

    if (folder == null) {

    System.out.println("服务器不可用");return;

    }/*System.out.println("默认信箱名:" + folder.getName());

    Folder[] folders = folder.list();// 默认目录列表

    for(int i = 0; i < folders.length; i++) {

    System.out.println(folders[0].getName());

    }

    System.out.println("默认目录下的子目录数: " + folders.length);*/Folder popFolder= folder.getFolder("INBOX");//获取收件箱

    popFolder.open(Folder.READ_WRITE);//可读邮件,可以删邮件的模式打开目录//4. 列出来收件箱 下所有邮件

    Message[] messages =popFolder.getMessages();//取出来邮件数

    int msgCount =popFolder.getMessageCount();

    System.out.println("共有邮件: " + msgCount + "封");//FetchProfile fProfile = new FetchProfile();//选择邮件的下载模式,//根据网速选择不同的模式//fProfile.add(FetchProfile.Item.ENVELOPE);//folder.fetch(messages, fProfile);//选择性的下载邮件//5. 循环处理每个邮件并实现邮件转为新闻的功能

    for (int i = 0; i < msgCount; i++) {//单个邮件

    System.out.println("第" + i +"邮件开始");

    mailReceiver(messages[i]);

    System.out.println("第" + i +"邮件结束");//邮件读取用来校验

    messages[i].writeTo(new FileOutputStream("D:/pop3MailReceiver"+ i +".eml"));

    }//7. 关闭 Folder 会真正删除邮件, false 不删除

    popFolder.close(true);//8. 关闭 store, 断开网络连接

    store.close();

    }catch(NoSuchProviderException e) {//TODO Auto-generated catch block

    e.printStackTrace();

    }catch(Exception e) {//TODO Auto-generated catch block

    e.printStackTrace();

    }

    }/*** 解析邮件

    *

    *@parammessages 邮件对象

    *@parami

    *@return*@throwsIOException

    *@throwsMessagingException

    *@throwsFileNotFoundException

    *@throwsUnsupportedEncodingException*/

    private void mailReceiver(Message msg)throwsException{//发件人信息

    Address[] froms =msg.getFrom();if(froms != null) {//System.out.println("发件人信息:" + froms[0]);

    InternetAddress addr = (InternetAddress)froms[0];

    System.out.println("发件人地址:" +addr.getAddress());

    System.out.println("发件人显示名:" +addr.getPersonal());

    }

    System.out.println("邮件主题:" +msg.getSubject());//getContent() 是获取包裹内容, Part相当于外包装

    Object o =msg.getContent();if(o instanceofMultipart) {

    Multipart multipart=(Multipart) o ;

    reMultipart(multipart);

    }else if (o instanceofPart){

    Part part=(Part) o;

    rePart(part);

    }else{

    System.out.println("类型" +msg.getContentType());

    System.out.println("内容" +msg.getContent());

    }

    }/***@parampart 解析内容

    *@throwsException*/

    private void rePart(Part part) throwsMessagingException,

    UnsupportedEncodingException, IOException, FileNotFoundException {if (part.getDisposition() != null) {

    String strFileNmae= MimeUtility.decodeText(part.getFileName()); //MimeUtility.decodeText解决附件名乱码问题

    System.out.println("发现附件: " +MimeUtility.decodeText(part.getFileName()));

    System.out.println("内容类型: " +MimeUtility.decodeText(part.getContentType()));

    System.out.println("附件内容:" +part.getContent());

    InputStream in= part.getInputStream();//打开附件的输入流//读取附件字节并存储到文件中

    java.io.FileOutputStream out = newFileOutputStream(strFileNmae);intdata;while((data = in.read()) != -1) {

    out.write(data);

    }

    in.close();

    out.close();

    }else{if(part.getContentType().startsWith("text/plain")) {

    System.out.println("文本内容:" +part.getContent());

    }else{//System.out.println("HTML内容:" + part.getContent());

    }

    }

    }/***@parammultipart // 接卸包裹(含所有邮件内容(包裹+正文+附件))

    *@throwsException*/

    private void reMultipart(Multipart multipart) throwsException {//System.out.println("邮件共有" + multipart.getCount() + "部分组成");//依次处理各个部分

    for (int j = 0, n = multipart.getCount(); j < n; j++) {//System.out.println("处理第" + j + "部分");

    Part part = multipart.getBodyPart(j);//解包, 取出 MultiPart的各个部分, 每部分可能是邮件内容,//也可能是另一个小包裹(MultipPart)//判断此包裹内容是不是一个小包裹, 一般这一部分是 正文 Content-Type: multipart/alternative

    if (part.getContent() instanceofMultipart) {

    Multipart p= (Multipart) part.getContent();//转成小包裹//递归迭代

    reMultipart(p);

    }else{

    rePart(part);

    }

    }

    }/***@paramargs*/

    public static voidmain(String[] args) {newPOP3MailReceiverTest();

    }

    }

    展开全文
  • (1)在设置里面打开163邮箱的smtp、pop3,拿到授权码 (2)在引用pom.xml中引用mail.jar com.sun.mail javax.mail 1.5.4 (3)邮件内容不要乱写,否则失败 public class TestJava { public static void ...

    1,发送邮件

    (1)在设置里面打开163邮箱的smtp、pop3,设置授权码

    (2)在引用pom.xml中引用mail.jar

    <dependency >  
          <groupId >com.sun.mail </groupId >  
          <artifactId >javax.mail </artifactId >  
          <version >1.5.4 </version >  
    </dependency >
    (3)邮件内容不要乱写,否则失败


    public class TestJava {
    	public static void main(String[] args) throws MessagingException {
    		Properties prop=new Properties();
    		prop.put("mail.host","smtp.163.com" );
    		prop.put("mail.transport.protocol", "smtp");
    		prop.put("mail.smtp.auth", true);
    		//使用java发送邮件5步骤
    		//1.创建sesssion
    		Session session=Session.getInstance(prop);
    		//开启session的调试模式,可以查看当前邮件发送状态
    		session.setDebug(true);
    
    		//2.通过session获取Transport对象(发送邮件的核心API)
    		Transport ts=session.getTransport();
    		//3.通过邮件用户名密码链接
    		ts.connect("邮箱名", "授权码");
    
    		//4.创建邮件
    		Message msg=createSimpleMail(session);
    
    		//5.发送电子邮件
    		ts.sendMessage(msg, msg.getAllRecipients());
    	}
    	
    	public static MimeMessage createSimpleMail(Session session) throws AddressException,MessagingException{
    		//创建邮件对象
    		MimeMessage mm=new MimeMessage(session);
    		//设置发件人
    		mm.setFrom(new InternetAddress(""));
    		//设置收件人
    		mm.setRecipient(Message.RecipientType.TO, new InternetAddress(""));
    		//设置抄送人
    		mm.setRecipient(Message.RecipientType.CC, new InternetAddress(""));
    
    		mm.setSubject("第一封JAVA邮件!");
    		mm.setContent("咱们开会吧", "text/html;charset=utf-8");
    
    		return mm;
    	}
    
    
    }
    代码引用:http://www.cnblogs.com/tptptptp/p/5874538.html



    2,读取邮件

    import java.util.Properties;
    
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    
    import com.sun.mail.imap.IMAPFolder;
    import com.sun.mail.imap.IMAPStore;
    
    /**
     *  使用IMAP协议接收邮件
     * @author hj
     * @time 2017-07-20
     */
    public class ReciveIMAPmail {
    	public static void main(String[] args) {
            String imapserver = "imap.163.com"; // 邮件服务器
            String user = "用户名";
            String password = "授权码";     // 根据自已的密码修改
            // 获取默认会话
            Properties prop = System.getProperties();
            prop.put("mail.imap.host",imapserver);
           
            prop.put("mail.imap.auth.plain.disable","true");
            Session mailsession=Session.getInstance(prop,null);
            mailsession.setDebug(false); //是否启用debug模式
            IMAPFolder folder= null;
            IMAPStore store=null;
            int total= 0;
            try{
               store=(IMAPStore)mailsession.getStore("imap");  // 使用imap会话机制,连接服务器
               store.connect(imapserver,user,password);
               folder=(IMAPFolder)store.getFolder("INBOX"); //收件箱 
               // 使用只读方式打开收件箱 
               folder.open(Folder.READ_WRITE);
               //获取总邮件数
               total = folder.getMessageCount();
               System.out.println("-----------------您的邮箱共有邮件:" + total+" 封--------------");
               // 得到收件箱文件夹信息,获取邮件列表
               Message[] msgs =folder.getMessages();
               System.out.println("\t收件箱的总邮件数:" + msgs.length);  
               System.out.println("\t未读邮件数:" + folder.getUnreadMessageCount());  
               System.out.println("\t新邮件数:" + folder.getNewMessageCount());  
               System.out.println("----------------End------------------");
            }
            catch(MessagingException ex){
                 System.err.println("不能以读写方式打开邮箱!");
                 ex.printStackTrace();
            }finally {
            // 释放资源
              try{
                  if(folder!=null)
                      folder.close(true); //退出收件箱时,删除做了删除标识的邮件
                 if (store != null)
                    store.close();
                }catch(Exception bs){
                 bs.printStackTrace();
                }             
            }          
        }
    }
    

    可能会报错:

    javax.mail.MessagingException: A3 NO SELECT Unsafe Login. Please contact kefu@188.com for help;

      nested exception is:

    然后会收到邮件,打开


    然后 输入 http://config.mail.163.com/settings/imap/index.jsp?uid=xxxxxx@163.com

    进行手机验证就OK;



    展开全文
  • 老师要求让获取邮箱邮件内容,发件人、收件人、发送时间等等。转存到excel里面。并下载邮件带的附件,通过网上搜集资料,整理出例如以下代码,仅仅是实现功能。代码并未优化。使用的时候仅仅须要填写自己邮箱账号...

    老师要求让获取邮箱邮件内容,发件人、收件人、发送时间等等。转存到excel里面。并下载邮件带的附件,通过网上搜集资料,整理出例如以下代码,仅仅是实现功能。代码并未优化。

    使用的时候仅仅须要填写自己邮箱账号password就可以(须要源代码的能够留下邮箱)

    以下贴出代码,

    package ltg.defualt;

    import java.io.File;

    import java.io.FileOutputStream;

    import java.io.OutputStream;

    import ltg.helper.POP3ReceiveMailTest;

    import ltg.helper.helpHandler;

    public class test {

    /**

    * @param args

    */

    public static void main(String[] args) {

    try

    {

    //输出Excel

    File fileWrite = new File("d:/testWrite.xls");

    fileWrite.createNewFile();

    OutputStream osq = new FileOutputStream(fileWrite);

    //helpHandler.writeExcel(osq);

    helpHandler handler=new helpHandler(osq);

    handler.receive();

    }

    catch(Exception e)

    {

    e.printStackTrace();

    }

    }

    }获取的内容就会存在d盘的testWrite.xls

    public class helpHandler {

    static OutputStream osq;

    static WritableWorkbook wwb;

    static WritableSheet ws;

    static String array[] = { "主题", "发件人", "收件人", "发送时间", "是否已读", "邮件优先级",

    "是否须要回执", "邮件大小", "是否包括附件", "邮件正文" };

    public helpHandler(OutputStream os) throws IOException {

    this.osq = os;

    }

    /**

    * 接收邮件

    */

    public static void receive() throws Exception {

    // 准备连接server的会话信息

    Properties props = new Properties();

    props.setProperty("mail.store.protocol", "pop3"); // 协议

    props.setProperty("mail.pop3.port", "110"); // 端口

    props.setProperty("mail.pop3.host", "pop3.163.com"); // pop3server

    // 创建Session实例对象

    Session session = Session.getInstance(props);

    Store store = session.getStore("pop3");

    store.connect("163邮箱账号", "password");

    // 获得收件箱

    Folder folder = store.getFolder("INBOX");

    /*

    * Folder.READ_ONLY:仅仅读权限 Folder.READ_WRITE:可读可写(能够改动邮件的状态)

    */

    folder.open(Folder.READ_WRITE); // 打开收件箱

    // 因为POP3协议无法获知邮件的状态,所以getUnreadMessageCount得到的是收件箱的邮件总数

    System.out.println("未读邮件数: " + folder.getUnreadMessageCount());

    // 因为POP3协议无法获知邮件的状态,所以以下得到的结果始终都是为0

    System.out.println("删除邮件数: " + folder.getDeletedMessageCount());

    System.out.println("新邮件: " + folder.getNewMessageCount());

    // 获得收件箱中的邮件总数

    System.out.println("邮件总数: " + folder.getMessageCount());

    // 得到收件箱中的全部邮件,并解析

    Message[] messages = folder.getMessages();

    parseMessage(messages);

    // 释放资源

    folder.close(true);

    store.close();

    }

    /**

    * 解析邮件

    *

    * @param messages

    * 要解析的邮件列表

    * @throws WriteException

    * @throws RowsExceededException

    */

    public static void parseMessage(Message... messages)

    throws MessagingException, IOException, RowsExceededException,

    WriteException {

    if (messages == null || messages.length < 1)

    throw new MessagingException("未找到要解析的邮件!");

    try {

    wwb = Workbook.createWorkbook(osq);

    // 创建Excel工作表 指定名称和位置

    ws = wwb.createSheet("Test Sheet 1", 0);

    // **************往工作表中加入数据*****************

    // "未读邮件数", "删除邮件数", "新邮件", "邮件总数",

    // 写入标题

    for (int i = 0; i < array.length; i++) {

    Label labe1 = new Label(i, 0, array[i]);

    ws.addCell(labe1);

    }

    // 解析全部邮件

    for (int i = 0, count = messages.length; i < count; i++) {

    MimeMessage msg = (MimeMessage) messages[i];

    System.out.println("------------------解析第"

    + msg.getMessageNumber() + "封邮件-------------------- ");

    boolean b1 = POP3ReceiveMailTest.isSeen(msg);

    boolean isContainerAttachment = POP3ReceiveMailTest

    .isContainAttachment(msg);

    boolean b2=POP3ReceiveMailTest.isReplySign(msg);

    String str1 = "";

    String str2 = "";

    String str3 = "";

    if (b1)

    str1 = "是";

    else

    str1 = "否";

    if (isContainerAttachment)

    str3 = "是";

    else

    str3 = "否";

    if (b2)

    str2 = "是";

    else

    str2 = "否";

    StringBuffer content = new StringBuffer(30);

    POP3ReceiveMailTest.getMailTextContent(msg, content);

    String array1[] = { POP3ReceiveMailTest.getSubject(msg),

    POP3ReceiveMailTest.getFrom(msg),

    POP3ReceiveMailTest.getReceiveAddress(msg, null),

    POP3ReceiveMailTest.getSentDate(msg, null), str1,

    POP3ReceiveMailTest.getPriority(msg), str2,

    msg.getSize() * 1024 + "kb", str3, content.toString() };

    str1 = "";

    str2 = "";

    str3 = "";

    for (int j = 0; j < array1.length; j++) {

    Label labe2 = new Label(j, i + 1, array1[j]);

    ws.addCell(labe2);

    }

    System.out

    .println("主题: " + POP3ReceiveMailTest.getSubject(msg));

    System.out.println("发件人: " + POP3ReceiveMailTest.getFrom(msg));

    System.out.println("收件人:"

    + POP3ReceiveMailTest.getReceiveAddress(msg, null));

    System.out.println("发送时间:"

    + POP3ReceiveMailTest.getSentDate(msg, null));

    System.out.println("是否已读:" + POP3ReceiveMailTest.isSeen(msg));

    System.out.println("邮件优先级:"

    + POP3ReceiveMailTest.getPriority(msg));

    System.out.println("是否须要回执:"

    + POP3ReceiveMailTest.isReplySign(msg));

    System.out.println("邮件大小:" + msg.getSize() * 1024 + "kb");

    System.out.println("是否包括附件:" + isContainerAttachment);

    if (isContainerAttachment) {

    POP3ReceiveMailTest.saveAttachment(msg, "d:\\mailtmp\\"

    + msg.getSubject() + "_"); // 保存附件

    }

    // StringBuffer content = new StringBuffer(30);

    // POP3ReceiveMailTest.getMailTextContent(msg, content);

    // System.out.println("邮件正文:" + (content.length() > 100 ?

    // content.substring(0,100) + "..." : content));

    System.out.println("邮件正文:" + content);

    System.out.println("------------------第"

    + msg.getMessageNumber()

    + "封邮件解析结束-------------------- ");

    System.out.println();

    }

    // 写入工作表

    wwb.write();

    wwb.close();

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }String array[] = { "主题", "发件人", "收件人", "发送时间", "是否已读", "邮件优先级", "是否须要回执", "邮件大小", "是否包括附件", "邮件正文" };这是excel表的字段  顺序不可变 要和以下的顺序一致。下载的相关文档和图片在D盘的mailtmp目录下

    package ltg.helper;

    import java.io.BufferedInputStream;

    import java.io.BufferedOutputStream;

    import java.io.File;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.UnsupportedEncodingException;

    import java.text.SimpleDateFormat;

    import java.util.Date;

    import java.util.Properties;

    import javax.mail.Address;

    import javax.mail.BodyPart;

    import javax.mail.Flags;

    import javax.mail.Folder;

    import javax.mail.Message;

    import javax.mail.MessagingException;

    import javax.mail.Multipart;

    import javax.mail.Part;

    import javax.mail.Session;

    import javax.mail.Store;

    import javax.mail.internet.InternetAddress;

    import javax.mail.internet.MimeMessage;

    import javax.mail.internet.MimeMultipart;

    import javax.mail.internet.MimeUtility;

    /**

    * 使用POP3协议接收邮件

    */

    public class POP3ReceiveMailTest {

    /**

    * 获得邮件主题

    *

    * @param msg

    * 邮件内容

    * @return 解码后的邮件主题

    */

    public static String getSubject(MimeMessage msg)

    throws UnsupportedEncodingException, MessagingException {

    return MimeUtility.decodeText(msg.getSubject());

    }

    /**

    * 获得邮件发件人

    *

    * @param msg

    * 邮件内容

    * @return 姓名

    * @throws MessagingException

    * @throws UnsupportedEncodingException

    */

    public static String getFrom(MimeMessage msg) throws MessagingException,

    UnsupportedEncodingException {

    String from = "";

    Address[] froms = msg.getFrom();

    if (froms.length < 1)

    throw new MessagingException("没有发件人!");

    InternetAddress address = (InternetAddress) froms[0];

    String person = address.getPersonal();

    if (person != null) {

    person = MimeUtility.decodeText(person) + " ";

    } else {

    person = "";

    }

    from = person + "";

    return from;

    }

    /**

    * 依据收件人类型。获取邮件收件人、抄送和密送地址。

    假设收件人类型为空,则获得全部的收件人

    *

    * Message.RecipientType.TO 收件人

    *

    *

    * Message.RecipientType.CC 抄送

    *

    *

    * Message.RecipientType.BCC 密送

    *

    *

    * @param msg

    * 邮件内容

    * @param type

    * 收件人类型

    * @return 收件人1 , 收件人2 , ...

    * @throws MessagingException

    */

    public static String getReceiveAddress(MimeMessage msg,

    Message.RecipientType type) throws MessagingException {

    StringBuffer receiveAddress = new StringBuffer();

    Address[] addresss = null;

    if (type == null) {

    addresss = msg.getAllRecipients();

    } else {

    addresss = msg.getRecipients(type);

    }

    if (addresss == null || addresss.length < 1)

    throw new MessagingException("没有收件人!");

    for (Address address : addresss) {

    InternetAddress internetAddress = (InternetAddress) address;

    receiveAddress.append(internetAddress.toUnicodeString())

    .append(",");

    }

    receiveAddress.deleteCharAt(receiveAddress.length() - 1); // 删除最后一个逗号

    return receiveAddress.toString();

    }

    /**

    * 获得邮件发送时间

    *

    * @param msg

    * 邮件内容

    * @return yyyy年mm月dd日 星期X HH:mm

    * @throws MessagingException

    */

    public static String getSentDate(MimeMessage msg, String pattern)

    throws MessagingException {

    Date receivedDate = msg.getSentDate();

    if (receivedDate == null)

    return "";

    if (pattern == null || "".equals(pattern))

    pattern = "yyyy年MM月dd日 E HH:mm ";

    return new SimpleDateFormat(pattern).format(receivedDate);

    }

    /**

    * 推断邮件中是否包括附件

    *

    * @param msg

    * 邮件内容

    * @return 邮件中存在附件返回true。不存在返回false

    * @throws MessagingException

    * @throws IOException

    */

    public static boolean isContainAttachment(Part part)

    throws MessagingException, IOException {

    boolean flag = false;

    if (part.isMimeType("multipart/*")) {

    MimeMultipart multipart = (MimeMultipart) part.getContent();

    int partCount = multipart.getCount();

    for (int i = 0; i < partCount; i++) {

    BodyPart bodyPart = multipart.getBodyPart(i);

    String disp = bodyPart.getDisposition();

    if (disp != null

    && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp

    .equalsIgnoreCase(Part.INLINE))) {

    flag = true;

    } else if (bodyPart.isMimeType("multipart/*")) {

    flag = isContainAttachment(bodyPart);

    } else {

    String contentType = bodyPart.getContentType();

    if (contentType.indexOf("application") != -1) {

    flag = true;

    }

    if (contentType.indexOf("name") != -1) {

    flag = true;

    }

    }

    if (flag)

    break;

    }

    } else if (part.isMimeType("message/rfc822")) {

    flag = isContainAttachment((Part) part.getContent());

    }

    return flag;

    }

    /**

    * 推断邮件是否已读

    *

    * @param msg

    * 邮件内容

    * @return 假设邮件已读返回true,否则返回false

    * @throws MessagingException

    */

    public static boolean isSeen(MimeMessage msg) throws MessagingException {

    return msg.getFlags().contains(Flags.Flag.SEEN);

    }

    /**

    * 推断邮件是否须要阅读回执

    *

    * @param msg

    * 邮件内容

    * @return 须要回执返回true,否则返回false

    * @throws MessagingException

    */

    public static boolean isReplySign(MimeMessage msg)

    throws MessagingException {

    boolean replySign = false;

    String[] headers = msg.getHeader("Disposition-Notification-To");

    if (headers != null)

    replySign = true;

    return replySign;

    }

    /**

    * 获得邮件的优先级

    *

    * @param msg

    * 邮件内容

    * @return 1(High):紧急 3:普通(Normal) 5:低(Low)

    * @throws MessagingException

    */

    public static String getPriority(MimeMessage msg) throws MessagingException {

    String priority = "普通";

    String[] headers = msg.getHeader("X-Priority");

    if (headers != null) {

    String headerPriority = headers[0];

    if (headerPriority.indexOf("1") != -1

    || headerPriority.indexOf("High") != -1)

    priority = "紧急";

    else if (headerPriority.indexOf("5") != -1

    || headerPriority.indexOf("Low") != -1)

    priority = "低";

    else

    priority = "普通";

    }

    return priority;

    }

    /**

    * 获得邮件文本内容

    *

    * @param part

    * 邮件体

    * @param content

    * 存储邮件文本内容的字符串

    * @throws MessagingException

    * @throws IOException

    */

    public static void getMailTextContent(Part part, StringBuffer content)

    throws MessagingException, IOException {

    // 假设是文本类型的附件,通过getContent方法能够取到文本内容,但这不是我们须要的结果。所以在这里要做推断

    boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;

    if (part.isMimeType("text/*") && !isContainTextAttach) {

    content.append(part.getContent().toString());

    } else if (part.isMimeType("message/rfc822")) {

    getMailTextContent((Part) part.getContent(), content);

    } else if (part.isMimeType("multipart/*")) {

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

    int partCount = multipart.getCount();

    for (int i = 0; i < partCount - 1; i++) {

    BodyPart bodyPart = multipart.getBodyPart(i);

    getMailTextContent(bodyPart, content);

    }

    }

    }

    /**

    * 保存附件

    *

    * @param part

    * 邮件中多个组合体中的当中一个组合体

    * @param destDir

    * 附件保存文件夹

    * @throws UnsupportedEncodingException

    * @throws MessagingException

    * @throws FileNotFoundException

    * @throws IOException

    */

    public static void saveAttachment(Part part, String destDir)

    throws UnsupportedEncodingException, MessagingException,

    FileNotFoundException, IOException {

    if (part.isMimeType("multipart/*")) {

    Multipart multipart = (Multipart) part.getContent(); // 复杂体邮件

    // 复杂体邮件包括多个邮件体

    int partCount = multipart.getCount();

    for (int i = 0; i < partCount; i++) {

    // 获得复杂体邮件中当中一个邮件体

    BodyPart bodyPart = multipart.getBodyPart(i);

    // 某一个邮件体也有可能是由多个邮件体组成的复杂体

    String disp = bodyPart.getDisposition();

    if (disp != null

    && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp

    .equalsIgnoreCase(Part.INLINE))) {

    InputStream is = bodyPart.getInputStream();

    saveFile(is, destDir, decodeText(bodyPart.getFileName()));

    } else if (bodyPart.isMimeType("multipart/*")) {

    saveAttachment(bodyPart, destDir);

    } else {

    String contentType = bodyPart.getContentType();

    if (contentType.indexOf("name") != -1

    || contentType.indexOf("application") != -1) {

    saveFile(bodyPart.getInputStream(), destDir,

    decodeText(bodyPart.getFileName()));

    }

    }

    }

    } else if (part.isMimeType("message/rfc822")) {

    saveAttachment((Part) part.getContent(), destDir);

    }

    }

    /**

    * 读取输入流中的数据保存至指定文件夹

    *

    * @param is

    * 输入流

    * @param fileName

    * 文件名称

    * @param destDir

    * 文件存储文件夹

    * @throws FileNotFoundException

    * @throws IOException

    */

    private static void saveFile(InputStream is, String destDir, String fileName)

    throws FileNotFoundException, IOException {

    BufferedInputStream bis = new BufferedInputStream(is);

    BufferedOutputStream bos = new BufferedOutputStream(

    new FileOutputStream(new File(destDir + fileName)));

    int len = -1;

    while ((len = bis.read()) != -1) {

    bos.write(len);

    bos.flush();

    }

    bos.close();

    bis.close();

    }

    /**点击打开链接

    * 文本解码

    *

    * @param encodeText

    * 解码MimeUtility.encodeText(String text)方法编码后的文本

    * @return 解码后的文本

    * @throws UnsupportedEncodingException

    */

    public static String decodeText(String encodeText)

    throws UnsupportedEncodingException {

    if (encodeText == null || "".equals(encodeText)) {

    return "";

    } else {

    return MimeUtility.decodeText(encodeText);

    }

    }

    }

    须要导入两个jar包

    da3c633a84b1bf24bda93ff99198e659.png

    下载地址

    https://yunpan.cn/cqj2hyXEUf5fQ  訪问password 6acc

    https://yunpan.cn/cqj27LI3kjBbU  訪问password 3712

    展开全文
  • java读取邮箱邮件

    千次阅读 2019-07-16 15:15:58
    package org.yangxin.study.jm;...import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; im...
  • javamail实现的邮件发送功能,内含完整代码和jar包,邮件服务协议smtp,pop3,用javamail发送邮件,支持QQ,163邮件发送。相同邮件(QQ邮箱给QQ邮箱)和不同邮件(QQ邮箱给163邮箱)是不一样的。
  • 本文介绍两种邮件发送,一个是网易163邮箱,另一个是网易企业163邮箱 项目地址:https://github.com/su1573/mail-send 一、登录邮箱设置 网易163邮箱 1、登录个人163邮箱,如图选中POP3/SMTP/IMAP 2、开启POP3/...
  • 1、打开P0P3/SMTP/IMAP 2、开发工具类CodeUtils.javapackage ...import java.util.UUID;public class CodeUtils {public static String generateUniqueCode(){return UUID.randomUUID().toString().repla...
  • 1.创建一个用户类,来接收发送邮件邮箱地址和授权密码(需要注意的是,这里的密码不是正常使用邮箱的登陆密码,而是客户端生成的另一个专门的授权码 ,这里需要开启相应的发送邮件的服务,这里开启的是pop3/smtp...
  • java读取qq邮箱邮件

    2019-09-01 21:48:57
    说起获取邮箱邮件的时候我首选的是网易的,因为之前写了,网易的发邮件,想着再写一个收邮件那就完美了,可以网易的那个。http://config.mail.163.com/settings/imap/index.jsp?uid=xxxxxx@163.com不能使用。于是...
  • java实现163邮箱发送邮件到qq邮箱成功案例下载和上传附件、发送短信和发送邮件,都算是程序中很常用的功能,之前记录了文件的上传和下载还有发送短信,由于最近比较忙,邮件发送的功能就没有时间去弄,现在终于成功...
  • importjava.util.Properties;importjavax.activation.DataHandler;importjavax.activation.FileDataSource;importjavax.mail.*;importjavax.mail.internet.*;/*** 本程序用java来实现Email的发送,所用到的协议为:...
  • // message.setSubject("java邮件测试"); message.setSubject(subject); // 邮件容器 MimeMultipart mimeMultiPart = new MimeMultipart(); // 设置HTML BodyPart bodyPart = new ...
  • 通过Java读取QQ邮件邮件解析

    万次阅读 2016-10-18 11:58:25
    最近为了抓取QQ邮箱中部分邮件中的数据,网上找的资料基本都是基于163邮箱的代码,结合部分代码整理出获取QQ邮箱邮件的方式,记录并共享一下。 资源下载mail.jar(用于连接,获取邮件) jsoup.jar(用于解析html)基础...
  • javamail 读取163邮箱的垃圾邮件

    千次阅读 2013-07-10 22:12:14
    package com.canevent.util;import java.util.Properties;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Store;public c
  • 邮件集成java mail + 读取邮件模板

    千次阅读 2019-01-07 08:36:27
    读取邮件模板sendMail.vm文件。 1.邮件发送 import java.io.StringWriter; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport;...
  • Java读取邮件

    千次阅读 2019-11-01 11:05:42
    smtp协议可以用来发送邮件,IMAP协议可以用来读取邮件。QQ邮箱设置中开启POP3协议。发送邮件javaMail-发送邮件 二、代码实现 参看了一篇几年前的博客,拿过来进行了一些改造,只是一个基版,一些邮箱的东西什么可以...
  • 原本业务使用javamail通过imap协议读取邮件,后来有个客户那边不支持imap协议,需要改为通过微软的ews(exchange web service)协议读取。结合网上已有的资料,自己测试了一把,以下是可以成功运行的代码 1.maven...
  • 但这些应用不可能和我们自己平时发邮件一样,先打开浏览器,登录邮箱,创建邮件再发送。本文将简单介绍如何通过 Java 代码来创建电子邮件,并连接邮件服务器发送邮件。 电子邮件协议 电子邮件的在网络中传输和网页...
  • public class SendMailText_Picture_Enclosure { public static void main(String[] args) throws Exception { /** 登录邮箱 **/ String email = "xxxxxxxx@163.com"; /** 邮箱密码(授权码) **/ ...
  • 使用Hutool结合163邮箱发送邮件 进入163设置邮箱POP3/SMTP/IMAP 引入Hutool依赖 地址:https://www.hutool.cn/docs/#/extra/%E9%82%AE%E4%BB%B6%E5%B7%A5%E5%85%B7-MailUtil <dependency> <...
  • pop_conn.pass_(password) #Get messages from server: # 获得邮件 messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] #print messages #print "------------------------------------...
  • 可以简单实现邮件的收发功能,支持126.163邮箱,不支持qq邮箱,新注册的163邮箱不支持。自己修改过的程序代码,经测试能运行。注意:代码里需要改成自己的邮箱和密码就可以运行了!
  • Java使用QQ邮箱或网易163邮箱发送自定义CSS样式邮件前言一、战前准备1.导入依赖2.配置文件3.邮箱工具类二、战争开始1.QQ邮箱发送2.网易邮箱发送战后反思 前言 我们可能有时候项目中会有需求要给客户或者员工发送...
  • javamail定时读取邮箱的未读邮件(imap)

    万次阅读 热门讨论 2016-03-10 16:39:46
    步骤: 1、设置被读取邮箱的安全性,允许我们的系统读取邮件,例如:网易邮箱,当读取邮件的时候,网易的邮箱会受到一封标题为“网易邮箱提醒:阻止了一次不安全的收信请求“的邮件 , 第二部:读取邮
  • 邮件发送与接收,支持163邮箱、outlook邮箱、exchange邮箱 依赖的jar包 邮件收发公共服务层实现 package com.example.demo.service.impl; import com.example.demo.model.EmailMessageBO; import ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,609
精华内容 1,843
关键字:

java163邮箱读取邮件

java 订阅