简体   繁体   中英

how to send email through outlook from java

I need to add e-mails (with attachments) in my MS Outlook 2000 outcoming msgs queue. In my company we use a software called "spool robot" (spool office suite) that does it retrieving data from an AS/400 server. I have to do the same but reading data from my MySql database in Java.

I found the JDIC api but it prepares the message window, but it doesn't send the email automatically. I can't use Java mail because I need to use the Outlook client (due to administrative constraints).

Any ideas?

Thanks.

I'm pretty sure there is a COM interface for Outlook. I looked around a bit and found sufficient evidence but no real documentation I could link to. The Outlook Developer Center seems to be a good starting point though.

Using COM from Java is pretty straight forward using a Java COM Bridge like JACOB . I think there are commercial libraries available, but JACOB was pretty straight forward to use - at least 5 years ago when I last used it :)

You can can manipulate Outlook from Java using the JACOB library as stated in the previous comment. The below example works with Microsoft Outlook 2010 and 2013. This example creates a draft email and saves it to the Drafts folder in Outlook.

//---------------------------------------------------------------------
Map<String, Object> params = new HashMap<String, Object>();
param.put("subject", "Test subject");
param.put("body", "Please see attached.");

String attachment[] = new String[2];
attachment[0] = "C:/Test1.pdf";
attachment[1] = "C:/Test2.pdf";
param.put("attachments", attachment);

String to[] = new String[2];
to[0] = "test1@test.com";
to[1] = "test2@test.com";
param.put("to", to);

OutlookJACOB mail = new OutlookJACOB();
mail.createEmail(param);
//---------------------------------------------------------------------

import java.util.Map;

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

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

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

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

    public void createEmail(Map<String, Object> params)
    {
        Dispatch mail = Dispatch.call(outlook,"CreateItem",email).toDispatch();
        Dispatch.put(mail, "Subject", params.get("subject"));
        Dispatch.put(mail, "Body", params.get("body"));

        String to[] = (String[]) params.get("to");
        String attachments[] = (String[]) params.get("attachments");

        if(to != null)
        {
            if(to.length>0)
            {
                String _to = "";

                for(Object t : to)
                {
                    _to += t + ";";
                }

                Dispatch.put(mail, "To", _to);
            }
        }

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

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

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

It will make your life much much easier if you can be allowed to use the Exchange SMTP server as this works well with JavaMail. The system administrator can possibly provide you with a username+password for this particular purpose.

If you really MUST use the client, could you share details about how the current "spool robot" does it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM