C#如何发送带附件的邮件?

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

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

非常感谢帮助。

敬礼。


请返回翻译后的文本。 - sondra.kinsey
4个回答

57

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

4
这段代码可用于向默认的电子邮件客户端发送附件。并非所有人都使用Outlook,因此这段代码非常实用! - Brent
当我附加一个文件时,这会抛出一个AccessViolationException异常。 - Sinaesthetic
不错的参考!应该成为标准做法。 - LueTm
1
如果您的邮件客户端已经在运行,您需要确保您的邮件客户端和程序使用相同的权限运行。例如,如果Outlook正在以管理员身份运行,则使用此“MAPI”类的应用程序也需要以管理员身份运行。否则,您可能会遇到“MAPISendMail失败并返回错误代码2”的问题。 - Daniel Bonetti
Mapi能够与Web应用程序一起使用吗? - It's Siddiqi

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

4

这个应用程序真的需要使用Outlook吗?不使用System.Net.Mail命名空间有什么原因吗?

如果你确实需要使用Outlook(我不建议这样做,因为那样你的应用程序将基于第三方依赖项,并且很可能会发生变化),那么你需要研究Microsoft.Office命名空间。

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


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接