Compose Email with attachment on Windows Phone 8.1 Silverlight application

吃可爱长大的小学妹 提交于 2019-12-12 06:11:50

问题


Here's my function (based on this: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn632427.aspx):

    private async void ComposeEmail(String subject, String messageBody, String attachmentFile)
    {
        var msg = new EmailMessage();
        msg.Subject = subject;
        msg.Body = messageBody;
        msg.To.Add(new EmailRecipient("example@example.com"));

        if (attachmentFile != null && attachmentFile != "")
        {
            bool exists = File.Exists(attachmentFile);
            Debug.WriteLine("File: " + attachmentFile + (exists ? " exists" : " not exists"));

            try
            {
                StorageFile fl = await StorageFile.GetFileFromPathAsync(attachmentFile);
                IRandomAccessStream stream = await fl.OpenAsync(FileAccessMode.Read);
                var rastream = RandomAccessStreamReference.CreateFromStream(stream);
                var attachment = new EmailAttachment(attachmentFile, rastream);
                msg.Attachments.Add(attachment);
            }
            catch (Exception e)
            {
                Debug.WriteLine("[C#] attachment exception: " + e.Message);
            }

        }

        try
        {
            await EmailManager.ShowComposeNewEmailAsync(msg);
        }
        catch (Exception e)
        {
            Debug.WriteLine("[C#] email manager exception: " + e.Message);
        }

    }

And here's an output:

File: C:\Data\Users\DefApps\APPDATA\Local\Packages\92319996-2154-4e16-91ac-c74ccb656b65_60jzptcn15482\AC\Temp\screenshot.png exists

[C#] email manager exception: The handle with which this oplock was associated has been closed. The oplock is now broken. (Exception from HRESULT: 0x80070323)

First of all file exist, I tried loading it and displaying in image and it works. So this is not an issue with permissions. Second of all if I won't add an attachment (simply commenting out the code) the presented exception won't be raised and I'd see mail composer with correct subject, body etc.


回答1:


Thanks @Will for hint.

Here's a working code:

private StorageFile storageFile;

private async void ComposeEmail(String subject, String messageBody, StorageFile fl)
    {
        var msg = new EmailMessage();
        msg.Subject = subject;
        msg.Body = messageBody;
        msg.To.Add(new EmailRecipient("contact@123kidsfun.com"));

        String attachmentFile = fl.Name;

        if (fl != null && attachmentFile != "")
        {
            try
            {
                var rastream = RandomAccessStreamReference.CreateFromFile(fl);
                var attachment = new EmailAttachment(attachmentFile, rastream);
                msg.Attachments.Add(attachment);
            }
            catch (Exception e)
            {
                Debug.WriteLine("[C#] attachment exception: " + e.Message);
            }
        }

        try
        {
            await EmailManager.ShowComposeNewEmailAsync(msg);
        }
        catch (Exception e)
        {
            Debug.WriteLine("[C#] email manager exception: " + e.Message);
        }
    }

    public void SendEmail(String subject, String body, String attachmentPath)
    {
        Debug.WriteLine("[C#] sendEmail(" + subject + ", " + body + ", " + attachmentPath + ")");
        Dispatcher.BeginInvoke(() =>
        {
            CreateEmail(subject, body, attachmentPath);
        });
    }

    private async void CreateEmail(String subject, String body, String path)
    {
        storageFile = await StorageFile.GetFileFromPathAsync(path);
        ComposeEmail(subject, body, storageFile);
    }


来源:https://stackoverflow.com/questions/28631498/compose-email-with-attachment-on-windows-phone-8-1-silverlight-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!