Zimbra offers Open Source email server software and shared calendar for Linux and the Mac
Go Back   Zimbra :: Forums > Zimbra Collaboration Suite > Developers

Welcome to the Zimbra :: Forums!
Welcome, if you would like to post a comment please register. We also encourage you to explore all things Zimbra with our team and members of the community.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-20-2009, 08:31 AM
Member
 
Posts: 14
Default Sample code to create email

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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads

Why Join?

Registering let's you ask questions, makes it easier to search, displays any files attached to posts, and notifies you about replies.

blog.zimbra.com




 

SEO by vBSEO ©2011, Crawlability, Inc.