从刚收到的电子邮件中获取身份证

时间:2018-05-16 07:00:30

标签: java exchangewebservices

我尝试获取通过Java EWS API发送的电子邮件的ID。 我的目标是当我获得该ID时,我会将其存储到数据库中。

我正在尝试:

    try {
        String isiEmail     = generateIsiEmail(nmBank, jenis, tglAw, tglAk, produk);
        EmailMessage mail   = new EmailMessage(service);
        mail.setSubject(jdlEmail);
        mail.setBody(new MessageBody(isiEmail));

        //set to cc 
        mail.getToRecipients().add(new EmailAddress(from.replaceAll("\\s", "")));                   
        String[] too = to.split("\\;");
        for (int i = 0; i <too.length; i++) { 
            mail.getToRecipients().add(new EmailAddress(too[i].replaceAll("\\s", "")));                 
        }           
        String[] ccc = cc.split("\\;");
        for (int i = 0; i <ccc.length; i++) {
            mail.getCcRecipients().add(new EmailAddress(ccc[i].replaceAll("\\s", "")));
        }           
        mail.sendAndSaveCopy();
    } catch (ServiceLocalException e) {
        e.printStackTrace();
    } catch (Exception e) { 
        e.printStackTrace();
    }

感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

您应该使用InternetMessageId属性。

调用FindItems方法搜索已发送邮件文件夹中的邮件。然后实例化一个EmailMessage对象,以便您可以访问InternetMessageId属性:

ItemView view = new ItemView(100); // You can change this to your needs.
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.InternetMessageId);
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.SentItems, view);
foreach (Item item in results)
{
    if (item is EmailMessage)
    {
        EmailMessage emailMsg = item as EmailMessage;
        Console.WriteLine(emailMsg.InternetMessageId);
    }
}

答案 1 :(得分:1)

我自己解决了。 这一步我完成了什么。 我使用ExtendedPropertyDefinition参考本教程https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/dd633654(v%3dexchg.80),但我从C#修改为java编程语言,

  1. 设置ExtendedPropertyDefinition,然后将uuidToStr保存到数据库

        UUID uuid = UUID.randomUUID();
        ExtendedPropertyDefinition epd = new ExtendedPropertyDefinition(uuid, "NamaId", MapiPropertyType.String);
        String uuidToStr = uuid.toString();
    
        String isiEmail     = generateIsiEmail(nmBank, jenis, tglAw, tglAk, produk);
        EmailMessage mail   = new EmailMessage(service);
        mail.setSubject(jdlEmail);
        mail.setBody(new MessageBody(isiEmail)); 
    
        //set to cc 
        mail.getToRecipients().add(new EmailAddress(from.replaceAll("\\s", "")));                   
        String[] too = to.split("\\;");
        for (int i = 0; i <too.length; i++){
            mail.getToRecipients().add(new EmailAddress(too[i].replaceAll("\\s", "")));                 
        }           
        String[] ccc = cc.split("\\;");
        for (int i = 0; i <ccc.length; i++){
            mail.getCcRecipients().add(new EmailAddress(ccc[i].replaceAll("\\s", "")));
        }           
        mail.setExtendedProperty(epd, "isiId"); 
        mail.sendAndSaveCopy();     
    
  2. 从数据库中获取基于ExtendedPropertyDefinition uuidToStr的电子邮件

        UUID uuid = UUID.fromString("cc59cdbf-aad4-4cd1-a4f0-e7819c56b884");
        ExtendedPropertyDefinition epd = new ExtendedPropertyDefinition(uuid, "NamaId", MapiPropertyType.String);
    
        ItemView view2 = new ItemView(3);
        SearchFilter sf = new SearchFilter.IsEqualTo(epd,"isiId");
        FindItemsResults<Item> fir = service.findItems(WellKnownFolderName.SentItems, sf, view2);
        for (Item itm : fir.getItems()){
            System.out.println(itm.getId());
            System.out.println(itm.getSubject());  
        }
    
  3. DONE;

答案 2 :(得分:0)

我正在使用 C# EWS Api,但是这个逻辑对你有用。 首先,您必须将电子邮件保存在草稿中,然后才能获得电子邮件 ID

EmailMessage emailMessage = new EmailMessage(service);
emailMessage.From = email.From;
emailMessage.Subject = email.Subject;
emailMessage.Body = new MessageBody(BodyType.HTML, email.Body);

foreach (var toAddress in email.To)
{
  emailMessage.ToRecipients.Add(toAddress);
}

// Send message and save copy by default to sentItems folder
emailMessage.Save();
emailMessage.Load();

// Get Email Conversation Id.
string ConversationId = emailMessage.ConversationId.UniqueId;
string EmailMessageId;

emailMessage.SendAndSaveCopy();


// Get Email Message Id by InternetMessageId.
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
            searchFilterCollection.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.InternetMessageId, InternetMessageId));

// Create the search filter.
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());

ItemView view = new ItemView(50);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.InternetMessageId);
            FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.SentItems, searchFilter, view);

            
if (results.Items.Count > 0)
{
 Item item = results.Items[0];
 EmailMessage message = EmailMessage.Bind(service, item.Id);

 EmailMessageId = message.Id.UniqueId;
}

答案 3 :(得分:-1)

我相信解决方案是:

EmailMessage em = EmailMessage.bind( service, item.getId(),
                   new PropertySet( EmailMessageSchema.InternetMessageId) );

说明:

我们必须将项目绑定到电子邮件消息,但不是抓取所有信息,而是通过PropertySet参数询问ID和我们想要的任何其他属性。

发现 - &gt; Exchange Web Services get Message Message-ID