Exchange web services (EWS) - Sending Mail with Attachments from Mendix

1
Hi I am trying to implement a java SendExchangeMail and have some issues with attachments. i get the following error: Sending email caused an error: Email Header Analyzer, RFC822 Parser - MxToolbox.pdf (The system cannot find the file specified) com.mendix.systemwideinterfaces.MendixRuntimeException: microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. Email Header Analyzer, RFC822 Parser - MxToolbox.pdf (The system cannot find the file specified)     at com.mendix.basis.actionmanagement.ActionManager.executeSync(ActionManager.scala:84)   It seams that the attachment has no content? Thanks Stephan       // This file was generated by Mendix Studio Pro. // // WARNING: Only the following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. // Special characters, e.g., Ã©, Ã¶, Ã , etc. are supported in comments.   package communication.actions;   import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.stream.Collectors; import java.util.List; import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction; import communication.impl.connection.ExchangeConnection; import communication.proxies.EmailSettings; import communication.proxies.Recipient; import communication.proxies.Enum_ExchangeVersion; import communication.util.AutoDiscover; import communication.util.ExchangeException; import communication.util.ExchangeVersionConverter; import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion; import microsoft.exchange.webservices.data.core.service.item.Item; import microsoft.exchange.webservices.data.property.complex.AttachmentCollection; import microsoft.exchange.webservices.data.property.complex.FileAttachment; import com.mendix.systemwideinterfaces.core.IMendixObject;   public class SendExchangeMail extends CustomJavaAction<java.lang.Boolean> {     private IMendixObject __email;     private communication.proxies.Email email;     private java.lang.String password;     private java.util.List<IMendixObject> __recipients;     private java.util.List<communication.proxies.Recipient> recipients;       public SendExchangeMail(IContext context, IMendixObject email, java.lang.String password, java.util.List<IMendixObject> recipients)     {         super(context);         this.__email = email;         this.password = password;         this.__recipients = recipients;     }       @java.lang.Override     public java.lang.Boolean executeAction() throws Exception     {         this.email = __email == null ? null : communication.proxies.Email.initialize(getContext(), __email);           this.recipients = new java.util.ArrayList<communication.proxies.Recipient>();         if (__recipients != null)             for (IMendixObject __recipientsElement : __recipients)                 this.recipients.add(communication.proxies.Recipient.initialize(getContext(), __recipientsElement));           // BEGIN USER CODE                  ILogNode log = Core.getLogger("Email_Exchange");         EmailSettings account = email.getEmail_EmailSettings();         String username = account.getUsername_Outgoing();         String address = account.getExchangeEmailAddress();         String url = account.getExchangeURL();                  ExchangeVersion version = ExchangeVersionConverter.convert(account.getExchangeVersion());                  if(url == null) {             url = new AutoDiscover().autoDiscoverURL(username, password, address, version);             if(url != null) {                 account.setExchangeURL(url);                 account.commit();             } else {                 throw new ExchangeException("Could not auto discover url for this account");             }         }         AttachmentCollection attachments = new AttachmentCollection();         if(email.gethasAttachments()) {             masterdata.proxies.Attachment file;             ByteArrayOutputStream os;             List<masterdata.proxies.Attachment> mxAttachmentList = email.getEmail_Attachment();             for(masterdata.proxies.Attachment mxAttachment : mxAttachmentList) {                 file = (masterdata.proxies.Attachment) mxAttachment;                 os = new ByteArrayOutputStream();                 file.getContents(getContext(), os);                 InputStream stream = new ByteArrayInputStream(os.toByteArray());                 String fileName = file.getName();                 attachments.addFileAttachment(fileName, stream);                 stream.close();                                  }         }                      ExchangeConnection conn = new ExchangeConnection(username, password, url, version);         conn.sendEmail(email.getSubject(), email.getContent(),                  recipients.stream().map((Recipient r) -> r.getEmailAddress()).collect(Collectors.toList()), attachments.getItems());                  return true;         // END USER CODE     }       /**      * Returns a string representation of this action      */     @java.lang.Override     public java.lang.String toString()     {         return "SendExchangeMail";     }       // BEGIN EXTRA CODE     // END EXTRA CODE }    
asked
1 answers
0

You may find it easier to use Core.getFileDocumentContent to get the InputStream of the FileDocument for the addFileAttachment method.

https://apidocs.rnd.mendix.com/8/runtime/com/mendix/core/Core.html#getFileDocumentContent(com.mendix.systemwideinterfaces.core.IContext,com.mendix.systemwideinterfaces.core.IMendixObject)

 

answered