用Java创建电子邮件 [英] Creating an email in Java

查看:104
本文介绍了用Java创建电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java创建"电子邮件.

I would like to "create" an email using Java.

这是我的意思:

根据我已有的信息,我想在Microsoft Outlook中打开一封电子邮件,其中包含字段收件人",抄送",主题",邮件正文"以及已填充的附件(现在都存储为字符串,附件目录为也存储为字符串).

Based on information I already have, I would like to make an email message open in Microsoft Outlook with the fields To, CC, Subject, Message Body, and attachments already populated (all now stored as strings, the directories for attachments are stored as strings as well).

该邮件需要在Outlook中打开,以便用户验证内容,并有机会添加更多抄送,对主题和邮件正文进行细微调整.

The message needs to open in Outlook for the user to verify the contents and give the opportunity for adding more CC, slight adjustments to subject and message body.

据我所知,实现此目的的最佳"方法似乎是首先在Outlook可以读取的磁盘上创建一个文件,其中包含我的消息,然后使用类似于以下代码的方式在Outlook中打开该文件. /p>

From what I gather, it seems that the "best" way of achieving this is first creating a file on disk that Outlook can read, which contains my message, then opening it with outlook using something similar to the code below.

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("OUTLOOK Directory + CMD switches for opening files");

  1. 理想情况下,我想使用一个简单的框架来简单地使用我已经在不同字段中使用的字符串来创建这样的文件(或者通过一个非简单的框架来实现相同的目的).

  1. Ideally, I would like to use a simple framework for simply creating such a file using the strings I already have for the different fields (or achieving the same thing through a a non-simple framework).

如果没有实现上述目标的好方法",我会选择将附件文件附加到给定的Outlook模板(.oft)文件中的方法.

If there's no "good" way of achieving the above, I'd settle for a method of just attaching my attachment file to a given Outlook template (.oft) file.

(3.我将采用当前的解决方案,即简单地在Outlook中打开模板,在资源管理器中打开附件,并提示用户将文件拖到Outlook中.)

(3. I'll resort to my current solution of simply having the template open in Outlook, the attachment in Explorer, and prompt the user to drag the file into Outlook.)

我已经在Apache POI中查看过HSMF(我在程序的其他部分中是Apache POI),但目前看来还处于试验阶段,我无法为其找到很多文档.

I've looked at HSMF in Apache POI (I'm Apache POI for other parts of my program), but it appears to be rather experimental at this point, and I've been unable to find much documentation for it.

有人对哪里看有什么建议吗?

Does anyone have any suggestions on where to look?

推荐答案

使用桌面 API URI.html#URI-java.lang.String-java.lang.String-java.lang.String-"rel =" nofollow> URI 构造函数,它将引用合法字符.此示例代码将打开您的默认邮件客户端,并填充标题.

Use the Desktop API with the URI constructor that will quote legal characters. This example code will open your default mail client with the headers populated.

public static void main(String[] args) throws Exception {
    URI msg = new URI("mailto", "you@foo.com&cc=team@bar.com&subject=How to create email in Java?body=Use JavaMail.", (String) null);
    Desktop.getDesktop().mail(msg);
}

唯一的限制是,对于URI的长度,上限​​操作系统可以处理.在Windows上,开始"命令还理解 RFC 2368 中解释的语法.

The only limitation is that there is an upper limit to the length of the URI that the O/S can handle. On windows, the 'start' command also understands the syntax which is explained in RFC 2368.

这篇关于用Java创建电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆