【问题标题】:C# MailTo with Attachment?C# MailTo 带附件?
【发布时间】:2010-11-14 18:31:14
【问题描述】:

目前我正在使用以下方法打开用户的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");
}

但是这似乎不起作用。 有谁知道可以让它工作的方法!?

帮助不胜感激。

问候。

【问题讨论】:

标签: c# attachment mailto


【解决方案1】:

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

如果您确实需要使用 Outlook(我不推荐它,因为您的应用基于可能会更改的 3rd 方依赖项),您将需要查看 Microsoft.Office 命名空间

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 此代码对于将附件发送到默认电子邮件客户端很有用。不是每个人都使用 Outlook,所以这段代码很棒!
      • 附加文件时会引发 AccessViolationException。
      • 不错的参考!应该是标准的做法。
      • 如果您的邮件客户端已经在运行,您需要确保您的邮件客户端和您的程序以相同的权限运行。例如,如果 Outlook 以管理员身份运行,则使用此 MAPI 类的应用程序也需要以管理员身份运行。否则,您可能会收到MAPISendMail fails and returns error code 2
      【解决方案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();
      

      【讨论】:

        猜你喜欢
        • 2010-12-08
        • 2011-10-26
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-08
        • 1970-01-01
        相关资源
        最近更新 更多