【问题标题】:Is it possible to create an email-attachment on a Silverlight email?是否可以在 Silverlight 电子邮件上创建电子邮件附件?
【发布时间】:2011-08-07 20:15:06
【问题描述】:

我需要能够从 silverlight 客户端应用程序发送电子邮件。 我通过实现由应用程序使用的 web 服务来完成这项工作。 问题是现在我需要能够为正在发送的电子邮件添加附件。

我已经阅读了各种帖子,尝试了十几次自己弄清楚,但没有成功。

所以现在我发现自己想知道这是否可能?

主要问题是附件集合需要可序列化。所以,通过这个,ObservableCollection - 类型(FileInfo)不起作用,ObservableCollection - 类型(对象)不起作用......我尝试使用 List - 类型(Stream),它可以序列化,但后来我做了不知道如何在 web 服务端创建文件,因为流对象没有名称(这是我尝试分配给附件对象的第一件事,然后将其添加到 message.attachments 中)...我在这里有点陷入困境。

请问有人能解释一下吗?

【问题讨论】:

    标签: web-services email silverlight-4.0 attachment


    【解决方案1】:

    我想出了如何做到这一点,它并没有看起来那么困难。 在您的 web 服务命名空间中创建以下内容: `

    [Serializable]
    public class MyAttachment
    {
        [DataMember]
        public string Name { get; set; }
    
        [DataMember]
        public byte[] Bytes { get; set; }
    }`
    

    然后将以下内容添加到您的网络方法参数中: MyAttachment[] attachment

    在您的网络方法的执行块中添加以下内容:`

                foreach (var item in attachment)
                {
                    Stream attachmentStream = new MemoryStream(item.Bytes);
                    Attachment at = new Attachment(attachmentStream, item.Name);
    
                    msg.Attachments.Add(at);
                }`
    

    在您的客户端创建以下属性(或类似属性): `

        private ObservableCollection<ServiceProxy.MyAttachment> _attachmentCollection;
        public ObservableCollection<ServiceProxy.MyAttachment> AttachmentCollection
        {
            get { return _attachmentCollection; }
            set { _attachmentCollection = value; NotifyOfPropertyChange(() => AttachmentCollection); }
        }`
    

    在构造函数中新建公共属性 (AttachmentCollection)。 在 OpenFileDialog 应该返回文件的地方添加以下内容:`

    如果 (openFileDialog.File != null)

            {
                foreach (FileInfo fi in openFileDialog.Files)
                {
                    var tempItem = new ServiceProxy.MyAttachment();
                    tempItem.Name = fi.Name;
    
                    var source = fi.OpenRead();
                    byte[] byteArray = new byte[source.Length];
                    fi.OpenRead().Read(byteArray, 0, (int)source.Length);
                    tempItem.Bytes = byteArray;
                    source.Close();
    
                    AttachmentCollection.Add(tempItem);
                }
    
            }`
    

    最后,在您调用网络方法发送电子邮件的地方,添加以下内容(或类似内容):

    MailSvr.SendMailAsync(FromAddress, ToAddress, Subject, MessageBody, AttachmentCollection);

    这对我有用,附件随邮件一起发送,其所有数据与原始文件完全相同。

    【讨论】:

    • 这行得通,但我改用“lesnikowski”来处理我的邮件,不得不承认,mail.dll 工作得很好,而且很容易实现。
    猜你喜欢
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2016-11-30
    • 2013-07-25
    • 1970-01-01
    • 2017-06-03
    • 2011-09-02
    相关资源
    最近更新 更多