繁体   English   中英

Java使用本地邮件客户端自动发送邮件

[英]Java send mail automatically with local mail client

从java程序中我想自动通过用户的本地邮件客户端发送邮件。

我使用以下代码打开客户端并填写必填字段,但如何在没有任何用户交互的情况下自动发送它?

    private void sendMail() throws MessagingException {
    try {
        Desktop.getDesktop().mail(new URI("mailto:abc@def.com?subject=someSubject&cc=aa@bb.cc,dd@dd.ds&bcc=x@y.zz&body=someBodyText"));
    } catch (Exception e) {
        e.printStackTrace();
    }

}

基本上我想发送不离开公司网络的邮件。

答案是Java Mail API

基本上,您需要一个邮件帐户(通常是用户名+密码),您还需要邮件SP的SMTP服务器地址,这通常在他们的网站上。

根据本指南,我找到了一种处理Outlook的方法: Vogella,Eclipse-Microsoft Integration

基本上我使用OleClientSite类来调用outlook。 然后我使用oleAutomation类发送邮件。

代码段:

            Shell shell = new Shell(Display.getDefault());
            OleFrame frame = new OleFrame(shell, SWT.NONE);
            // This should start outlook if it is not running yet
            OleClientSite site = new OleClientSite(frame, SWT.NONE, "OVCtl.OVCtl");
            site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
            // Now get the outlook application
            OleClientSite site2 = new OleClientSite(frame, SWT.NONE,
                "Outlook.Application");
            OleAutomation outlook = new OleAutomation(site2);
            // 
            OleAutomation mail = invoke(outlook, "CreateItem", 0 /* Mail item */)
                .getAutomation();
            setProperty(mail, "To", "aav@gmail.com"); /*
                                   * Empty but could also be
                                   * predefined
                                   */

            setProperty(mail, "Bcc", "test@gmail.com"); /*
                                   * Empty but could also be
                                   * predefined
                                   */

            setProperty(mail, "BodyFormat", 2 /* HTML */);
            setProperty(mail, "Subject", "Top News for you");
            setProperty(mail, "HtmlBody",
                "<html>Hello<p>, please find some infos here.</html>");

            invoke(mail, "Send" /* or "Send" */);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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