.net core借助sendCloud实现邮件验证码发送

来源:互联网 发布:安华卫浴知乎 编辑:程序博客网 时间:2024/05/16 19:15
官方网站:

http://sendcloud.sohu.com/app/

邮件教程

http://sendcloud.sohu.com/doc/email_v2/send_email/#_2


可视化界面


使用邮件模板进行接口调用

URL

http://sendcloud.sohu.com/webapi/mail.send_template.json

HTTP请求方式

post

 

参数说明


请求实例:

#调用模板发送, `%`需要 urlencode curl -d'api_user=***&api_key=***&from=test@test.com&fromname=来自测试发送&subject=测试&template_invoke_name=ifaxin_bill&replyto=reply@test.com&label=16800&resp_email_id=true' --data-urlencode 'substitution_vars={"to":["ben@ifaxin.com","joe@ifaxin.com"],"sub":{"%name%":["Ben", "Joe"],"%money%":[288, 497]}}' http://sendcloud.sohu.com/webapi/mail.send_template.json

利用.net core 实现

引用:
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Net.Http;using System.Security.Cryptography;using System.Text;using System.Threading.Tasks;


public static void Main(string[] args)        {            string substitution_vars = "{\"to\": [\"" + "mengruipan@126.com" + "\"], \"sub\" : { \"%name%\" : [\"" + "猫猫" + "\"],\"%url%\" : [\"" + "http://www.baidu.com" + "\"]}}";            send(substitution_vars);            Console.ReadKey();        }


 public static void send(String substitution_vars)        {            String url = "http://sendcloud.sohu.com/webapi/mail.send_template.json";            HttpClient client = null;            HttpResponseMessage response = null;            try            {                client = new HttpClient();                List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();                paramList.Add(new KeyValuePair<string, string>("api_user", "api_user"));                paramList.Add(new KeyValuePair<string, string>("api_key", "api_key"));                paramList.Add(new KeyValuePair<string, string>("from", "mengruipan@126.com"));                                paramList.Add(new KeyValuePair<string, string>("fromname", "ylb"));                paramList.Add(new KeyValuePair<string, string>("subject", "GRID重置密码"));                paramList.Add(new KeyValuePair<string, string>("template_invoke_name", "girdemail_template_forgot"));                               paramList.Add(new KeyValuePair<string, string>("substitution_vars", substitution_vars));                           var multipartFormDataContent = new MultipartFormDataContent();                foreach (var keyValuePair in paramList)                {                    multipartFormDataContent.Add(new StringContent(keyValuePair.Value), String.Format("\"{0}\"", keyValuePair.Key));                }                Console.WriteLine(multipartFormDataContent);                 response = client.PostAsync(url, multipartFormDataContent).Result;                String result = response.Content.ReadAsStringAsync().Result;                Console.WriteLine("result:{0}", result);            }            catch (Exception e)            {                Console.WriteLine("\nException Caught!");                Console.WriteLine("Message :{0} ", e.Message);            }            finally            {                if (null != client)                {                    client.Dispose();                }            }        }


注意:其中的girdemail_template_forgot是自己实现的,在发送相关--》选择邮件模板---》创建模板


其html代码如下:

<p> </p><p> </p><style type="text/css">html,    body {        margin: 0;        padding: 0;    }</style><center><table><tbody><tr><td><table border="0" cellpadding="0" cellspacing="0" width="600px"><tbody><tr><td><table style="background:url(http://7xi9bi.com1.z0.glb.clouddn.com/35069/2015/07/20/a9179f3bf9714c08b6b8e02c2296aa42.jpg) no-repeat top center;width:100%;font-size:14px;"><tbody><tr><td align="center"><table border="0" cellpadding="0" cellspacing="0" style="background:url(http://7xi9bi.com1.z0.glb.clouddn.com/35069/2015/07/20/e8b87c314d4c4b9d9dbbe63c6c2b0632.jpg) no-repeat center center;margin-top:53px;width:500px;height:449px;box-shadow: 3px 3px 3px #eee"><tbody><tr><td align="center" colspan="2" valign="top"><div style="margin-top:115px;color:#1271be;line-height:1.5;">%name% ,您使用了密码重置功能!<br />请点击下方链接继续下一步。</div><div style="margin-top:60px"><a href="%url%" style="text-decoration:none;display:inline-block;color:#fff;width:144px;height:38px;line-height:38px;background:url(http://7xi9bi.com1.z0.glb.clouddn.com/35069/2015/07/20/2f7e0a9789e642eeb3ccdcfcbf759086.jpg) no-repeat center;">立即激活</a></div></td></tr><tr><td valign="bottom"><table border="0" cellpadding="0" cellspacing="0" style="background:url(http://7xi9bi.com1.z0.glb.clouddn.com/35069/2015/07/20/0f5667bc43d94ae49fed78ad3e412e14.png) #f5f5f5 no-repeat top right" width="214px"><tbody><tr><td align="center" height="130px" valign="middle"><div style="width:148px;color:#fff;font-size:12px;text-align:left;padding-left:10px;line-height:2;">如果以上按钮无法打开,<br />请把右侧的链接复制到浏览<br />器地址栏中打开:</div></td></tr></tbody></table></td><td valign="bottom"><table border="0" cellpadding="0" cellspacing="0" style="background:#f5f5f5" width="287px"><tbody><tr><td align="center" height="130px" valign="middle"><p style="padding: 0 20px;word-wrap:break-word;word-break:break-word;line-height:1.75">%url%</p></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></center>


出现的问题:

substitution_vars:接收的是一个json格式的字符串,实例:{"to": ["ben@ifaxin.com","joe@ifaxin.com"],"sub":{"%name%":["Ben", "Joe"],"%money%":[288, 497]}}

注意点1

.netcore中,json格式的字符串需要这样表示(特殊字符需要转义à这是说的是引号):

 

StringjsonContent = “{\“key1\”:\”value1\”,\”key2\”:\”value2\”}”

 

注意点2:

模板中给出的to的地址列表也是一个[]形式,即数组,所以需要我们对[]里面的引号也要进行转义

 

所以给出一个正确的格式如下:

 

string substitution_vars ="{

\"to\": [\"" + "919389760@qq.com " +"\"],

 \"sub\" : {

\"%name%\" : [\"" + "猫猫"+ "\"],

\"%url%\" : [\"" + "http://www.baidu.com" + "\"]

}

}";


效果:




原创粉丝点击