Domanda

Attualmente sto usando il metodo seguito per aprire l'account di posta elettronica degli utenti di Outlook e compilare una mail con il contenuto rilevante per l'invio:

public void SendSupportEmail(string emailAddress, string subject, string body)
{
   Process.Start("mailto:" + emailAddress + "?subject=" + subject + "&body=" 
                + body);
}

Voglio però essere in grado di popolare l'e-mail con un file allegato.

qualcosa di simile:

public void SendSupportEmail(string emailAddress, string subject, string body)
{
   Process.Start("mailto:" + emailAddress + "?subject=" + subject + "&body=" 
      + body + "&Attach="
      + @"C:\Documents and Settings\Administrator\Desktop\stuff.txt");
}

Tuttavia questo non sembra funzionare. Qualcuno sa di un modo che permetterà a questo lavoro!?

Guida notevolmente apprezzare.

Saluti.

È stato utile?

Soluzione

mailto: non supporta ufficialmente allegati. Ho sentito Outlook 2003 funzionerà con la seguente sintassi:

<a href='mailto:name@domain.com?Subject=SubjTxt&Body=Bod_Txt&Attachment=""C:\file.txt"" '>

Un modo migliore per gestire questa situazione è quello di inviare la posta sul server utilizzando System.Net.Mail.Attachment .

    public static void CreateMessageWithAttachment(string server)
    {
        // Specify the file to be attached and sent.
        // This example assumes that a file named Data.xls exists in the
        // current working directory.
        string file = "data.xls";
        // Create a message and set up the recipients.
        MailMessage message = new MailMessage(
           "jane@contoso.com",
           "ben@contoso.com",
           "Quarterly data report.",
           "See the attached spreadsheet.");

        // Create  the file attachment for this e-mail message.
        Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
        // Add time stamp information for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(file);
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
        disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
        // Add the file attachment to this e-mail message.
        message.Attachments.Add(data);

        //Send the message.
        SmtpClient client = new SmtpClient(server);
        // Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;

        try {
          client.Send(message);
        }
        catch (Exception ex) {
          Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", 
                ex.ToString() );              
        }
        data.Dispose();
    }

Altri suggerimenti

Se si vuole accedere al client di posta elettronica predefinito quindi è possibile utilizzare MAPI32.dll (funziona solo con Windows). Date un'occhiata al seguente involucro:

http://www.codeproject.com/KB/IP/SendFileToNET.aspx

Codice assomiglia a questo:

MAPI mapi = new MAPI();
mapi.AddAttachment("c:\\temp\\file1.txt");
mapi.AddAttachment("c:\\temp\\file2.txt");
mapi.AddRecipientTo("person1@somewhere.com");
mapi.AddRecipientTo("person2@somewhere.com");
mapi.SendMailPopup("testing", "body text");

// Or if you want try and do a direct send without displaying the mail dialog
// mapi.SendMailDirect("testing", "body text");

Questo app davvero bisogno di usare Outlook? C'è una ragione per non usare lo spazio dei nomi System.Net.Mail?

Se si ha realmente bisogno di utilizzare Outlook (e io non lo consiglio perché poi si sta basando la vostra applicazione su 3rd dipendenze parti che sono suscettibili di variazioni) è necessario esaminare gli spazi dei nomi Microsoft.Office

Mi piacerebbe iniziare qui: http://msdn.microsoft.com/en- us / library / microsoft.office.interop.outlook.aspx

Prova questo

var proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = string.Format("\"{0}\"", Process.GetProcessesByName("OUTLOOK")[0].Modules[0].FileName);
proc.StartInfo.Arguments = string.Format(" /c ipm.note /m {0} /a \"{1}\"", "someone@somewhere.com", @"c:\attachments\file.txt");
proc.Start();

Ufficialmente sì il protocollo mailTo non supporta gli allegati. Ma Williwyg ha spiegato molto bene qui che c'è un modo per farlo - Aprire client di posta predefinito con un allegato

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top