带附件的C#MailTo?

时间:2009-07-28 16:03:46

标签: c# attachment mailto

目前,我使用以下方法打开用户展望的电子邮件帐户,并使用相关内容填充电子邮件:

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

但是,我希望能够使用附件文件填充电子邮件。

类似的东西:

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

然而,这似乎不起作用。 有没有人知道一种方法可以让它发挥作用!?

非常感谢。

问候。

5 个答案:

答案 0 :(得分:51)

如果要访问默认电子邮件客户端,则可以使用MAPI32.dll(仅适用于Windows操作系统)。 看看下面的包装器:

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

代码如下所示:

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

答案 1 :(得分:10)

mailto:不正式支持附件。我听说Outlook 2003将使用这种语法:

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

处理此问题的更好方法是使用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();
    }

答案 2 :(得分:4)

这个应用真的需要使用Outlook吗?是否有理由不使用System.Net.Mail命名空间?

如果你真的需要使用Outlook(我不推荐它,因为你的应用程序基于可能会改变的第三方依赖项),你需要查看Microsoft.Office命名空间

我从这里开始: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx

答案 3 :(得分:2)

试试这个

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

答案 4 :(得分:0)

官方说明,mailTo协议不支持附件。但Williwyg在这里解释得很清楚,有办法做到这一点 - Open default mail client along with a attachment