通过Exchange WebServices Java API 1.1发送电子邮件

时间:2011-05-02 17:50:14

标签: java email exchangewebservices

其他操作,例如创建文件夹,检索有关现有文件夹的信息,项目似乎有效但将电子邮件发送到有效的电子邮件地址无效。以下代码可能出现什么问题?感谢您的帮助。

    ExchangeService service = new ExchangeService();
    ExchangeCredentials credentials = new WebCredentials("vuser","vpass");
    service.setCredentials(credentials);    
    service.setUrl(new URI("https://valid_server/ews/Exchange.asmx"));      


    EmailMessage msg= new EmailMessage(service);
    msg.setSubject("Hello world!"); 
    msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Managed API."));
    msg.getToRecipients().add("valid@hotmail.com");
    msg.sendAndSaveCopy();

System.out.println("done");

3 个答案:

答案 0 :(得分:1)

找到下面对我来说完全正常的代码。

    public void  sendEmail(String body, String subject, String recipients, String from)
    {
        service = new ExchangeService();
        ExchangeCredentials credentials = new WebCredentials(username, password);
        service.setCredentials(credentials);
        service.setUrl(ewsUri);

        try 
        {
            EmailMessage replymessage = new EmailMessage(service);
            EmailAddress fromEmailAddress = new EmailAddress(from);
            replymessage.setFrom(fromEmailAddress);
            replymessage.getToRecipients().add(recipients);
            //replymessage.setInReplyTo(recipients);
            replymessage.setSubject(subject);
            replymessage.setBody(new MessageBody(body));
            replymessage.send();

        }catch (Exception e)
        {
            logger.error(""+e);
        }




    }

答案 1 :(得分:0)

您是否尝试使用Send()方法而不是SendAndSaveCopy()?或者您是否检查过邮件副本是否已保存且仅发送部分无效?我问,因为我的C#项目中的代码几乎完全相同,而且完美无缺。根据文档,如果电子邮件有未保存的附件,SendAndSaveCopy不起作用,但这显然不是一个案例。

答案 2 :(得分:0)

这对我来说很好......

public static void sendMail(String[] addresses) throws Exception {
    // enter vaild mail id and password
    ExchangeCredentials credentials = new WebCredentials("mail_id", "passwd");
    service.setCredentials(credentials);
    // enter vaild server url
    service.setUrl(new URI("server"));
    EmailMessage mail = new EmailMessage(service);
    mail.setSubject("Hello EWS Send");
    mail.setBody(new MessageBody("pffed bye!!!"));
    for (String string : addresses)
        mail.getToRecipients().add(new EmailAddress(string));
    mail.sendAndSaveCopy();
}