sending mails from mail box doesn't save in sent items

亡梦爱人 提交于 2019-12-11 04:08:36

问题


I am using using EWS to read a mailbox 'test@comp.com' and give in service account credentials to log into the exchange. that service account has access to read test@comp.com and even has access to send mails from it. but when I try to send out mails using following code :

private static void sendMailviaEWS(String to, String from, String subject,
        String body, List<String> attname, ExchangeService service) {

    try 
    {

        EmailMessage replymessage = new EmailMessage(service);
        replymessage.setSender(new EmailAddress(from));

        EmailAddress fromEmailAddress = new EmailAddress(from);
        replymessage.setFrom(fromEmailAddress);
        replymessage.getToRecipients().add(to);
        //replymessage.setInReplyTo(recipients);
        replymessage.setSubject(subject);
        replymessage.setBody(new MessageBody(body));
        replymessage.sendAndSaveCopy(WellKnownFolderName.SentItems);

    }catch (Exception e)
    {
       e.printStackTrace();
    }
}

I don't see the mails getting sent from test@comp.com the to address is mine and from address which I give in is that of test@comp.com

The sent mails always end up in sent items folder of my service account which i use to log into the exchange server.

Is there a way to achieve what I want to?


回答1:


You need to set the FolderId of the folder your saving the sent copy to so it reflects the Mailbox your sending as eg change

replymessage.sendAndSaveCopy(WellKnownFolderName.SentItems);

to

FolderId SentFolderForUser = new FolderId(WellKnownFolderName.SentItems, fromEmailAddress);
replymessage.sendAndSaveCopy(SentFolderForUser);

Cheers Glen



来源:https://stackoverflow.com/questions/26121609/sending-mails-from-mail-box-doesnt-save-in-sent-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!