protected void Page_Load(object sender, EventArgs e)
{
//根据appId判断获取
if (!AccessTokenContainer.CheckRegistered(appId)) //检查是否已经注册
{
AccessTokenContainer.Register(appId, appSecret); //如果没有注册则进行注册
}
string access_token = AccessTokenContainer.GetAccessTokenResult(appId).access_token; //AccessToken
string openId = "xxxxxx"; //用户openId
string templateId = "xxxxxx"; //模版id
string linkUrl = "http://www.baidu.com"; //点击详情后跳转后的链接地址,为空则不跳转
//为模版中的各属性赋值
var templateData = new ProductTemplateData()
{
first = new TemplateDataItem("您好,您的订单已支付成功!", "#000000"),
product = new TemplateDataItem("旺旺大礼包", "#000000"),
price = new TemplateDataItem("99.8元", "#000000"),
time = new TemplateDataItem("2016-11-09 16:50:38", "#000000"),
remark = new TemplateDataItem("感谢您的光临~", "#000000")
};
SendTemplateMessageResult sendResult = TemplateApi.SendTemplateMessage(access_token, openId, templateId, "", linkUrl, templateData);
//发送成功
if (sendResult.errcode.ToString() == "请求成功")
{
//...
}
else
{
Response.Write("出现错误:" + sendResult.errmsg);
}
}
/// <summary>
/// 定义模版中的字段属性(需与微信模版中的一致)
/// </summary>
public class ProductTemplateData
{
public TemplateDataItem first { get; set; }
public TemplateDataItem product { get; set; }
public TemplateDataItem price { get; set; }
public TemplateDataItem time { get; set; }
public TemplateDataItem remark { get; set; }
}