【问题标题】:Start user's standard mail client with attachment pre-attached使用预先附加的附件启动用户的标准邮件客户端
【发布时间】:2014-08-21 17:14:14
【问题描述】:

我正在寻找一种方法,让我的应用程序可以调用用户的标准邮件应用程序(例如 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 上看到了解决方案(herehere),但不知何故我无法让它们工作。
现在我正在尝试使用命令行:
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"

-->有关该方法的更多信息,请参阅我提供的答案

【问题讨论】:

  • @sina72 读完,似乎没有给这个问题真正的解决方案(答案在 64 位系统上不起作用)而且这个问题已经 3 年了 - 有很多时间来寻找新的解决方案起来。
  • 预先附加附件有多重要?因为mailto: 处理程序应该能够执行其他所有操作。
  • @stripybadger 是的,我也调查过。但预先附上附件对我来说非常重要。
  • 如果你能设法找出如何从批处理文件启动它,你可以使用运行时启动它,如:Runtime.getRuntime().exec("cmd.exe /c start") ;

标签: java c# c++ email email-attachments


【解决方案1】:

所以...

经过几天的研究,我放弃了寻求通用解决方案。 我想出了一个至少适用于两个最常见的客户(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();
        }
    }

【讨论】:

    【解决方案2】:

    您可以使用 C#:Example C# 或 java:Example Java

    编辑

    您可以将Boost 用于 ssl 并通过 smtp 发送电子邮件

    【讨论】:

    • 如果我做对了,他们都不会打开我的标准邮件客户端(outlook、thunderbird 等)——如果我错了,请纠正我
    • 你有用户名+密码吗?
    • 有什么用?我想要一个类似于点击邮件的功能:地址
    猜你喜欢
    • 2011-08-27
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多