JavaMail第三方库-sendCloud

java中对字符串进行拼接,然后加上模版的地址,拼接式请求参数。就会完成邮件的发送

package a_mailDemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class Test3 {
    public static void main(String[] args) throws IOException {
        //适用java发送http请求
        //先把请求参数放入map,方便后续的url编码,拼接等操作
        Map<String, String> map = new HashMap<String,String>();
        map.put("apiUser", "lzbhnr_test_CMyzxt");
        map.put("apiKey", "Het0bnSv1fK34RLV");
        map.put("from", "lzbsdust@sina.com");
        map.put("templateInvokeName", "test_template_active");

        StringBuilder params = new StringBuilder();
        Set<Entry<String, String>> entrySet = map.entrySet();
        for(Entry<String,String> entry:entrySet){
            String key = entry.getKey();
            String value= entry.getValue();
            params.append(key).append("=").append(value).append("&");
        }
        //删除最后多余的&
        params.deleteCharAt(params.length()-1);
        URL url = new URL("http://api.sendcloud.net/apiv2/mail/sendtemplate"); //发送模版邮件的
        URLConnection conn = url.openConnection();//打开连接
        conn.setDoOutput(true);//设置输出流为可用。post请求需要用到输出流
        OutputStream output = conn.getOutputStream();

        //把请求参数发送给sendcloud服务器,服务器收到请求以后,会向指定的收件人发送模版邮件
        output.write(params.toString().getBytes());

        //读取响应
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        StringBuilder result = new StringBuilder();
        while((line = reader.readLine())!=null){
            result.append(line);
        }
        System.out.println(result);
        output.close();
        reader.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值