31

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

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

帮助不胜感激。

问候。

4

4 回答 4

56

如果您想访问默认的电子邮件客户端,则可以使用 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");
于 2009-08-29T20:14:10.413 回答
11

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();
    }
于 2009-07-28T16:12:38.670 回答
4

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

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

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

于 2009-07-28T16:09:28.550 回答
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();
于 2014-08-27T13:22:34.667 回答