2

I want to send email attachements through Exchange server using java. Sending email is working fine:

ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials(username,password);
service.setCredentials(credentials);
service.setUrl(new URI(MailHost));

EmailMessage msg = new EmailMessage(service);
msg.setSubject("My Subject!");
msg.setBody(MessageBody.getMessageBodyFromText("My Message"));
msg.getToRecipients().add(mailTo);

Can anyone please help me?

Noah Martin
  • 1,708
  • 9
  • 35
  • 73
  • What API are the exchange classes from? Why can't you just use smtp? – Andreas May 29 '14 at 15:25
  • And which `EmailMessage` implementation? There seem to be quite a few unrelated ones out there. – nobody May 29 '14 at 15:48
  • Thank you for your comments. Exchange api is the same as used here: http://www.aaronheld.com/post/reading-exchange-mail-with-java EmailMessage class microsoft.exchange.webservices.data.EmailMessage – Noah Martin May 30 '14 at 06:29

2 Answers2

3

I found the solution HERE

public boolean sendEWSMail(String subject, String bodyContent, String to, String cc){
    ExchangeService service = new ExchangeService();
    EmailMessage msg = null; 
    ExchangeCredentials credentials = null;
    String domain = "domain name";
    if (domain == null || domain.equals("")) {
        credentials = new WebCredentials("username", 
                "password");
    } else {
        credentials = new WebCredentials("username", 
                "password", domain);
    }
    service.setCredentials(credentials);
    try {
        service.setUrl(new URI("Mail server URL"));
        msg = new EmailMessage(service);
        msg.setSubject(subject); 
        msg.setBody(MessageBody.getMessageBodyFromText(bodyContent));
        msg.getAttachments().addFileAttachment("Complete File Path");
        if(to == null || to.equals("")){
            LOGGER.warn("To distribution list is empty. Could not send the mail ");
        }else{
            String[] mailTos = to.split(";");
            for(String mailTo : mailTos){
                if(mailTo != null && !mailTo.isEmpty())
                msg.getToRecipients().add(mailTo);
            }
            if(cc != null && !cc.isEmpty()){
                String[] mailCCs = cc.split(";");
                for(String mailCc : mailCCs){
                    if(mailCc != null && !mailCc.equals(""))
                    msg.getCcRecipients().add(mailCc);
                }
            }
            msg.send();
            LOGGER.debug("Mail successfully send ");
            return true;
        }
    } catch (Exception e) {
        LOGGER.error("Exception occurred while sending EWS Mail ", e);
    }
    return false;
}
Noah Martin
  • 1,708
  • 9
  • 35
  • 73
  • 1
    Okay, so you have answered your own question and the bounty is gone. Anyway, as a side note and because I wanted to try and help you: Do you know of any Exchange Server test environments available via internet for developers who do not have access to a "real" Exchange corporate server? – kriegaex Jun 02 '14 at 09:45
  • Yes I know but as I saw such a low number of views I just posted the answer after I found it. About your question I don't know any one. – Noah Martin Jun 02 '14 at 11:15
1

Nowadays (2016) you could also use the new Outlook Email API and send RESTful requests from the java code using an OutlookClient.

See here too.

Gabe
  • 5,997
  • 5
  • 46
  • 92