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

AWS SES无法添加文件附件的技术求助

AWS SES无法添加文件附件的技术求助

嘿,我看你已经把AWS SES SMTP发送邮件的核心逻辑搭好了,现在就差添加附件这一步啦!其实操作很简单,只需要在构建MailMessage的时候,把本地的CSV文件加入到邮件的附件集合里就行。

你代码里已经定义了附件路径String attachment = "C:\\20231030.csv";,但还没把它关联到邮件对象上。下面是修改后的完整代码,我特意把添加附件的部分标出来了:

// Amazon SES
// Requires a AWS account and setup

// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
String FROM = "*****@gmail.com";

String FROMNAME = "Danny";

// Replace recipient@example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
String TO = s;

// Replace smtp_username with your Amazon SES SMTP user name.
String SMTP_USERNAME = "****";

// Replace smtp_password with your Amazon SES SMTP password.
String SMTP_PASSWORD = "*********";

// (Optional) the name of a configuration set to use for this message.
// If you comment out this line, you also need to remove or comment out
// the "X-SES-CONFIGURATION-SET" header below.
String CONFIGSET = "ConfigSet";

// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate AWS Region.
String HOST = "email-smtp.us-east-2.amazonaws.com";

// The port you will connect to on the Amazon SES SMTP endpoint. We
// are choosing port 587 because we will use STARTTLS to encrypt
// the connection.
int PORT = 587;

// The subject line of the email
String SUBJECT =
"Amazon SES test (SMTP interface accessed using C#)";

String attachment = "C:\\20231030.csv";

// The body of the email
String BODY =
"<h1>Amazon SES Test</h1>" +
"<p>This email was sent through the " +
"<a href='https://aws.amazon.com/ses'>Amazon SES</a> SMTP interface " +
"using the .NET System.Net.Mail library.</p>" +
"<p>Last Hours Total was " + txtLastHour.Text + "<p>" + "" +
"<p>Yesterdays Total was " + txtTotal.Text + " " + "" +
"<p>Total Bad Reads from Yesterday " + txtBadRead.Text + "<p> " + "" +
"<p>Total Jackpot from Yesterday " + txtJackpot.Text + "<p> " + "";

// Create and build a new MailMessage object
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(FROM, FROMNAME);
message.To.Add(new MailAddress(TO));
message.Subject = SUBJECT;
message.Body = BODY;

// --- 新增:添加附件的核心代码 ---
try
{
    // 根据指定路径创建附件对象
    Attachment csvAttachment = new Attachment(attachment);
    // 将附件添加到邮件的附件集合中
    message.Attachments.Add(csvAttachment);
}
catch (Exception ex)
{
    Console.WriteLine("添加附件时出错: " + ex.Message);
}
// --- 附件代码结束 ---

// Comment or delete the next line if you are not using a configuration set
//message.Headers.Add("X-SES-CONFIGURATION-SET", CONFIGSET);

using (var client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
    client.UseDefaultCredentials = false;
    // Pass SMTP credentials
    client.Credentials = new NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
    // Enable SSL encryption
    client.EnableSsl = true;
    // Try to send the message. Show status in console.
    try
    {
        Console.WriteLine("Attempting to send email...");
        client.Send(message);
        Console.WriteLine("Email sent!");
    }
    catch (Exception ex)
    {
        Console.WriteLine("The email was not sent.");
        Console.WriteLine("Error message: " + ex.Message);
    }
}

最后给你几个小提醒:

  • 确保C:\\20231030.csv这个文件确实存在,并且运行程序的账号有读取该文件的权限
  • 如果是在服务器或云环境中运行,尽量不要用本地绝对路径,改用相对路径或者云存储的临时挂载路径
  • AWS SES单封邮件的总大小(包括附件)不能超过10MB,注意附件大小不要超标

这样修改后,发送邮件时就会自动带上你指定的CSV附件啦!

备注:内容来源于stack exchange,提问作者Kaptindan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.21 09:49:38