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

调用SetAsm后未收到SendGrid邮件的C#技术问题排查

问题:SendGrid调用SetAsm后纯文本邮件无法送达(已获取Transaction ID)

我尝试让SendGrid在纯文本邮件中替换退订标签,但调用SetAsm方法后始终无法收到邮件。调用SendGridClient.SendEmailAsync已返回transactionId,但收件方未收到邮件。怀疑是退订组配置或代码逻辑问题,相关C#代码如下:

public static async Task<string> SendAsync(Email email)
{
    SendGridMessage mailMessage = email.Convert();
    return await SendAsync(mailMessage);
}

public static SendGridMessage Convert(this Email email)
{
    SendGridMessage mailMessage = new SendGridMessage();

    mailMessage.From = new EmailAddress(email.From);
    if (email.Destination.Contains(Separator.SEMICOLON))
    {
        List<EmailAddress> destinationList = new List<EmailAddress>();
        foreach (var mailTo in email.Destination.Split(char.Parse(Separator.SEMICOLON)))
        {
            if (!string.IsNullOrEmpty(mailTo))
            {
                destinationList.Add(new EmailAddress(mailTo.Trim()));
            }
        }
        mailMessage.AddTos(destinationList);
    }
    else
    {
        mailMessage.AddTo(email.Destination);
    }

    mailMessage.Subject = email.Subject;
        
    string unsubscribeLink = "<a href=\"<% asm_group_unsubscribe_raw_url %>\">Unsubscribe from Us</a>";

    int unsubscribeGrpId = Configuration.Configuration.SendGridUnsubscribeGroupID;
    
    if (unsubscribeGrpId > 0)
    {                
        email.Message = unsubscribeLink;
        mailMessage.SetAsm(unsubscribeGrpId, new List<int> { unsubscribeGrpId });                   
    }

    mailMessage.AddContent(MimeType.Text, email.Message);

    return mailMessage;
}

private static async Task<string> SendAsync(SendGridMessage mailMessage)
{
    SendGrid.SendGridClient sendGridClient = new SendGrid.SendGridClient(ApiKey);
    Response response = await sendGridClient.SendEmailAsync(mailMessage).ConfigureAwait(false);
}

排查与解决方案

  • 纯文本内容格式错误:你在纯文本邮件中插入了HTML标签<a href="...">...</a>,SendGrid的纯文本内容不会解析HTML,这类混合格式可能触发内容过滤机制导致邮件被拦截。纯文本退订链接应改为纯文本格式:

    string unsubscribeLink = "Unsubscribe from Us: <% asm_group_unsubscribe_raw_url %>";
    
  • 退订组配置验证:

    • 确认SendGridUnsubscribeGroupID对应的退订组在SendGrid后台「Unsubscribe Groups」中已创建且处于启用状态。
    • 检查退订组的默认落地页(Default Landing Page)配置正常,无无效URL或错误设置。
  • SetAsm参数逻辑:SetAsm(groupId, groupsToDisplay)中,两个参数传同一ID逻辑上没问题,但需确保groupId是后台存在的有效ID。

  • 后台日志排查:登录SendGrid后台,进入「Email Activity」,通过transactionId查找对应邮件,查看具体状态(如被标记为垃圾邮件、拦截、或被收件箱过滤),这是最直接的问题定位方式。

  • API权限验证:确认使用的SendGrid API Key拥有mail.send权限,且账号无发送限制。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 12:28:26