You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中不使用本地文件系统添加Azure Blob文件作为邮件附件?

直接将Azure Blob文件作为邮件附件发送(无需本地文件系统)

完全可以实现,核心思路是直接读取Azure Blob的数据流,将其作为邮件附件的数据源,不需要把文件下载到本地磁盘。以下是具体实现方案:

依赖准备

需要安装两个核心NuGet包:

  • Azure Blob存储操作:Azure.Storage.Blobs
  • 邮件发送库(推荐更现代的MailKit,兼容.NET所有版本):MailKit

实现代码(MailKit版本)

using Azure.Storage.Blobs;
using MailKit.Net.Smtp;
using MimeKit;
using System.IO;

public async Task SendEmailWithBlobAttachmentAsync(
    string blobConnectionString, 
    string containerName, 
    string blobName, 
    string smtpServer, 
    int smtpPort, 
    string smtpUser, 
    string smtpPassword, 
    string toEmail, 
    string subject, 
    string body)
{
    // 1. 获取Blob的只读数据流
    var blobServiceClient = new BlobServiceClient(blobConnectionString);
    var blobContainerClient = blobServiceClient.GetBlobContainerClient(containerName);
    var blobClient = blobContainerClient.GetBlobClient(blobName);

    using var blobStream = await blobClient.OpenReadAsync();
    var blobProps = await blobClient.GetPropertiesAsync();

    // 2. 构建邮件及附件
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("发送方", smtpUser));
    message.To.Add(new MailboxAddress("接收方", toEmail));
    message.Subject = subject;

    var bodyBuilder = new BodyBuilder();
    bodyBuilder.TextBody = body;
    // 直接将Blob流作为附件传入,自动获取文件的MIME类型
    bodyBuilder.Attachments.Add(blobName, blobStream, ContentType.Parse(blobProps.Value.ContentType));

    message.Body = bodyBuilder.ToMessageBody();

    // 3. 发送邮件
    using var smtpClient = new SmtpClient();
    await smtpClient.ConnectAsync(smtpServer, smtpPort, MailKit.Security.SecureSocketOptions.StartTls);
    await smtpClient.AuthenticateAsync(smtpUser, smtpPassword);
    await smtpClient.SendAsync(message);
    await smtpClient.DisconnectAsync(true);
}

备选方案(System.Net.Mail版本,适用于.NET Framework或兼容场景)

using Azure.Storage.Blobs;
using System.Net;
using System.Net.Mail;
using System.IO;

public async Task SendEmailWithBlobAttachmentNetMailAsync(
    string blobConnectionString, 
    string containerName, 
    string blobName, 
    string smtpServer, 
    int smtpPort, 
    string smtpUser, 
    string smtpPassword, 
    string toEmail, 
    string subject, 
    string body)
{
    var blobServiceClient = new BlobServiceClient(blobConnectionString);
    var blobContainerClient = blobServiceClient.GetBlobContainerClient(containerName);
    var blobClient = blobContainerClient.GetBlobClient(blobName);

    using var blobStream = await blobClient.OpenReadAsync();
    var blobProps = await blobClient.GetPropertiesAsync();

    // 创建附件时直接传入Blob流
    var attachment = new Attachment(blobStream, blobName, blobProps.Value.ContentType);

    var mailMessage = new MailMessage();
    mailMessage.From = new MailAddress(smtpUser);
    mailMessage.To.Add(toEmail);
    mailMessage.Subject = subject;
    mailMessage.Body = body;
    mailMessage.Attachments.Add(attachment);

    using var smtpClient = new SmtpClient(smtpServer, smtpPort)
    {
        Credentials = new NetworkCredential(smtpUser, smtpPassword),
        EnableSsl = true
    };
    await smtpClient.SendMailAsync(mailMessage);
}

关键注意事项

  • 权限验证:确保Blob客户端拥有Blob的读取权限,可以通过连接字符串、SAS令牌或Azure AD身份验证实现。
  • 资源释放:使用using语句包裹Blob流,确保数据流使用后自动释放,避免内存泄漏。
  • 大文件支持:流式处理无需将整个文件加载到内存,适合大文件场景。

内容的提问来源于stack exchange,提问作者AZ Chad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 10:41:31