Pergunta

Atualmente estou usando o abaixo método para abrir a conta de e-mail aos usuários do Outlook e preencher um e-mail com o conteúdo relevante para o envio:

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

Eu quero no entanto, ser capaz de preencher o e-mail com um arquivo anexado.

algo como:

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

No entanto, este não parece trabalho. Alguém sabe de uma maneira que permitirá que isso funcione!?

Ajuda apreciamos muito.

Cumprimentos.

Foi útil?

Solução

mailto: não suporta oficialmente anexos. Eu ouvi Outlook 2003 irá trabalhar com esta sintaxe:

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

A melhor maneira de lidar com isso é para enviar o e-mail no servidor usando 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();
    }

Outras dicas

Se você quiser acessar o cliente de email padrão, então você pode usar Mapi32.dll (trabalhos no sistema operacional Windows somente). Dê uma olhada no seguinte embalagem:

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

código parecido com este:

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");

Será que este aplicativo realmente precisa usar o Outlook? Existe uma razão para não usar o namespace System.Net.Mail?

Se você realmente precisa usar o Outlook (e eu não recomendo, porque então você está baseando a sua aplicação em dependências do 3o partido que são susceptíveis de mudança) você terá que olhar para o Microsoft.Office namespaces

Eu começaria aqui: http://msdn.microsoft.com/en- us / library / microsoft.office.interop.outlook.aspx

Tente este

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();

Officially sim o protocolo mailto não suporta os Anexos. Mas Williwyg explicou isso muito bem aqui que há uma maneira de fazer isso - Abrir cliente de email padrão juntamente com uma anexo

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top