Results 1 to 9 of 9

Thread: How to send emails with Zimbra Java API?

  1. #1
    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    2

    Default How to send emails with Zimbra Java API?

    Hello folks

    I've developed a Zimlet taht is basically a Zimbra ServerExtension written in Java... - I've extended the com.zimbra.cs.extension.ZimbraExtension class to create my own SOAP Service.
    Everything is working fine.

    Next step is to send an email from the current logged accout (the user thta invokes my zimlet and so my Server Extension). This email is some kind of log when something fails, like an administrator message.

    Can someone address me to ZImbra JAVA API that can send such email?

    Thanks
    Roberto

  2. #2
    Join Date
    Jul 2010
    Posts
    113
    Rep Power
    5

    Default

    Take a look at SendMsg. Here's a very simple example:

    Code:
        <SendMsgRequest xmlns="urn:zimbraMail">
          <m>
            <e t="t" a="user2@host.local"/>
            <e t="f" a="user1@host.local"/>
            <su>test msg</su>
            <mp ct="text/plain">
              <content>some content</content>
            </mp>
          </m>
        </SendMsgRequest>
    Also, here are links to the SOAP API docs:

    Zimbra SOAP API Reference 8.0.4_GA_5737
    SendMsg

  3. #3
    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    2

    Default

    Thank you for your reply, jflanigan!

    I was aware of this API, my problem is how to use this in Java.
    I took a look at the source code and to some Tests Units, but I did not found a complete usable example.

    Somthing like this, for example:

    SendMsgRequest smr = new SendMsgRequest();
    smr.setSomething1(...);
    smr.setSomething2(...);
    smr.setSomething3(...);

    smr.send/invoke/launch....();

    Are you aware of some articles that could give me such JAVA example?

    Thank you so much
    Roberto

  4. #4
    Join Date
    Feb 2013
    Location
    India
    Posts
    28
    Rep Power
    2

    Default

    @roby.tea

    if you dont want to use any zimbra class, there is option of SAAJ api for java. you can construct a SOAP message for request like sendmessage according to the structure shown in zimbra SOAP api. using same api you can then send this message to your server and get reply. reply message will be in the form of SOAP response. so you can exctract elements you want. this is simple but harder way. another way is to use JAXB to handle this creation of messages for requests and response. and last but not the least,the easiest among all is to use jackson like parser for JSON request and response. also there is pre built jars under zimbra source which you can use/consume in your app. but i also struggled to find any good example to consume that jars. so used above ways and it is working great.

  5. #5
    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    2

    Default

    Hello Parin and All,

    first of all thanks for your reply and your suggestions.

    I was writing meanwhile a piece of code, and seems working: can you please give me your feedback? Honestly I'm not sure if this code is for some reasons illegal or non secure at all or something similar.

    Here's what I did:

    -- I'm inside a Zimbra Java soap extension, I receive an email address as parameter --

    Code:
    // get zMailbox instance of this email account
    SoapProvisioning soapProv = SoapProvisioning.getAdminInstance();
    DelegateAuthResponse dar = soapProv.delegateAuth(AccountBy.name, account, 60 * 60 * 24);
    ZAuthToken zToken = dar.getAuthToken();
    Options options = new ZMailbox.Options(this.zToken.getValue(), soapProv.soapGetURI());
    options.setTargetAccount("abc@myzcsservertest.com");
    zMailbox zMailbox = ZMailbox.getMailbox(options);
    
    // get account id
    ZGetInfoResult zir = this.zMailbox.getAccountInfo(false);
    String accountId = zir.getId();
            
    // compose a new message
    ZMimeMessage zMimeMessage = new ZMimeMessage(JMSession.getSession());
    zMimeMessage.setContent(<myLogTrace>.toString(), MimeConstants.CT_TEXT_PLAIN);
    zMimeMessage.setRecipients(RecipientType.TO, "admin@myzcsservertest.com");
    zMimeMessage.setSubject("error log generated");
    
    // send the message without saving it (this message is just for automatic notification)
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(accountId);
    MailSender mailSender = mbox.getMailSender().setSaveToSent(false);
    mailSender.sendMimeMessage(null, mbox, zMimeMessage);
    What do you think of that piece of code?
    Is this good?

    Thanks
    Roberto

  6. #6
    Join Date
    Feb 2013
    Location
    India
    Posts
    28
    Rep Power
    2

    Default

    @roby.tea

    As i told you in previous post, i dont know how to use that soap extension in project,i have no idea about this code. if you know how to use them,please share with us.

    Thanx.

  7. #7
    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    2

    Default

    Hello parin,

    I followed this article: Extending Zimbra with Server Extensions :: Zimbra :: Blog
    and the source code at \ZimbraHelloWorld\src\java\com\zimbra\extension

    Roberto

  8. #8
    Join Date
    Feb 2013
    Location
    India
    Posts
    28
    Rep Power
    2

    Default

    Quote Originally Posted by roby.tea View Post
    Hello parin,

    I followed this article: Extending Zimbra with Server Extensions :: Zimbra :: Blog
    and the source code at \ZimbraHelloWorld\src\java\com\zimbra\extension

    Roberto
    Thanx rody.tea for sharing that post here. but i think this blog post is about extending Zimbra Server capabilities and not using its core api.

  9. #9
    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    2

    Default

    Indeed Parin,

    as I described in my first post, I created my own Server extension capability (a new Soap Service), and inside this extension I had to send an email for logging purposes.

    I'm trying to understand if using these Zimbra core API here is allowed or not suggested or anything else.

    Thanks
    Roberto

Similar Threads

  1. Zimbra can receive emails but cannot send out
    By israel.lugayizi in forum Administrators
    Replies: 2
    Last Post: 06-18-2013, 12:22 AM
  2. Replies: 4
    Last Post: 04-26-2012, 01:01 AM
  3. dual NIC and send emails to a second zimbra server
    By ciprianl in forum Administrators
    Replies: 2
    Last Post: 03-24-2011, 01:17 AM
  4. Number of emails zimbra can send in 1 hour
    By abhiz in forum Administrators
    Replies: 5
    Last Post: 03-04-2009, 02:48 AM
  5. Replies: 4
    Last Post: 12-19-2006, 05:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •