如何通过java代码找到空白主题(没有主题)的电子邮件? [英] How to find a blank subject(no subject) emails through java code?

查看:73
本文介绍了如何通过java代码找到空白主题(没有主题)的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过java代码阅读电子邮件并执行操作。但我无法对没有主题的电子邮件进行操作。也无法找到没有主题的电子邮件。



消息消息=消息[i];

String subject = message.getSubject()。 toString();

我应该检查没有主题的电子邮件?



我尝试了什么: < br $>


消息消息=消息[i];

字符串主题= message.getSubject()。toString();

I am trying to read emails through java code and performing operations. but i am unable to perform operation on no subject emails . Also unable to find emails with no subject.

Message message = messages[i];
String subject=message.getSubject().toString();
what should i check for no subject emails?

What I have tried:

Message message = messages[i];
String subject=message.getSubject().toString();

推荐答案

虽然主题不是必需的标题字段,但几乎所有邮件都有一个。如果您需要没有主题标题的邮件,只需创建一个(例如,通过将其发送到用作应用程序输入的地址)。但是可能有必要以编程方式创建/发送邮件而不是使用电子邮件客户端应用程序,因为它们具有主题的输入字段,因此可能始终包括标题,即使是空内容。



如何检测没有主题的邮件取决于 Message.getSubject()函数的实现。它可能返回一个空字符串,一个 null 指针,或抛出一个 MessagingException (我还没有找到一个明确的声明文档,但你应该能够找到处理没有主题标题的邮件时。)



在第一种情况下你无法检测到没有主题的邮件(你只能检测空主题字段)。在第三种情况下,你必须捕获异常。



处理所有情况的代码片段:

While the subject is not a required header field, nearly all mails will have one. If you need a mail without a subject header, just create one (e.g. by sending it to the address that is used as input for your application). But it might be necessary to create/send the mail programmatically instead of using an email client application because those have an input field for the subject and might therefore always include the header even with empty content.

How to detect a mail without subject depends on the implementation of the Message.getSubject() function. It may return an empty string, a null pointer, or throw a MessagingException (I have not found a clear statement in the documentation but you should be able to find out when processing a mail without the subject header).

In the first case you can't detect mails without subject (you can only detect empty subject fields). In the third case you have to catch the exception.

A code snippet handling all cases:
try {
    String subject=message.getSubject();
    if (subject == null)
    {
        // no subject (if null is returned for this case)
    }
    else if (subject.isEmpty())
    {
        // empty subject
    }
} catch (MessagingException me) {
    // Handle exception here
    // Invalid message or - if it applies - subject not present
}



最后没有必要调用 toString()因为 Message.getSubject()返回一个字符串。


Finally there is no need to call toString() because Message.getSubject() returns a string.


这篇关于如何通过java代码找到空白主题(没有主题)的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆