启动用户的标准邮件客户端,并预先附加附件
本文关键字:客户端 用户 标准 启动 | 更新日期: 2023-09-27 18:11:08
我正在寻找一种方法,使我的应用程序可以调用用户的标准邮件应用程序(例如Outlook, Thunderbird等)。并给它一个收件人地址,电子邮件文本和附件。
所以,基本上标准的电子邮件应用程序应该弹出,为我准备好电子邮件(包括收件人,文本和附件),剩下要做的就是在我的outlook, thunderbird等中按"发送"键。
我已经在谷歌上搜索了一段时间,但我找不到一个真正的解决方案。
我一直在寻找mapi一点,但它似乎1。它已被弃用,2。它主要是为outlook构建的。
任何帮助/建议/解决方案,非常感谢!编辑:我看到过这个问题,开始邮件客户端与附件,但没有工作的答案在那里提供,而且这个问题已经超过3年了。
Edit:其他语言也可以。必须在Windows XP, Vista, 7,8(32位和64位)上工作
UPDATE:这似乎比我想象的要困难得多。
我一直在研究JMAPI,它显然只适用于32位系统。我也在codeproject.org(这里和这里)上看到了解决方案,但不知怎么的,我无法让它们工作。
现在我试着用命令行来做:
1. 读取用户的默认邮件客户端
2. 根据电子邮件客户端调用批处理文件。(是的,你必须为每一个常见的邮件客户端写一个批处理文件。outlook示例:
"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"
->关于该方法的进一步信息请参阅我提供的答案
So…
经过几天的研究,我放弃了得到一个通用的解决方案。我想出了一个解决方案,至少适用于两种最常见的客户(Thunderbird &前景)
我的解决方案基本上是从命令行调用应用程序。
对于那些感兴趣的人,这里是我的解决方案:(我还没有跨平台测试-虽然在我的旧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注册表类:(从这里得到的)
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();
}
}
您可以使用c#: Example c#或java: Example java
编辑
您可以使用Boost ssl和通过smtp发送电子邮件