2015-12-24 18 views
8
string email ="[email protected]"; 
attachment = path + "/" + filename; 
Application.OpenURL ("mailto:" + 
         email+" 
         ?subject=EmailSubject&body=EmailBody"+"&attachment="+attachment); 

Nel codice precedente, attachment non funziona. C'è qualche altra alternativa per aggiungere allegati usando un collegamento mailto: in C#?Come aggiungere allegati a mailto in C#?

+1

perché non dovresti dare uno sguardo a System.Net.Mail, è possibile ottenere le cose in un modo molto più semplice, ha tutti classe richiesta implementato come mailAddress, e la classe di attacco a scopo di attacco – csharpcoder

+1

Possibile duplicato: [C# MailTo con allegato?] (http://stackoverflow.com/questions/1195111/c-sharp-mailto-with-attachment) –

risposta

2

È possibile utilizzare il System.Net.Mail che ha la proprietà MailMessage.Attachments. Qualcosa di simile:

message.Attachments.Add(new Attachment(yourAttachmentPath)); 

O

Si può provare in questo modo:

using SendFileTo; 

namespace TestSendTo 
{ 
    public partial class Form1 : Form 
    { 
     private void btnSend_Click(object sender, EventArgs e) 
     { 
      MAPI mapi = new MAPI(); 

      mapi.AddAttachment("c:\\temp\\file1.txt"); 
      mapi.AddAttachment("c:\\temp\\file2.txt"); 
      mapi.AddRecipientTo("[email protected]"); 
      mapi.AddRecipientTo("[email protected]"); 
      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"); 
     } 
    } 
} 

Il codice precedente utilizza la MAPI32.dll.

Source

Probabilmente non sarà allegare il documento perché si è in libertà del client di posta elettronica per implementare il protocollo mailto e includere analisi per la clausola allegato. Potresti non sapere quale client di posta elettronica è installato sul PC, quindi potrebbe non funzionare sempre - Outlook sicuramente non supporta gli allegati tramite mailto.

+1

"Outlook non supporta certamente gli allegati tramite mailto." Funzionerà per alcune versioni ma a destra, non sai cosa sono installati i client di posta, quindi ti suggerisco di usare C# System.Net.Mail.Attachment. –

+1

@MaxSassen: - Questo è il motivo dopo aver postato la risposta, ho aggiunto l'opzione per usare 'System.Net.Mail' –

2

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

<a href='mailto:[email protected]?Subject=SubjTxt&Body=Bod_Txt&Attachment=""C:\file.txt"" '> 

Il tuo problema è già stato risposto: c-sharp-mailto-with-attachment

-1

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(
     "[email protected]", 
     "[email protected]", 
     "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(); 
} 
+2

Hai appena copiato il post: http://stackoverflow.com/questions/1195111/c-sharp-mailto -with-attachment # answer-1195153 basta collegarlo –