在 Windows Phone 8.1 Silverlight 应用程序上撰写带附件的电子邮件
Compose Email with attachment on Windows Phone 8.1 Silverlight application
这是我的函数(基于此: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);
}
}
这是一个输出:
File: C:\Data\Users\DefApps\APPDATA\Local\Packages319996-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)
首先,文件存在,我尝试加载它并在图像中显示,它工作正常。所以这不是权限问题。
其次,如果我不添加附件(只是注释掉代码),则不会引发出现的异常,我会看到邮件编辑器具有正确的主题、正文等。
感谢@Will 的提示。
这是一个工作代码:
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://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);
}
}
这是一个输出:
File: C:\Data\Users\DefApps\APPDATA\Local\Packages319996-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)
首先,文件存在,我尝试加载它并在图像中显示,它工作正常。所以这不是权限问题。 其次,如果我不添加附件(只是注释掉代码),则不会引发出现的异常,我会看到邮件编辑器具有正确的主题、正文等。
感谢@Will 的提示。
这是一个工作代码:
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);
}