I've written some code which creates an email with an attachment and adds it to the draft folder. It needs more work to be truly useful, but I thought it might be helpful to someone or you might be able to suggest a better approach.
SourceForge.net Repository - [cahaya-hubung] View of /trunk/zmsrc/au/com/cahaya/hubung/zm/contact/mail/ZimbraAddDraftEmail.java
I've made heavy use of com.zimbra.cs.zclient.ZMailbox to simplify the code. This is the key method:
Code:
public String send (File messageFile)
{
AttachmentMessage message = new AttachmentMessage (getSession ());
try {
message.setFrom (new InternetAddress (getMailbox ().getName ()));
message.setSubject ("test");
message.attachDescription ("Test", MimeEnumType.eTextPlain.toString ());
message.attachFile (messageFile);
message.prepareToSend ();
}
catch (Addres***ception exc) {
// TODO Auto-generated catch block
myLog.error ("send", exc);
}
catch (MessagingException exc) {
// TODO Auto-generated catch block
myLog.error ("send", exc);
}
catch (ServiceException exc) {
// TODO Auto-generated catch block
myLog.error ("send", exc);
}
ZFolder folder = null;
try {
folder = getMailbox ().getFolderByPath ("Drafts");
}
catch (ServiceException exc) {
// TODO Auto-generated catch block
myLog.error ("send", exc);
}
if (folder != null) {
int size;
try {
size = message.getSize ();
if (size <= 0) {
size = 4096;
}
try {
ByteArrayOutputStream out = new ByteArrayOutputStream (size);
message.writeTo (out);
return getMailbox ().addMessage (folder.getId (), null, null, 0, out.toByteArray (), false);
}
catch (IOException exc) {
// TODO Auto-generated catch block
myLog.error ("send", exc);
}
catch (ServiceException exc) {
// TODO Auto-generated catch block
myLog.error ("send", exc);
}
}
catch (MessagingException exc) {
// TODO Auto-generated catch block
myLog.error ("send", exc);
}
}
return null;
} Feedback welcome.