How do I popup the compose / create mail dialog using the user's default email client?

前端 未结 7 1656
攒了一身酷
攒了一身酷 2021-01-05 15:32

The use case is simple. At a certain point of time, I need to be able to show the user his familiar compose email dialog (Outlook or other) with

  • fields like f
相关标签:
7条回答
  • 2021-01-05 15:55

    Could it be that you used the mailto: protocol?

    Almost all of what you highlight can be done, but I am quite sure, that you cant do attachments.

    Microsoft MailTo Documentation

    0 讨论(0)
  • 2021-01-05 15:59

    Since mailto does not support attachments, and since MAPI is not supported within managed code, your best bet is to write (or have someone write) a small non-managed program to call MAPI functions that you can call with command-line arguments. Pity that .NET does not have a cleaner alternative.

    See also : MAPI and managed code experiences?

    0 讨论(0)
  • 2021-01-05 16:03

    It's probably not the most efficient or elegant way, but shelling a "mailto:" link will do what you want, I think.

    EDIT: Sorry, left out a very important "not".

    0 讨论(0)
  • Starting process with mailto: arguments is the simplest approach. Yet, it does not allow anything more or less complex.

    Slightly different approach involves creating email template and then feeding it to the Process.Start:

    var client = new SmtpClient();
    
    var folder = new RandomTempFolder();
    client.DeliveryMethod = 
      SmtpDeliveryMethod.SpecifiedPickupDirectory;
    
    client.PickupDirectoryLocation = folder.FullName;
    
    var message = new MailMessage("to@no.net",
      "from@no.net", "Subject","Hi and bye");
    
    // add attachments here, if needed
    // need this to open email in Edit mode in OE
    message.Headers.Add("X-Unsent", "1");
    
    client.Send(message);
    
    var files = folder.GetFiles();
    
    Process.Start(files[0].FullName);
    

    Scenarios for the default email handler:

    • Outlook express opens
    • Windows: Outlook - does not handle by default, Outlook Express is called instead
    • Windows: The Bat! - message is opened for viewing, hit Shift-F6 and Enter to send

    I've also tested with Mono and it worked more or less.

    Additional details are available in this post: Information integration - simplest approach for templated emails

    PS: in the end I went for slightly more complex scenario:

    • Defined interface IEmailIntegraton
    • Code above went into the DefaultEmailIntegration
    • Added implementations for OutlookEmailIntegration (automation) and theBat! email integration (using their template format).
    • Allowed users of the SmartClient to select their scenario from the drop-down (alternatively this could've been implemented as "Check the system for the default email handler and decide automatically")
    0 讨论(0)
  • 2021-01-05 16:05

    you can use a trick if you intend to use Outlook[this code is based on outlook 2010[v14.0.0.]] Create Outlook MailItem and transmit file (ie download) if user opens the file (.msg) the compose message dialog opens automatically

    here is the code ...

        Microsoft.Office.Interop.Outlook.Application outapp = new Application();
        try
        {
    
            _NameSpace np = outapp.GetNamespace("MAPI");
            MailItem oMsg = (MailItem)outapp.CreateItem(OlItemType.olMailItem);
            oMsg.To = "a@b.com";
            oMsg.Subject = "Subject";
    
           //add detail
    
            oMsg.SaveAs("C:\\Doc.msg", OlSaveAsType.olMSGUnicode);//your path
            oMsg.Close(OlInspectorClose.olSave);
        }
        catch (System.Exception e)
        {
            status = false;
        }
        finally
        {
            outapp.Quit();
        }
    

    then transmit the file you created say "Doc.msg"

        string filename ="Doc.msg";//file name created previously
        path = "C:\\" + filename; //full path ; 
        Response.ContentType="application/outlook";
        Response.AppendHeader("Content-Disposition", "filename=\"" + filename + "\"");
        FileInfo fl = new FileInfo(path);
        Response.AddHeader("Content-Length", fl.Length.ToString());
        Response.TransmitFile(path,0,fl.Length);
        Response.End();
    
    0 讨论(0)
  • 2021-01-05 16:13

    You're making the assumption that they will have an email client installed, of course.

    The option I've taken in the past (in a corporate environment where everyone has at least one version of Outlook installed) was to use the Outlook interop - you only need to reference the earliest version you need to support.

    You could look at P/Invoking MAPISendDocuments (which I'd try and avoid, personally), or the other option would be to create your own "compose" form and use the objects from the System.Net.Mail namespace.

    0 讨论(0)
提交回复
热议问题