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

使用C# REST API触发SFMC中Content Builder的Triggered Send邮件遇404问题

SFMC Triggered Send 问题解决与REST API示例

一、FuelSDK初始化404错误修复

你的ETClient初始化出现404,核心原因是认证端点配置错误或参数传递不完整,以下是修正方案:

  1. 完善ETClient参数配置
    不需要手动拼接认证URL,直接在参数中指定官方认证端点和SOAP端点,确保参数包含完整的必要信息:

    NameValueCollection parameters = new NameValueCollection();
    parameters.Add("clientId", clientId);
    parameters.Add("clientSecret", clientSecret);
    parameters.Add("accountId", "你的实际MID(Member ID)");
    // 生产环境认证端点,沙箱环境替换为https://auth-test.exacttargetapis.com/v1/requestToken
    parameters.Add("authEndpoint", "https://auth.exacttargetapis.com/v1/requestToken");
    parameters.Add("soapEndpoint", "https://webservice.exacttarget.com/Service.asmx");
    
    ETClient myclient = new ETClient(parameters);
    
  2. 修正代码语法错误
    myTest@test.com需作为字符串常量添加引号,否则会编译失败:

    subscriber.EmailAddress = "myTest@test.com";
    subscriber.SubscriberKey = "myTest@test.com";
    
  3. 补充邮件变量传递
    邮件中的%%=v(@EVENT)=%%等变量,需要通过Subscriber的Attributes字段传入:

    subscriber.Attributes = new List<SubscriberAttribute>
    {
        new SubscriberAttribute { Name = "EVENT", Value = myEvent },
        new SubscriberAttribute { Name = "Name", Value = myName }
    };
    

二、REST API触发发送邮件示例

如果不想依赖FuelSDK,直接调用SFMC REST API触发发送的步骤如下:

1. 获取认证Token

发送POST请求到认证端点,请求体包含身份信息:

{
    "clientId": "你的Client ID",
    "clientSecret": "你的Client Secret",
    "accountId": "你的实际MID"
}

响应中会返回accessToken和instanceUrl,后续请求需要用到这两个值。

2. 触发邮件发送

使用获取到的accessToken,发送POST请求到消息触发端点,请求体包含收件人信息和变量:

{
    "To": {
        "Address": "myTest@test.com",
        "SubscriberKey": "myTest@test.com",
        "ContactAttributes": {
            "SubscriberAttributes": {
                "EVENT": "13989",
                "Name": "John Adams"
            }
        }
    }
}

请求头需添加:

  • Authorization: Bearer {accessToken}
  • Content-Type: application/json

C# 调用REST API示例代码

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;

public async Task TriggerSendViaRest(string triggeredSendKey, string clientId, string clientSecret, string mid)
{
    // 1. 获取认证Token
    var authClient = new HttpClient();
    var authPayload = new 
    {
        clientId = clientId,
        clientSecret = clientSecret,
        accountId = mid
    };
    var authRequest = new StringContent(
        JsonSerializer.Serialize(authPayload),
        Encoding.UTF8,
        "application/json");
    
    var authResponse = await authClient.PostAsync("https://auth.exacttargetapis.com/v1/requestToken", authRequest);
    authResponse.EnsureSuccessStatusCode();
    var authData = JsonSerializer.Deserialize<AuthResponse>(await authResponse.Content.ReadAsStringAsync());

    // 2. 触发邮件发送
    var sendClient = new HttpClient();
    sendClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {authData.accessToken}");
    
    var sendPayload = new 
    {
        To = new 
        {
            Address = "myTest@test.com",
            SubscriberKey = "myTest@test.com",
            ContactAttributes = new 
            {
                SubscriberAttributes = new 
                {
                    EVENT = "13989",
                    Name = "John Adams"
                }
            }
        }
    };
    var sendRequest = new StringContent(
        JsonSerializer.Serialize(sendPayload),
        Encoding.UTF8,
        "application/json");
    
    var sendResponse = await sendClient.PostAsync($"{authData.instanceUrl}/messaging/v1/messageDefinitionSends/{triggeredSendKey}/send", sendRequest);
    var sendResult = await sendResponse.Content.ReadAsStringAsync();
    
    Console.WriteLine("发送结果: " + sendResult);
}

// 用于解析认证响应的强类型类
public class AuthResponse
{
    public string accessToken { get; set; }
    public string instanceUrl { get; set; }
}

注意事项:

  • 确保SFMC中的API集成已配置TriggeredSend读写权限
  • 子账户场景下,需确认Client ID/Secret对应的集成有权限访问目标子账户
  • 沙箱环境需替换认证端点为https://auth-test.exacttargetapis.com/v1/requestToken

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 15:15:37