繁体   English   中英

如何使用 System.Net.Mail 向电子邮件添加附件?

[英]How do I add an attachment to an email using System.Net.Mail?

我有一个表示为 byte[] 的 excel 文档,我想将它作为电子邮件中的附件发送。

我在构建附件时遇到了一些麻烦。

我可以创建一个具有以下构造函数的附件:

(Stream contentStream, ContentType contentType)
(Stream contentStream, string name)
(Stream contentStream, string name, string mediaType)

我目前的想法是从 byte[] 创建一个 MemoryStream 并将其传递给创建附件的方法。

不幸的是,我看不到从 MemoryStream 获取预期文件名和内容类型的方法,也看不到如何提供正确的内容类型。 有纯文本、Pdf、Rtf 等选项,但没有一个我能立即跳出来作为我应该用于 Excel 文档的选项。

我能找到的最接近的是MediaTypeNames.Application.Octet ,它指出:

Octet 成员指定附件包含通用二进制数据。

但是,即使这是要使用的,除非它可以作为 Stream 的属性传递,否则我发送电子邮件的方法只能将 byte[] 作为 Excel 文档发送...

也许我可以使用其他类型的流? 或者我是否必须创建自己类型的 Stream,其中包含我需要的详细信息。

肯定有人以前做过这件事,而且微软肯定会考虑到这个程度......

任何帮助将非常感激。

更新:请不要投票支持使用将文件名作为字符串的构造函数的任何答案。 我真的需要使用 Stream 的帮助...我想避免将文件写入磁盘,通过电子邮件发送,然后立即将其删除。 由于有一种方法可以让我这样做,如果可能的话,我想使用该方法。

解决方案更新

康拉德设法找到了我要找的东西! 谢谢堆人!

我只会记录建议的解决方案,以防万一提供的链接中的内容发生问题。

此解决方案归功于 www.systemnetmail.com

static void AttachmentFromStream()
{

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this content is in the body";

//Get some binary data
byte[] data = GetData();

//save the data to a memory stream
MemoryStream ms = new MemoryStream(data);

//create the attachment from a stream. Be sure to name the data 
//with a file and 
//media type that is respective of the data
mail.Attachments.Add( new Attachment( ms, "example.txt", "text/plain" ));

SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
}

就我而言,这只是意味着我必须更改我的方法以将文件名和文件格式作为字符串。 我将尝试使用 Octet 一个...但如果失败,我将只传递官方 MIME 类型。

考虑到所有因素,这是一个非常明显的解决方案......但我确实很感激解决它的帮助......好消息是这个解决方案将为未来遇到相同问题的程序员记录。

再次感谢大家的帮助!

附件构造函数确实有一个构造函数可以满足您的需要。 我假设您正在使用 .NET Framework 2 中的 System.Net.MailMessage 类。如果是这样,请阅读此链接以获取您需要的一些示例代码

由于已接受答案链接已消失,因此它来自Wayback Machine

TL; DR: mail.Attachments.Add(new Attachment(contentStream, "yourfilename.txt", "text/plain"));

满的:

static void AttachmentFromStream()
{

    //create the mail message
    MailMessage mail = new MailMessage();

    //set the addresses
    mail.From = new MailAddress("me@mycompany.com");
    mail.To.Add("you@yourcompany.com");

    //set the content
    mail.Subject = "This is an email";
    mail.Body = "this content is in the body";

    //Get some binary data
    byte[] data = GetData();

    //save the data to a memory stream
    MemoryStream ms = new MemoryStream(data);

    //create the attachment from a stream. Be sure to name the data with a file and 
    //media type that is respective of the data
    mail.Attachments.Add(new Attachment(ms, "example.txt", "text/plain"));

    //send the message
    SmtpClient smtp = new SmtpClient("127.0.0.1");
    smtp.Send(mail);
}
static byte[] GetData()
{
    //this method just returns some binary data.
    //it could come from anywhere, such as Sql Server
    string s = "this is some text";
    byte[] data = Encoding.ASCII.GetBytes(s);
    return data;
}

从 Microsoft 文档中得到了这个答案:

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 email 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 email 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());
    }
    // Display the values in the ContentDisposition for the attachment.
    ContentDisposition cd = data.ContentDisposition;
    Console.WriteLine("Content disposition");
    Console.WriteLine(cd.ToString());
    Console.WriteLine("File {0}", cd.FileName);
    Console.WriteLine("Size {0}", cd.Size);
    Console.WriteLine("Creation {0}", cd.CreationDate);
    Console.WriteLine("Modification {0}", cd.ModificationDate);
    Console.WriteLine("Read {0}", cd.ReadDate);
    Console.WriteLine("Inline {0}", cd.Inline);
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
    foreach (DictionaryEntry d in cd.Parameters)
    {
        Console.WriteLine("{0} = {1}", d.Key, d.Value);
    }
    data.Dispose();
}

--------------- 我猜我错了,这是如果您有要附加的文件 ---------------

看起来这里有一个发送带有附件的邮件的例子:

http://www.aspnettutorials.com/tutorials/email/email-attach-aspnet2-csharp.aspx

我希望这就是你要找的。

Attachment 构造函数中的 name 参数是将在收件人电子邮件中为附件显示的名称。

这样就可以自由选择name参数(扩展名.xls首选),将mediaType参数设置为“application/vnd.ms-excel”,这是excel文件定义的MIME类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM