用sendcloud来发邮件

平时发验证码邮件都是用免费域名邮箱,但是有时一频繁发多了就发不了了,听说用sendcloud可以避免,还能避免阿里云邮件发QQ邮箱进垃圾箱中,去注册了下,免费账户号每个月才50封,自己玩玩可以吧。。

收费的话一个月59,1万封以内的,不知道行不行。。先看看吧。。

下面是封装好的代码,那个api_user和api_key进网页里设置就好了


        /*
* SendMailBySendCloud
* 功能:使用SendCloud发送邮件
* 返回值:string,JSON格式的返回值,或者异常
* 参数:
* from - 显示的发件人邮箱
* to - 收件人邮箱
* title - 邮件标题
* content - 邮件内容
*/
        public static string SendMailBySendCloud(String from, String to, String title, String content, string api_user = "niuna?????", string api_key = "v2?????")
        {
            String url = "http://api.sendcloud.net/apiv2/mail/send";
            HttpClient client = null;
            HttpResponseMessage response = null;
            string result;

            try
            {

                client = new HttpClient();

                List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();

                paramList.Add(new KeyValuePair<string, string>("apiUser", api_user));
                paramList.Add(new KeyValuePair<string, string>("apiKey", api_key));
                paramList.Add(new KeyValuePair<string, string>("from", from));
                paramList.Add(new KeyValuePair<string, string>("fromName", from));
                paramList.Add(new KeyValuePair<string, string>("to", to));
                paramList.Add(new KeyValuePair<string, string>("subject", title));
                paramList.Add(new KeyValuePair<string, string>("html", content));

                response = client.PostAsync(url, new FormUrlEncodedContent(paramList)).Result;
                result = response.Content.ReadAsStringAsync().Result;
                //Console.WriteLine(result);
            }
            catch (Exception e)
            {
                result = e.Message;
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Message :{0} ", e.Message);
            }
            finally
            {
                if (null != client)
                {
                    client.Dispose();
                }
            }

            return result;
        }

sendcloud4j 是 SendCloud 邮件服务的 Java 语言封装包。特点:支持 邮箱API v2 普通发送和模板发送支持批量发送(模板批量变量替换)支持添加附件发送Maven<dependency>     <groupId>io.jstack</groupId>     <artifactId>sendcloud4j</artifactId>     <version>0.0.4</version> <dependency>Gradlecompile 'io.jstack:sendcloud4j:0.0.4'示例代码:初始化 API,通过 SendCloud 后台获取 apiUser 和 apiKey,创建 SendCloud 实例private String apiUser = "testApiUser"; private String apiKey = "testApiKey"; SendCloud webapi = SendCloud.createWebApi(apiUser, apiKey);创建邮件实例,支持普通邮件和模板邮件。普通邮件,邮件内容支持 HTML 或文本:Email email = Email.general()     .from("support@jstack.io")     .fromName("JStack Support")     .html("<b>Hello World!</b>")          // or .plain()     .subject("mail title")     .attachment(new File("att.png"))      // 添加附件 (File or byte[])     .to("denger.it@gmail.com");模块邮件,使用 Substitution.sub() 替换变量值:Email email = Email.template("template_order_customer")     .from("support@jstack.io")     .fromName("JStack Support")     .substitutionVars(Substitution.sub()  // 模板变量替换             .set("product", "iPhone 6S")             .set("name", "denger"))     .attachment(new File("att.png"))      // 添加附件 (File or byte[])     .to("denger.it@gmail.com");执行发送Result result = webapi.mail().send(email);处理发送结果result.isSuccess();      //API 请求是否成功 result.getStatusCode();  //API 返回码 result.getMessage();     //API 返回码的中文解释 标签:sendcloud4j
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值