1

I have a Java application running on Tomcat which needs to generate an email to Outlook on the connecting client's computer. Currently my code is always directing the email to Outlook on the server where tomcat is running and not to the connecting client. I have attached the code that I am using to connect to Outlook below. Any help to achieve this will be appreciated.

package org.outlook.emails;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

public class EmailAPI
{
    private ActiveXComponent ol;
    private Dispatch outlook;
    private Object mapi[] = new Object[1];
    private Object email[] = new Object[1];

    public EmailAPI()
    {
        mapi[0] = "MAPI";
        email[0] = 0;

        ol = new ActiveXComponent("Outlook.Application");
        outlook = ol.getObject();
        Dispatch.call(outlook,"GetNamespace",mapi).toDispatch();
    }

    public void createEmail(String receiver,String cc,String subject,    String body, String attachments[])
    {
        Dispatch mail = Dispatch.call(outlook,"CreateItem",email).toDispatch();
        Dispatch inspector = Dispatch.get(mail, "GetInspector").getDispatch();  
 Dispatch recipients = Dispatch.call(mail, "Recipients").getDispatch(); 
        Dispatch.call(recipients, "Add" , receiver); 
        Dispatch.put(mail, "CC",cc);
        Dispatch.put(mail, "Subject", subject);
        Dispatch.put(mail, "Body", body);

        if(attachments.length>0)
        {
            Dispatch attachs = Dispatch.get(mail, "Attachments").toDispatch();

            for(Object attachment : attachments)
            {
                Dispatch.call(attachs, "Add", attachment);
            }
        }

        Dispatch.call(mail, "Send");
    }
}

Thank you.

Med

Med
  • 11
  • 2

1 Answers1

0

Since your code is running on a server of course it will trigger what ever action (e.g. start outlook if existent) on the server.

You have to transfer the code to the client and execute it there. This was possible with java applets however has been removed/disabled recently. Another possibility would be the java web start technology which could do that for you. However the support for web start will be removed in future versions of java, too. So now we know too little about your scenario but I would advice you to do some evaluation/analysis again. Depending on your requirements there are several possibilities and one can only guess what the best option is for you. Some options:

  • Use Java Web Start since you need to offer that feature only for a limited timeframe
  • You write a standalone java app (since e.g. you manage all clients from your company)
  • You use javascript to start any email client installed on the client (e.g. window.location.href = "mailto:user@example.com?subject=Subject&body=message%20goes%20here"; or some IE only solution here
  • you implement your own HTML email web form and send the email from the server (this is easier than you think, except maybe for the emailing-infrastructure questions)
  • ...several more options
Lonzak
  • 9,334
  • 5
  • 57
  • 88