javamail在linux上部署收不了邮件

发布于 2021-11-30 05:45:37 字数 11602 浏览 885 评论 5


项目开发用的是javamail做的邮件接收和发送功能

线下测试都可以(windows),但是到了linux环境下,不能接收邮件,

(在Linux环境下,写了个小测试类可以接收,就是跑在服务里不能接收)

debug信息时,小测试类会打印C:NOOP

但是服务里就不做C:NOOP操作,但是Message[]长度是1,因为有一封邮件



不知哪位大侠遇见过此种现象,多多指教!

下面是部分代码



类MailConfig
 public static Properties getReceivePro(){
        if(receivePro == null) {
            receivePro = new Properties();
        }
        // 协议 或者使用imap协议 可以支持是否为已读邮件之类的操作
//        receivePro.setProperty("mail.store.protocol", "pop3");
        // pop3服务器 公司应该使用自己的服务器地址就ok了
        receivePro.put("mail.pop3.host", getString("rec.pop.host"));
        // 端口 imap的端口为143
        receivePro.put("mail.pop3.port", getString("rec.port"));
        return receivePro;
    }

类ReceiveMail
public static Session getSession() {
        if(session == null) {
            Properties props = MailConfig.getReceivePro();
            
            session = Session.getDefaultInstance(props);
            log.info("mail session==" + session);
        }
        
        return session;
    }

public void receive(List<MailInfo> res, Date lastDate) {
        Session session = getSession();
        session.setDebug(true);
        Store store = null;
        Folder folder = null;
        try {
            
            store = session.getStore();
            
            store.connect("XXXXX@XXX","XXXX");
            
//            store.connect(MailConfig.getString("rec.pop.host") , 
//                    MailConfig.getString("fromMail") , 
//                    new String(Base64.decode(MailConfig.getString("fromMail.pwd"))));
            
            folder = store.getFolder("INBOX");//POP协议的话,这里只能是INBOX
            folder.open(Folder.READ_ONLY);//以只读方式打开收件箱
            
            parseMessages(folder.getMessages(), res, lastDate);
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                folder.close(false);
                store.close();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            
        }
    }

public void parseMessages(Message[] messages, List<MailInfo> res, Date lastDate) throws MessagingException, IOException {
		if(messages.length < 1 || messages == null) {
			log.info("没有邮件!!!!!");
		} else {
//			String[] fileNameArray = null;
//			MailInfo tmp = null;
			log.info(""+messages.length);
			for(int i = 0; i < messages.length; i++) {
				MimeMessage each = (MimeMessage) messages[i];
				
				log.info("------------------解析第" + each.getMessageNumber() + "封邮件-------------------- "); 
				log.info("each.getSubject()--" + each.getSubject());
				log.info("each.getSubject()--"+each.getFrom());
				log.info("each.getSize()--"+each.getSize());
				log.info("each.getFrom()--"+each.getFrom());
				log.info("each.getSentDate()--" + each.getSentDate());
				
				log.info("主题: " + getSubject(each)); 
				log.info("发件人: " + getFrom(each)); 
				log.info("收件人:" + getReceiveAddress(each, null)); 
				log.info("发送时间:" + getSentDate(each, ReceiveMail.DEFAULT_PATTERN)); 
				log.info("是否已读:" + isSeen(each)); 
				log.info("邮件优先级:" + getPriority(each)); 
				log.info("是否需要回执:" + isReplySign(each)); 
				log.info("邮件大小:" + each.getSize() + "b"); 
//				if(each.getSentDate().getTime() - lastDate.getTime() < 0) break;
				
	            boolean isContainerAttachment = isContainAttachment(each); 
	            log.info("是否包含附件:" + isContainerAttachment); 
	            if (isContainerAttachment) { 
	                saveAttachment(each, MailConfig.getString("recMailFilePath")); //保存附件 
	                
//	                fileNameArray = new String[fileNames.size()];
//					for(int j = 0;j < fileNames.size(); j++){
//						fileNameArray[j] = fileNames.get(j);
//					}
//	                
//	                tmp = new MailInfo();
//					tmp.setSubject(each.getSubject());
//					tmp.setContent(each.getContent().toString());
//					tmp.setSendDate(each.getSentDate());
//					tmp.setAttachFileNames(fileNameArray);
//					
//					res.add(tmp);
	            }  
//	            StringBuffer content = new StringBuffer(30); 
//	            getMailTextContent(each, content); 
//	            log.info("邮件正文:" + (content.length() > 100 ? content.substring(0,100) + "..." : content)); 
	            log.info("------------------第" + each.getMessageNumber() + "封邮件解析结束-------------------- "); 
	            fileNames.clear();
			}
		}
	}
到这里就不进行了。log.info("主题: " + getSubject(each)); 因为message的subject为空

本地测试,写的小程序都没问题,就是部到线上就不行。


错误日志(小测试):


javax.mail.Session@37a1c887
1
�ʼ�����1
δ���ʼ���1
ɾ���ʼ���0
���ʼ���0
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]
DEBUG POP3: connecting to host "pop.263xmail.com", port 110, isSSL false
S: +OK Welcome to XMAIL POP3 Server Version 0.0.1
C: USER XXXXXXX
S: +OK XMAIL POP3
C: PASS XXXXX
S: +OK XMAIL POP3
C: STAT
S: +OK 1 30944
C: NOOP
S: +OK XMAIL POP3
------------------������1���ʼ�-------------------- 
C: TOP 1 0
S: +OK
Received: from smtp.263xmail.com (localhost.localdomain [127.0.0.1])
	by smtp.263xmail.com (Postfix) with ESMTP id A30201F546
	for <onlineshipping_hf@dhgate.com>; Wed,  1 May 2013 19:45:05 +0800 (CST)
X-263anti-spam:KSV:0;


线上日志


2013-05-02 07:41:49,123 INFO  [com.dhgate.logistics.mail.ReceiveMail] mail session==javax.mail.Session@17f7be7b
Loading javamail.default.providers from jar:file:/usr/local/project/dhgate-logistics-service/dhgate-logistics-impl-1.0-SNAPSHOT/lib/mail-1.4.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: getProvider() returning provider protocol=pop3; type=javax.mail.Provider$Type@21e30857; class=com.sun.mail.pop3.POP3Store; vendor=Sun Microsystems, Inc
DEBUG POP3: connecting to host "pop.263xmail.com", port 110, isSSL false
S: +OK Welcome to XMAIL POP3 Server Version 0.0.1
C: USER XXXXX@XXXXX
S: +OK XMAIL POP3
C: PASS XXXXXX
S: +OK XMAIL POP3
C: STAT
S: +OK 1 30944
2013-05-02 07:41:49,328 INFO  [com.dhgate.logistics.mail.ReceiveMail] 1
2013-05-02 07:41:49,329 INFO  [com.dhgate.logistics.mail.ReceiveMail] null
2013-05-02 07:41:49,330 INFO  [com.dhgate.logistics.mail.ReceiveMail] ------------------解析第1封邮件-------------------- 
2013-05-02 07:41:49,330 INFO  [com.dhgate.logistics.mail.ReceiveMail] 
C: QUIT
S: +OK XMAIL POP3
java.lang.NullPointerException
    at javax.mail.internet.MimeUtility.decodeText(MimeUtility.java:124)
    at com.dhgate.logistics.mail.ReceiveMail.getSubject(ReceiveMail.java:467)
    at com.dhgate.logistics.mail.ReceiveMail.parseMessages(ReceiveMail.java:170)
    at com.dhgate.logistics.mail.ReceiveMail.receive(ReceiveMail.java:132)
    at com.dhgate.logistics.job.MailJob.receiveMail(MailJob.java:268)
    at com.dhgate.logistics.impl.MailJobServiceImpl.doJob(MailJobServiceImpl.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.dhgate.apsaras.rpc.jbossremoting.JBossRemotingRPCServer$WrapperHandler.invoke(JBossRemotingRPCServer.java:188)
    at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:809)
    at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:608)
    at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:406)
    at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:173)
2013-05-02 07:41:49,358 INFO  [com.dhgate.logistics.impl.MailJobServiceImpl] remote invoke ReceiveMailJob  end


在线抓狂等!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱的故事 2021-12-05 10:44:47

我也遇到了,楼主最后解决了吗

明月松间行 2021-12-05 02:51:13

无人知道!

哑剧 2021-12-04 09:05:54

也可以帮忙分析下,是需要linux做什么配置吗?

瀞厅☆埖开 2021-12-03 22:56:22

补充:当前服务是跑的java进程,java -server 方式启动的

发邮件可以,收邮件不可以,自己顶!!

柒夜笙歌凉 2021-11-30 17:34:00

ok了,pop协议不行,用imap协议就ok了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文