首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java邮件API转发邮件功能

java邮件API转发邮件功能
EN

Stack Overflow用户
提问于 2017-03-30 05:20:13
回答 1查看 1.9K关注 0票数 1

我正在开发一个独立的java实用程序,它将连接到任何组织的现有邮箱,并将从收件箱读取/检查邮件,如果任何邮件的地址字段(“to和CC”)超过了2000个字符的字符限制或不超过…,则从收件箱读取/检查邮件。.if,它超越了限制,然后它将转发相同的邮件,一个电子邮件id,否则将不会做任何….i编写了转发以下邮件功能的代码.它正在工作,good...mail正在被转发,但是它显示了一个异常…。。请建议我缺少…的地方。。

代码语言:javascript
复制
public class ForwardMail {

    public static void main(String[] args){
        Properties properties = new Properties();
          properties.put("mail.store.protocol", "pop3");
          properties.put("mail.pop3s.host", "mailcluster.mycompany.com");
          properties.put("mail.pop3s.port", "995");
          properties.put("mail.pop3.starttls.enable", "true");
          properties.put("mail.smtp.auth", "true"); /* may be commented later*/
          properties.put("smtp.starttls.enable", "true");
          properties.put("mail.smtp.host", "mailcluster.mycompany.com");
          properties.put("mail.smtp.port", "25");

        //  Session session = Session.getDefaultInstance(properties);
          Session session = Session.getInstance(properties,new javax.mail.Authenticator() {  
                protected PasswordAuthentication getPasswordAuthentication() {  
                    return new PasswordAuthentication("myname@mycompany.com","mypassword@123");  
                }  
              });
     try{
         // session.setDebug(true);
         // Get a Store object and connect to the current host
         Store store = session.getStore("pop3s");
         store.connect("mailcluster.mycompany.com", "myname@mycompany.com","mypassword@123");


         // Create a Folder object and open the folder
         Folder folder = store.getFolder("inbox");
         folder.open(Folder.READ_ONLY);
         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
         Message[] messages = folder.getMessages();
         if (messages.length != 0) {

         for (int i = 0, n = messages.length; i < n; i++) {
            Message message = messages[i];
            // Get all the information from the message
           // String from = InternetAddress.toString(message.getFrom());
            String from = InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
            if (from != null) {
               System.out.println("From:"  + from);
            }
            /*String replyTo = InternetAddress.toString(message.getReplyTo());
            if (replyTo != null) {
               System.out.println("Reply-to: " + replyTo);
            }*/
            String to = InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
            if (to != null) {
               System.out.println("To: " + to);
            }

            String subject = message.getSubject();
            if (subject != null) {
               System.out.println("Subject: " + subject);
            }
            /*Date sent = message.getSentDate();
            if (sent != null) {
               System.out.println("Sent: " + sent);
            }*/
            System.out.print("Do you want to reply [y/n] : ");
            String ans = reader.readLine();
            if ("Y".equals(ans) || "y".equals(ans)) {
               Message forward = new MimeMessage(session);
               // Fill in header
               forward.setRecipients(Message.RecipientType.TO,InternetAddress.parse(from));
               forward.setSubject("Fwd: " + message.getSubject());
               forward.setFrom(new InternetAddress(to));

               // Create the message part
               MimeBodyPart messageBodyPart = new MimeBodyPart();
               // Create a multipart message
               Multipart multipart = new MimeMultipart();
               // set content
               messageBodyPart.setContent(message, "message/rfc822");
               // Add part to multi part
               multipart.addBodyPart(messageBodyPart);
               // Associate multi-part with message
               forward.setContent(multipart);
               forward.saveChanges();
               // Transport.send(forward);
               // Send the message by authenticating the SMTP server
               // Create a Transport instance and call the sendMessage
               Transport t = session.getTransport("smtp");
               try {
                  //connect to the smpt server using transport instance
                  t.connect("myname@mycompany.com", "mypassword@123");
                  t.sendMessage(forward, forward.getAllRecipients());

               } finally {
                  t.close();
               }

               System.out.println("message forwarded successfully....");

            // close the store and folder objects
            folder.close(false);
            store.close();
            }// end if

         }// end for
   }// end if
     }   catch(Exception e) {
          e.printStackTrace();  
    }
        }
    }

以下是我得到的例外。

代码语言:javascript
复制
java.lang.IllegalStateException: Folder is not Open
at com.sun.mail.pop3.POP3Folder.checkOpen(POP3Folder.java:551)
at com.sun.mail.pop3.POP3Folder.getProtocol(POP3Folder.java:581)
at com.sun.mail.pop3.POP3Message.loadHeaders(POP3Message.java:606)
at com.sun.mail.pop3.POP3Message.getHeader(POP3Message.java:383)
at javax.mail.internet.MimeMessage.getAddressHeader(MimeMessage.java:701)
at javax.mail.internet.MimeMessage.getRecipients(MimeMessage.java:534)
at servion.ForwardMail.main(ForwardMail.java:56)

如何删除此异常?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-30 06:38:54

在处理一条消息之后,您将关闭该文件夹,这意味着当您访问第二条消息的消息对象时,它已经关闭,因此JavaMail无法从服务器获取消息数据。

当您正在修复这个问题时,一定要修复这些常见JavaMail错误

最后,"smtp.starttls.enable"属性应该是"mail.smtp.starttls.enable"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43109136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档