代码之家  ›  专栏  ›  技术社区  ›  mezoid

如何使用系统向电子邮件添加附件。网。邮件?

  •  23
  • mezoid  · 技术社区  · 16 年前

    我有一个以字节[]表示的excel文档,我想将其作为电子邮件的附件发送。

    我在构建附件时遇到了一些困难。

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

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

    我现在的想法是从字节[]创建一个MemoryStream,并将其传递给创建附件的方法。

    不幸的是,我无法从MemoryStream中获得预期的文件名和内容类型,也无法提供正确的内容类型。有纯文本、Pdf、Rtf等选项,但我看不到任何一个选项会像我应该在Excel文档中使用的那样立即跳出来。

    我能找到的最近的是 MediaTypeNames.Application.Octet 其中指出:

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

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

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

    肯定有人以前做过这件事,微软肯定会把这件事想到这个地步。...

    任何帮助都将不胜感激。

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

    解决方案更新

    康拉德设法找到了我要找的东西!谢谢你,伙计!

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

    此解决方案的功劳归于 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);
    }
    

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

    综合考虑,这是一个非常明显的解决方案。..但我确实很感激在解决这个问题时得到的帮助……好的是,这个解决方案将被记录下来,供未来有同样问题的程序员使用。

    再次感谢大家的帮助!

    5 回复  |  直到 16 年前
        1
  •  15
  •   Rad    16 年前

    附件构造函数确实有一个构造函数可以执行您需要的操作。我假设你正在使用该系统。网。来自的MailMessage类。NET框架2。如果这样 read this link 获取您所需的一些示例代码

        2
  •  7
  •   drzaus tranceporter    8 年前

    自从 link accepted answer 不见了,这是从 Wayback Machine

    太长,读不下去了 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;
    }
    
        3
  •  1
  •   Michel El Hajj    4 年前

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

    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();
    }
    
        4
  •  0
  •   devio    16 年前

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

    因此,您可以自由选择name参数(首选扩展名.xls),并将mediaType参数设置为“application/vnd.ms-excel”,这是定义的 MIME type 用于excel文件。

        5
  •  0
  •   John Boker    16 年前

    ---------------我想我错了,如果你有一个要附加的文件---------------

    这里似乎有一个发送带有附件的邮件的示例:

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

    我希望这就是你要找的。

    推荐文章