问题:在Java中使用Microsoft Exchange选择收件箱中转发的邮件

huangapple go评论65阅读模式
英文:

Issues Picking Forwarded Mail from Inbox using Microsoft Exchange in Java

问题

I have a working code that connects to Microsoft Outlook and fetches mail based on some filters. It is working fine with reading the direct mail but not able to pick the same mail if forwarded. Any help is appreciated.

List<SearchFilter> searchFilterCollection = new ArrayList<>();
searchFilterCollection.add(new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, localDate));

// Flag to pick only email which contains attachments
searchFilterCollection.add(new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, Boolean.TRUE));
List<MetaInfoDTO> filterList = channel.getFilters();

// For each channel
log.info("Email from: {}", definedChannelFilter.getFieldData());
EmailAddress manager = new EmailAddress("abcd@outlook.com");
SearchFilter.IsEqualTo fromManagerFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, manager);
searchFilterCollection.add(fromManagerFilter);
log.info("Email Subject: {}", definedChannelFilter.getFieldData());
searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Subject ASDF"));
log.info("Email Body Content: {}", definedChannelFilter.getFieldData());
searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Body, "Body Content if any"));

return new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);
英文:

I have a working code that connects to Microsoft outlook and fetches Mail based on some filters.
It is working fine with reading the direct mail but not able to pick the same mail if forwarded.
Any Help is appreciated.

    List&lt;SearchFilter&gt; searchFilterCollection = new ArrayList&lt;&gt;();
	searchFilterCollection.add(new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived,localDate));

	// flag to pick only email which contains attachments
	searchFilterCollection.add(new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, Boolean.TRUE));
	List&lt;MetaInfoDTO&gt; filterList = channel.getFilters();
	// for each channel
	log.info(&quot;Email from: {}&quot;, definedChannelFilter.getFieldData());
	EmailAddress manager = new EmailAddress(&quot;abcd@outlook.com&quot;);
	SearchFilter.IsEqualTo fromManagerFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, manager);
	searchFilterCollection.add(fromManagerFilter);
	log.info(&quot;Email Subject: {}&quot;, definedChannelFilter.getFieldData());
	searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Subject,&quot;Subject ASDF&quot;));
	log.info(&quot;Email Body Content: {}&quot;, definedChannelFilter.getFieldData());
	searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Body,&quot;Body Content if any&quot;));
	
	return new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);		

答案1

得分: 1

以下是翻译好的代码部分:

private static void readAttachmentEmail(ExchangeService service) throws Exception {
		// 绑定到收件箱。
		Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
		ItemView view = new ItemView(5);
		List<SearchFilter> searchFilterCollection = new ArrayList<>();
	    // 标志以仅选择带有附件的电子邮件
	    searchFilterCollection.add(new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, Boolean.TRUE));
	    // 对于每个渠道
	    EmailAddress manager = new EmailAddress("manager@email.com");
	    SearchFilter.IsEqualTo fromManagerFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, manager);
	    searchFilterCollection.add(fromManagerFilter);
	    //searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Subject,"Subject ASDF"));
	    //searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Body,"Body Content if any"));
	    SearchFilter finalSearchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);
	    service.findItems(inbox.getId(), finalSearchFilter, view).forEach(item->{
	    	try {
				System.out.println("id==========" + item.getId());
				System.out.println("sub==========" + item.getSubject());
			} catch (ServiceLocalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    });
	}
英文:

You must revisit filters in your code.

Here is my code to read attached emails and I am able to print forwarded emails with attachments.

private static void readAttachmentEmail(ExchangeService service) throws Exception {
		// Bind to the Inbox.
		Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
		ItemView view = new ItemView(5);
		List&lt;SearchFilter&gt; searchFilterCollection = new ArrayList&lt;&gt;();
	    // flag to pick only email which contains attachments
	    searchFilterCollection.add(new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, Boolean.TRUE));
	    // for each channel
	    EmailAddress manager = new EmailAddress(&quot;manager@email.com&quot;);
	    SearchFilter.IsEqualTo fromManagerFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, manager);
	    searchFilterCollection.add(fromManagerFilter);
	    //searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Subject,&quot;Subject ASDF&quot;));
	    //searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Body,&quot;Body Content if any&quot;));
	    SearchFilter finalSearchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);
	    service.findItems(inbox.getId(), finalSearchFilter, view).forEach(item-&gt;{
	    	try {
				System.out.println(&quot;id==========&quot; + item.getId());
				System.out.println(&quot;sub==========&quot; + item.getSubject());
			} catch (ServiceLocalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    });
	}

huangapple
  • 本文由 发表于 2020年4月10日 22:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61142653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定