开发者

Is it possible to create an email-attachment on a Silverlight email?

开发者 https://www.devze.com 2023-02-27 06:27 出处:网络
I need to be able to send an email from a silverlight client-side application. I\'ve got this working by implementing a webservice which is consumed by the application.

I need to be able to send an email from a silverlight client-side application. I've got this working by implementing a webservice which is consumed by the application. The problem is that now I need to be able to add an attachment to the emails that are being sent.

I have read various posts, tried a dozen times to figure it out by myself, but to no prevail.

So now I find myself wondering if this is even possible?

The main issue is that the collection of attachments needs to be serializable. So, going by this, ObservableCollection - of type(FileInfo) is not working, ObservableCollection - of type (object) is not working... I've tried using List - of type(Stream), which serializes, but then i do not know how to create the file on the webservice side, as the stream-object does not have a name (which is the first thing I tried to assign to the Attachment object which will then be added to the message.attachments)... I开发者_如何学编程'm kind of stuck in a rut here.

Can anybody maybe shed some light on this please?


I figured out how to do this, and it wasn't really as difficult as it appeared. Create the following in your webservice-namespace: `

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

    [DataMember]
    public byte[] Bytes { get; set; }
}`

Then add the following to your web-method parameters: MyAttachment[] attachment

Add the following in the execution blocks of your web-method:`

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

                msg.Attachments.Add(at);
            }`

Create the following property (or something similar) at your client-side: `

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

New up the public property (AttachmentCollection) in the constructor. Add the following where your OpenFileDialog is supposed to return files:`

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

        }`

Then finally where you call your web-method to send the email, add the following (or something similar):

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

This works for me, the attachment is sent with the mail, with all of its data exactly like the original file.

0

精彩评论

暂无评论...
验证码 换一张
取 消