Noah Martin
Noah Martin

Reputation: 1809

How to send email attachments with microsoft exchange?

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?

Upvotes: 2

Views: 4206

Answers (2)

Gabe
Gabe

Reputation: 6347

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.

Upvotes: 1

Noah Martin
Noah Martin

Reputation: 1809

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

Upvotes: 4

Related Questions