我正在寻找一种方法,让我的应用程序可以调用用户的标准邮件应用程序(例如 Outlook、Thunderbird 等)。并为其提供收件人地址、电子邮件文本和附件。
所以,基本上标准的电子邮件应用程序应该会弹出并为我准备好电子邮件(包括收件人、文本和附件),剩下要做的就是在我的 outlook、thunderbird 等中按“发送”。
我已经用谷歌搜索了一段时间,但找不到真正的解决方案。
我一直在研究 mapi,但似乎 1. 它已被弃用,并且 2. 它主要是为 outlook 构建的。
非常感谢任何帮助/建议/解决方案!
编辑: 我已经看到问题 Start Mail-Client with Attachment但是那里没有提供有效的答案,而且这个问题已经存在超过 3 年了。
编辑:其他语言也可以。必须在 Windows XP、Vista、7、8(32 位和 64 位)上工作
更新:这似乎比我想象的要难。
我一直在调查 JMAPI ,这显然只适用于 32 位系统。
我也在 codeproject.org 上看到了解决方案(here 和 here),但我无法让它们工作。
现在我正在尝试使用命令行来完成它:
1.读取用户默认邮件客户端
2.根据邮件客户端调用一个批处理文件。 (是的,您必须为每个常见的邮件客户端编写一个批处理文件。
前景示例:
"outlook.exe" /a "F:\test.png" /m "test.test@test.test&cc=test@test.test&subject=subject123&body=Hello, how are you%%3F%%0D%%0Anew line"
-->有关该方法的更多信息,请参阅我提供的答案
最佳答案
所以...
经过几天的研究,我放弃了获得通用解决方案。 我想出了一个至少适用于两个最常见的客户端(Thunderbird 和 Outlook)的解决方案
我的解决方案基本上是从命令行调用应用程序。
对于那些感兴趣的人,这里是我的解决方案:(我还没有跨平台测试它 - 虽然可以在我的旧 XP 笔记本电脑上使用)
import java.io.IOException;
/*
:: Punctuation Hexadecimal equivalent
:: ----------------------------------------------
:: Space ( ) %20
:: Comma (,) %2C
:: Question mark (?) %3F
:: Period (.) %2E
:: Exclamation point (!) %21
:: Colon (:) %3A
:: Semicolon (;) %3B
:: Line feed %0A --> New line %0D%0A
:: Line break (ENTER key) %0D --> New line %0D%0A
*/
public class Main {
static String test = "hi";
private static String attachment;
private static String to;
private static String cc;
private static String subject;
private static String body;
public static void main (String[] args){
attachment = "F:\\pietquest.png";
to = "test@test.de";
cc = "a.b@c.de";
subject = "TestSubject 123";
body = "Hi, what\'s going on%0D%0Anew line";
body = replace(body);
subject = replace(subject);
String[] value = WindowsRegistry.readRegistry("HKEY_LOCAL_MACHINE\\SOFTWARE\\Clients\\Mail", "");
if (value[10].contains("Thunderbird")){
System.out.println("Thunderbird");
String[] pfad = WindowsRegistry.readRegistry("HKEY_LOCAL_MACHINE\\SOFTWARE\\Clients\\Mail\\Mozilla Thunderbird\\shell\\open\\command", "");
String Pfad = pfad[10] + " " + pfad[11];
String argument = Pfad + " /compose \"to=" + to + ",cc=" + cc + ",subject=" + subject + ",body=" + body + ",attachment=" + attachment + "\"";
// System.out.println(argument);
try {
Runtime.getRuntime().exec(argument);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (value[10].contains("Outlook")){
System.out.println("Outlook");
String[] pfad = WindowsRegistry.readRegistry(
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Clients\\Mail\\Microsoft Outlook\\shell\\open\\command", "");
String Pfad = pfad[10];
String argument = Pfad + " /a " + attachment + " /m \"" + to
+ "&cc=" + cc + "&subject=" + subject + "&body=" + body + "\"";
// System.out.println(argument);
try {
Runtime.getRuntime().exec(argument);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static String replace(String toReplace){
toReplace = toReplace.replace(" ", "%20");
toReplace = toReplace.replace(",", "%2C");
toReplace = toReplace.replace("?", "%3F");
toReplace = toReplace.replace(".", "%2E");
toReplace = toReplace.replace("!", "%21");
toReplace = toReplace.replace(":", "%3A");
toReplace = toReplace.replace(";", "%3B");
return toReplace;
}
}
这是 Windows 注册表类:(从 here 获得)
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
public class WindowsRegistry {
/**
*
* @param location path in the registry
* @param key registry key
* @return registry value or null if not found
*/
public static final String[] readRegistry(String location, String key){
try {
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec("reg query " +
'"'+ location);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
// Parse out the value
String[] parsed = reader.getResult().split("\\s+");
if (parsed.length > 1) {
return parsed;
}
} catch (Exception e) {}
return null;
}
static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw= new StringWriter();
public StreamReader(InputStream is) {
this.is = is;
}
public void run() {
try {
int c;
while ((c = is.read()) != -1)
sw.write(c);
} catch (IOException e) {
}
}
public String getResult() {
return sw.toString();
}
}
关于java - 使用预先附加的附件启动用户的标准邮件客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24505264/