C#的MailTo带附件?(C# MailTo with Attachment?)

2019-06-17 17:45发布

目前我使用下面的方法来打开用户的Outlook电子邮件帐户和填充发送相关内容的电子邮件:

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

然而,这似乎并没有工作。 有谁知道的一种方式,这将使这个工作!?

帮助非常感谢。

问候。

Answer 1:

邮寄地址:不正式支持附件。 我听说过的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();
    }


Answer 2:

如果您想访问默认的电子邮件客户端,那么你可以使用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");


Answer 3:

这是否应用确实需要使用Outlook? 是否有未使用System.Net.Mail命名空间的原因是什么?

如果你确实需要使用Outlook(和我不会推荐它,因为那时你在第三方的依赖有可能改变你的基础应用程序),您需要考虑的Microsoft.Office命名空间

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



Answer 4:

试试这个

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


Answer 5:

据官方统计是mailto协议不支持附件。 但Williwyg解释非常好这里是有办法做到这一点- 打开默认邮件客户端与附件一起



文章来源: C# MailTo with Attachment?