使用c#在Windows Azure上的电子邮件中发送附件
本文关键字:电子邮件 Windows Azure 使用 | 更新日期: 2023-09-27 18:00:42
我正在尝试从AZURE发送电子邮件。我成功发送了无附件电子邮件。当我发送带有附件的电子邮件时,下面是我在下载附件时面临的问题。
- 当我打开该附件时,它有0个字节。我在里面找不到任何内容。当我从我的azure门户下载时,我会完美地看到内容
对于附件,我在blob上上传PDF文件并下载,然后添加附件。我的代码如下。
var account = new CloudStorageAccount(new StorageCredentials("accountName", "keyvalue"), true);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container =blobClient.GetContainerReference("containername");
CloudBlockBlob blobread = container.GetBlockBlobReference(Session["UploadPDFFile"].ToString());
MemoryStream msRead = new MemoryStream();
using (msRead)
{
msRead.Position = 0;
blobread.DownloadToStream(msRead);
objMailMessgae.Attachments.Add(new System.Net.Mail.Attachment(msRead, Session["UploadPDFFile"].ToString(), "pdf/application"));
try
{
objSmtpClient.Send(objMailMessgae);
}
catch (Exception ex) {
string s = ex.Message;
}
}
在读取blob的内容后,您需要将内存流的位置重置为0
var account = new CloudStorageAccount(new StorageCredentials("accountName", "keyvalue"), true);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container =blobClient.GetContainerReference("containername");
CloudBlockBlob blobread = container.GetBlockBlobReference(Session["UploadPDFFile"].ToString());
MemoryStream msRead = new MemoryStream();
using (msRead)
{
blobread.DownloadToStream(msRead);
msRead.Position = 0;
objMailMessgae.Attachments.Add(new System.Net.Mail.Attachment(msRead, Session["UploadPDFFile"].ToString(), "pdf/application"));
try
{
objSmtpClient.Send(objMailMessgae);
}
catch (Exception ex) {
string s = ex.Message;
}
}
试试看。它应该起作用。