How to send emails with Zimbra Java API?

Have a great idea for extending Zimbra? Share ideas, ask questions, contribute, and get feedback.
roby.tea
Posts: 31
Joined: Sat Sep 13, 2014 3:18 am

How to send emails with Zimbra Java API?

Postby roby.tea » Wed Nov 13, 2013 7:46 am

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


17416jflanigan
Zimbra Alumni
Zimbra Alumni
Posts: 113
Joined: Sat Sep 13, 2014 1:20 am

How to send emails with Zimbra Java API?

Postby 17416jflanigan » Thu Nov 14, 2013 1:27 pm

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










test msg



some content








Also, here are links to the SOAP API docs:
Zimbra SOAP API Reference 8.0.4_GA_5737

SendMsg
roby.tea
Posts: 31
Joined: Sat Sep 13, 2014 3:18 am

How to send emails with Zimbra Java API?

Postby roby.tea » Fri Nov 29, 2013 5:08 am

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
parin
Posts: 28
Joined: Sat Sep 13, 2014 3:06 am

How to send emails with Zimbra Java API?

Postby parin » Fri Nov 29, 2013 5:35 am

@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.
roby.tea
Posts: 31
Joined: Sat Sep 13, 2014 3:18 am

How to send emails with Zimbra Java API?

Postby roby.tea » Fri Nov 29, 2013 7:36 am

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 --
// 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(.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
parin
Posts: 28
Joined: Sat Sep 13, 2014 3:06 am

How to send emails with Zimbra Java API?

Postby parin » Mon Dec 02, 2013 12:52 am

@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.
roby.tea
Posts: 31
Joined: Sat Sep 13, 2014 3:18 am

How to send emails with Zimbra Java API?

Postby roby.tea » Mon Dec 02, 2013 1:30 am

Hello parin,
I followed this article: Extending Zimbra with Server Extensions :: Zimbra :: Blog

and the source code at imbraHelloWorldsrcjavacomzimbraextension
Roberto
parin
Posts: 28
Joined: Sat Sep 13, 2014 3:06 am

How to send emails with Zimbra Java API?

Postby parin » Mon Dec 02, 2013 2:57 am

[quote user="roby.tea"]Hello parin,
I followed this article: Extending Zimbra with Server Extensions :: Zimbra :: Blog

and the source code at imbraHelloWorldsrcjavacomzimbraextension
Roberto[/QUOTE]
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.
roby.tea
Posts: 31
Joined: Sat Sep 13, 2014 3:18 am

How to send emails with Zimbra Java API?

Postby roby.tea » Mon Dec 02, 2013 3:19 am

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

Return to “Developers”

Who is online

Users browsing this forum: No registered users and 3 guests