kofhearts
kofhearts

Reputation: 3774

Sending email via Exchange WebServices Java API 1.1

Other operations such as creating folders, retrieving information about the existing folders, items seem to work but sending email to a valid email address doesnt work. What could be wrong with the following code is doing so? I appreciate your help.

    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("[email protected]");
    msg.sendAndSaveCopy();

System.out.println("done");

Upvotes: 3

Views: 8097

Answers (3)

pfflet
pfflet

Reputation: 1

This works fine for me...

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();
}

Upvotes: 0

user3686108
user3686108

Reputation: 11

Find below code which works perfectly fine for me .

    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);
        }




    }

Upvotes: 1

grapkulec
grapkulec

Reputation: 1020

Did you try using Send() method instead of SendAndSaveCopy()? Or did you checked if copy of messages is saved and only send part is not working? I ask because I have almost identical code in my C# project and it works perfectly. According to documentation SendAndSaveCopy doesn't work if email has unsaved attachments but that's obviously not a case here.

Upvotes: 0

Related Questions