發送郵件

package com.sinosoft.webservice;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import sun.misc.BASE64Encoder;
  

public class SendMail {
	
	public static void main(String[] args) throws RemoteException,
	     ServiceException, MalformedURLException {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<request>");
sb.append("<username>unitemessage</username>");
sb.append("<password>24BEACE863ECEBB494AAA19F016F2772</password>");
sb.append("<taskid>002jiekouceshi0001</taskid>");
sb.append("<to>[email protected]</to>");
//sb.append("<cc></cc>");
sb.append("<subject>無主題</subject>");
sb.append("<labels>");
sb.append("<label>");
sb.append("<labelName>測試標籤</labelName>");
sb.append("<labelValue>就簡單的測試下能不能通</labelValue>");
sb.append("</label>");
sb.append("<label>");
sb.append("<labelName>卡號</labelName>");
sb.append("<labelValue>87654321</labelValue>");
sb.append("</label>");
sb.append("<label>");
sb.append("<labelName>日期</labelName>");
sb.append("<labelValue>2013/04/26</labelValue>");
sb.append("</label>");
sb.append("</labels>");
sb.append("<affixs>");
sb.append(loadAffix("test.doc", "D:\\test.doc"));
sb.append(loadAffix("test1.doc", "D:\\test.doc"));
sb.append("</affixs>");
sb.append("</request>");

Service service = new Service();
Call call = (Call) service.createCall();
//服務器地址
String url = "http://*******";
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(url, "sendmail"));
call.addParameter("xml", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnClass(String.class);
String result = (String) call.invoke(new Object[] { sb.toString() });
System.out.println(sb.toString());
System.out.println("返回結果:"+result);
}

static public String loadAffix(String showName, String affixPath) {
StringBuilder sb = new StringBuilder();
sb.append("<affix>");
sb.append("<affixName>").append(showName).append("</affixName>");
byte[] tmp = null;
FileInputStream in = null;
File file = new File(affixPath);
long filesize = 0;
if (file.exists()) {
	filesize = file.length();
}
try {
	in = new FileInputStream(affixPath);
	ByteArrayOutputStream buf = new ByteArrayOutputStream();
	tmp = new byte[Integer.parseInt(String.valueOf(filesize))];
	int len = -1;
	while ((len = in.read(tmp)) >= 0) {
		buf.write(tmp, 0, len);
	}
	buf.flush();

	tmp = buf.toByteArray();
} catch (Exception e1) {
	e1.printStackTrace();
	return "";
}
sb.append("<affixSize>").append(filesize).append("</affixSize>");
sb.append("<affixContent>").append(new BASE64Encoder().encode(tmp)).append("</affixContent>");
sb.append("</affix>");
return sb.toString();
}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章