package org.mailsd;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class sendcloud {
static String buffemail;
static String email;
static String st;
static String str;
static String rcpt_to;
static StringBuffer buffer;
public static void EmailRead()throws UnsupportedEncodingException, IOException{
try {
FileInputStream fis=new FileInputStream(new File("L:\\123.txt"));//新建一个FileInputStream对象读取本地文件
BufferedReader bf = new BufferedReader(new InputStreamReader(fis));//把读取到的对象保存到bf缓存中。
try {
str=null;
buffer = new StringBuffer();
while((str=bf.readLine())!=null){
buffer.append(str);
}
String[] st = buffer.toString().split(".com");
List<String> list = null;
for(int i =0;i<st.length;i++){
st[i] = (String)st[i]+".com";
if(st[i].matches("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$")){
String url = "http://sendcloud.sohu.com/webapi/mail.send.json";
final String apiUser = "ypjr2014";
final String apiKey = "Q6bYEwdWDV8q0jAd";
rcpt_to=st[i];
HttpPost httpPost = new HttpPost(url);
HttpClient httpclient = new DefaultHttpClient();
// 组装基本邮件的参数
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));
entity.addPart("api_user", new StringBody(apiUser, Charset.forName("UTF-8"))); //API_USER
entity.addPart("api_key", new StringBody(apiKey, Charset.forName("UTF-8"))); //apiKey
entity.addPart("to", new StringBody(rcpt_to, Charset.forName("UTF-8"))); //收件人地址
entity.addPart("from", new StringBody("sendcloud@sendcloud.org", Charset.forName("UTF-8"))); //发件人地址. from 和发信域名, 会影响是否显示代发
entity.addPart("fromname", new StringBody("源璞金融", Charset.forName("UTF-8"))); //收件人姓名
entity.addPart("subject", new StringBody("源璞金融", Charset.forName("UTF-8")));//标题. 不能为空
entity.addPart("html",
new StringBody("<html><head></head><body><p>源璞公司,欢迎您的投资:</p>" +
"<p><a href='http://www.beckmoney.com'><img src='http://7u2pwr.com1.z0.glb.clouddn.com/股市.jpg' style='width: 1000px; height: 879px;'>"+
"</a></p>"+"<p> 感谢你们一直对贝壳的支持和信任</p>"+
"<p> 股市投资有风险,稳健收益到贝壳,守住来之不易的牛市成果,贝壳来帮你忙,现推出年化收益9%-12%的理财产品,收益稳定,详情点击www.beckmoney.com贝壳理财网是由上海源璞金融信息服务有限公司打造,推出全新的P2N(个人对机构)理财模式。P2N模式选择与优秀的小贷公司、担保公司、典当公司等类金融机构合作,精选优质借款项目,通过严格风控审核后再推荐给出借人,以提高投资项目的可靠性。<br />"+
" 贝壳理财的P2N模式对投资者还有一重保障,即出借人完成出借后,资金将通过第三方支付——汇付天下,从出借人账户划转至借款人账户,借款人按合同约定按期付息,到期还款。环环相扣的制度设计,能很大程度上杜绝一些不良情形的发生。</p></body></html>", Charset.forName("UTF-8")));
entity.addPart("resp_email_id", new StringBody("true")); //是否返回 emailId. 有多个收件人时, 会返回 emailId 的列表
// 添加附件
// File file = new File("");
// FileBody attachment = new FileBody(file, "application/octet-stream", "UTF-8");
// entity.addPart("files", attachment);
// 添加附件, 文件流形式
// File file = new File("/path/file");
// String attachName = "attach.txt";
// InputStreamBody is = new InputStreamBody(new FileInputStream(file), attachName);
// entity.addPart("files", is);
httpPost.setEntity(entity);
HttpResponse response = httpclient.execute(httpPost);
// 处理响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 正常返回, 解析返回数据
System.out.println(EntityUtils.toString(response.getEntity()));
} else {
System.err.println("error");
}
httpPost.releaseConnection();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws ClientProtocolException, IOException {
EmailRead();
}
}