0

I'm looking for a way that my application can call the user's standard mail application (e.g. Outlook, Thunderbird, etc.). And give it an recipient address, the email text and an attachment.

So, basically the standard email application should pop up have the email ready for me (with recipient, text and attachment) and all that is left to do for me is pressing "send" in my outlook, thunderbird etc.

I've been googling for a while now, but I couldn't find a real solution.

I've been looking into mapi a bit but it seems like 1. it's deprecated and 2. it's mainly built for outlook.

Any help/suggestions/solutions greatly appreciated!

Edit: I have seen the question Start Mail-Client with Attachment but no working answer was provided there and also the question is more than 3 years old.

Edit: Other languages would be ok, too. Has to work on Windows XP, Vista, 7, 8 (both 32 and 64 bit)

UPDATE: It seems to be more difficult than I have thought it to be.
I've been looking into JMAPI, which apparently only works for 32bit Systems.
I've also seen the solutions on codeproject.org (here and here), but I somehow couldn't get them to work.
Now I'm trying to do it with command line:
1. Read user's default mail client
2. Call a batch file according to the email client. (Yes you have to write a batch file for every common mail client.
Example for 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"

--> see my provided answer for futher info on that method

Community
  • 1
  • 1
itaton
  • 287
  • 2
  • 13
  • Read here: http://stackoverflow.com/questions/6029579/start-mail-client-with-attachment – sina72 Jul 01 '14 at 08:05
  • @sina72 finished reading, seems like there was no real solution given to this question (answer doesn't work on 64bit systems) and also the question is 3 years old - lots of time for new solutions to rise up. – itaton Jul 01 '14 at 08:10
  • How critical is it to have the attachment pre-attached? Because the `mailto:` handler should be able to do everything else. – stripybadger Jul 01 '14 at 08:21
  • @stripybadger Yeah I looked into that, too. But it's pretty important for me to have the attachment pre-attached. – itaton Jul 01 '14 at 08:23
  • If you can manage to find out how to start it from batch file, you can use Runtime to start it like in: Runtime.getRuntime().exec("cmd.exe /c start"); – LastFreeNickname Jul 01 '14 at 08:43
  • OS details probably needed. – Yakk - Adam Nevraumont Jul 01 '14 at 09:15
  • @Yakk Windows (all versions from XP to 8). But if there is a solution that's running on other OS too, that would be preferable. – itaton Jul 01 '14 at 09:41
  • @Quest Can you be more specific? I guess you're talking about JavaMailAPI which won't serve the purpose of preparing an email and open the standard email application – itaton Jul 01 '14 at 13:45
  • Can this flag "The question may already have an answer here:" be removed, since it doesn't? – itaton Jul 02 '14 at 07:05

2 Answers2

3

So...

After days of research I gave up to get a general solution. I came up with a solution working at least for the two most common clients (Thunderbird & Outlook)

My solution is basically calling the application from command line.

For those interested, here is my solution: (I haven't tested it cross platform - works on my old XP laptop though)

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;
    }
}

and this is the Windows Registry Class: (got that from 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();
        }
    }
itaton
  • 287
  • 2
  • 13
0

you can use C#: Example C# or java: Example Java

EDIT

You can use Boost for ssl and send email via smtp