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

为何无法使用Amazon SES服务发送邮件?附C#代码求助

Amazon SES SMTP 无法发送邮件问题排查与解决

问题背景

已退出Amazon SES沙箱,使用US East (Ohio)区域的SMTP凭证搭建网站实现用户间邮件发送;身处加拿大,发送方与接收方均为加拿大yahoo.ca邮箱,但用户1无法向用户2发送邮件。

相关代码

public void SendEmail(string sender, string recipient, string messageBODY)
{
    // Replace sender@example.com with your "From" address. 
    // This address must be verified with Amazon SES.
    String FROM = sender;
    String FROMNAME = "Sender Name";

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

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

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

    // (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#)";

    // The body of the email
    String BODY = messageBODY;

    // 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;
    // 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))
    {
        // 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);
        }
    }
}

排查与解决步骤

  1. 获取完整错误信息
    当前代码仅输出ex.Message,无法定位具体问题。修改catch块输出完整异常详情,包括内部异常和堆栈跟踪:

    catch (Exception ex)
    {
        Console.WriteLine("The email was not sent.");
        Console.WriteLine("Full error details: " + ex.ToString());
    }
    

    完整信息能帮你确认是SMTP认证失败、配额超限、内容被判定为垃圾邮件还是其他问题。

  2. 验证发件人地址
    即使退出沙箱,Amazon SES要求发件人地址必须完成验证。登录SES控制台,确认发送方的yahoo.ca邮箱已通过验证流程,未验证的地址会被直接拒绝发送请求。

  3. 检查SES发送配额
    在SES控制台查看发送统计,确认是否超过每日发送限额或每秒发送速率。如果配额不足,可申请提升限额。

  4. 排查收件方反垃圾策略

    • 检查接收方yahoo.ca邮箱的垃圾邮件文件夹,邮件可能被归类为垃圾邮件而非直接退回。
    • 简化测试邮件内容(比如仅发送纯文本"Test Email"),避免包含敏感词、可疑链接或复杂HTML,降低被反垃圾系统拦截的概率。
  5. 确认SMTP配置正确性

    • 当前代码中HOST(email-smtp.us-east-2.amazonaws.com)、端口587、EnableSsl=true的配置与US East (Ohio)区域匹配,无需修改。
    • 确认使用的SMTP用户名和密码是SES控制台生成的专用SMTP凭证,而非AWS Access Key。
  6. 替换过时的SmtpClient
    .NET原生System.Net.Mail.SmtpClient已被标记为过时,存在兼容性风险。建议替换为MailKit库的SmtpClient,示例代码如下:

    using MailKit.Net.Smtp;
    using MimeKit;
    
    public void SendEmail(string sender, string recipient, string messageBODY)
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Sender Name", sender));
        message.To.Add(new MailboxAddress("", recipient));
        message.Subject = "Amazon SES test (SMTP interface accessed using C#)";
        message.Body = new TextPart("html") { Text = messageBODY };
    
        using (var client = new SmtpClient())
        {
            client.Connect("email-smtp.us-east-2.amazonaws.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
            client.Authenticate("CAIASJA3JM5CXUEYAIAB", "RD75K2MbhvOYnBf20WYZ5O9oZlj9lCBH/hqrEIYnClYT");
            
            try
            {
                client.Send(message);
                Console.WriteLine("Email sent!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("The email was not sent.");
                Console.WriteLine("Full error details: " + ex.ToString());
            }
            finally
            {
                client.Disconnect(true);
            }
        }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 21:55:34