分享
 
 
 

构建WAP邮件发送服务器

王朝other·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

WAP(无限通讯协议)是在数字移动电话,PDA和计算机之间进行通讯的一种标准。因此构建WAP邮件发送服务器将具有很广泛的应用前景和很高的商业价值。下面就是一个使用Java编写的Servlet程序,通过一些简单的设置,你就可以使用它从WAP手机或任何支持WML的PDA上发送Email了。

用法是:

http://your_host/servlet/WapMailer?template_file

解释:

Template文件是一个描述邮件设置和模板的文本文件。你可以在这个文件中设置SMTP服务器的域名和端口,以及信件的相关内容的参数。根据Template文件设置的信息,WAP服务器将动态生成相应

的WML页面。

在描述你的信件的时候,也可以使用变量: $from,$to, $subject,$body.这些变量会被提交的表单的相应内容代替。当你在WML中提交表单时,邮件也将动态生成。你也可以粘贴一些文件,比如公司的价目表、你的履历表等等。

Template文件可以采用非常自由的格式,但是每一个参数的描述都必须另起一行。

现把参数列在下面:

Mailhost:SMTP服务器名,必须填写。

PORT:SMTP服务器端口。

Domain: 域名,必须填写。

From: 发信人地址。

To: 收信人地址。

Subject: 邮件主题。

Body: 邮件内容。

Attach: 设置粘贴的文件列表。

Encoding: 字符集编码,默认值为ISO-8859-1。

例如:最简单的设置文件:

Mailhost:smtp.abcdef.com

Domain:abcdef.com

Port:25

From:$from

To:$to

Subject:$subject

Body:$body

又如:对于某些短消息服务,可以描述To为To:$to@your_service_host ,并且在邮件表单收信人地址中仅写对方的手机号码。你还可以给任何域设置常量值,如:To:support@abcdef.com,在生成的WML表单中这个域就将被省略。

在描述body参数时,也可以使用现成的文本文件,方法是在body中加入: $file=path_to_some_text_file ,该文件的内容将被加进信中。当你设置好粘贴文件后,你就可以使用文件列表了。文件名应该用逗号分隔开。

需注意:由于手机及PDA中的WAP是全球应用的,因此标准字符集编码采用ISO-8859-1,而我们平时使用汉字是用GB2312编码,不经过内码转化将会出现乱码,造成手机用户无法阅读,因此需要把编码设置成GB2312。

||||||源程序:WapMailer.java,将其编译成".class"文件

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class WapMailer extends HttpServlet

{

private Hashtable getTemplate(String s)

{

Hashtable hashtable = (Hashtable)cfgs.get(s);

if(hashtable == null)

{

hashtable = new Hashtable();

readTemplateFile(s, hashtable);

cfgs.put(s, hashtable);

}

else

{

File file = new File(s);

String s1 = (String)hashtable.get("edt");

String s2 = String.valueOf(file.lastModified());

if(!s1.equals(s2))

{

hashtable = new Hashtable();

readTemplateFile(s, hashtable);

cfgs.remove(s);

cfgs.put(s, hashtable);

}

}

return hashtable;

}

public void init(ServletConfig servletconfig)

throws ServletException

{

super.init(servletconfig);

NEWLINE = System.getProperty("line.separator");

cfgs = new Hashtable();

System.out.println("WAP Mailer");

}

||||||public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)

throws ServletException, IOException

{

boolean flag = true;

boolean flag1 = true;

boolean flag2 = true;

boolean flag3 = true;

String s5 = HttpUtils.getRequestURL(httpservletrequest).toString();

int i;

if((i = s5.indexOf("?")) > 0)

s5 = s5.substring(0, i);

String s = httpservletrequest.getQueryString();

httpservletresponse.setContentType("text/vnd.wap.wml");

PrintWriter printwriter = httpservletresponse.getWriter();

printwriter.println("");

if(s == null)

{

printwriter.println("");

printwriter.println("");

printwriter.println("

");

printwriter.println("WapMailer: Can't read template file");

printwriter.println("

");

printwriter.println("");

printwriter.println("");

printwriter.flush();

printwriter.close();

return;

}

Hashtable hashtable = getTemplate(s);

if(hashtable.size() == 0)

{

printwriter.println("");

printwriter.println("");

printwriter.println("

");

printwriter.println("WapMailer: Can't read template file");

printwriter.println("

");

printwriter.println("");

printwriter.println("");

printwriter.flush();

printwriter.close();

return;

}

flag = usedParameter("$from", hashtable);

flag1 = usedParameter("$to", hashtable);

flag2 = usedParameter("$subject", hashtable);

flag3 = usedParameter("$body", hashtable);

String s1;

if((s1 = (String)hashtable.get("FROM")) == null)

s1 = "";

else

if(s1.indexOf("$") >= 0)

s1 = "";

String s2;

if((s2 = (String)hashtable.get("TO")) == null)

s2 = "";

else

if(s2.indexOf("$") >= 0)

s2 = "";

String s3;

if((s3 = (String)hashtable.get("SUBJECT")) == null)

s3 = "";

else

if(s3.indexOf("$") >= 0)

s3 = "";

String s4;

if((s4 = (String)hashtable.get("BODY")) == null)

s4 = "";

else

if(s4.indexOf("$") >= 0)

s4 = "";

printwriter.println("");

printwriter.println("");

printwriter.println("");

printwriter.println("");

if(flag)

printwriter.println("");

else

printwriter.println("");

if(flag1)

printwriter.println("");

else

printwriter.println("");

if(flag2)

printwriter.println("");

else

printwriter.println("");

if(flag3)

printwriter.println("");

else

printwriter.println("");

printwriter.println("");

printwriter.println("");

if(flag)

printwriter.println("

From:

");

if(flag1)

printwriter.println("

To:

emptyok=\"false\"/>

");

if(flag2)

printwriter.println("

Subject:

"\" emptyok=\"true\"/>

");

if(flag3)

printwriter.println("

Message:

"\" emptyok=\"false\"/>

");

printwriter.println("");

printwriter.println("");

printwriter.flush();

printwriter.close();

}

||||||public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse

httpservletresponse)

throws ServletException, IOException

{

PrintWriter printwriter = null;

Hashtable hashtable = null;

Hashtable hashtable1 = null;

String s = httpservletrequest.getQueryString();

httpservletresponse.setContentType("text/vnd.wap.wml");

printwriter = httpservletresponse.getWriter();

printwriter.println("");

if(s == null)

{

printwriter.println("");

printwriter.println("");

printwriter.println("

");

printwriter.println("WapMailer: Can't read template file");

printwriter.println("

");

printwriter.println("");

printwriter.println("");

printwriter.flush();

printwriter.close();

return;

}

hashtable1 = getTemplate(s);

if(hashtable1.size() == 0)

{

printwriter.println("");

printwriter.println("");

printwriter.println("

");

printwriter.println("WapMailer: Can't read template file");

printwriter.println("

");

printwriter.println("");

printwriter.println("");

printwriter.flush();

printwriter.close();

return;

}

if(hashtable1.get("PORT") == null || hashtable1.get("FROM") == null ||

hashtable1.get("TO") == null || hashtable1.get("MAILHOST") == null ||

hashtable1.get("DOMAIN") == null)

{

printwriter.println("");

printwriter.println("");

printwriter.println("

");

printwriter.println("WapMailer: Can't send mail. Check template file");

printwriter.println("

");

printwriter.println("");

printwriter.println("");

printwriter.flush();

printwriter.close();

return;

}

hashtable = new Hashtable();

String s1;

for(Enumeration enumeration = httpservletrequest.getParameterNames();

enumeration.hasMoreElements(); hashtable.put(s1.toUpperCase(),

decodeString(httpservletrequest.getParameter(s1),

httpservletrequest.getCharacterEncoding(), (String)hashtable1.get("ENCODING"))))

s1 = (String)enumeration.nextElement();

boolean flag = letterToNet(hashtable, hashtable1);

printwriter.println("");

printwriter.println("");

printwriter.println("

");

if(flag)

printwriter.println("WapMailer: your message has been send");

else

printwriter.println("WapMailer: Can't send mail");

printwriter.println("

");

printwriter.println("");

printwriter.println("");

printwriter.flush();

printwriter.close();

}

||||||public String getServletInfo()

{

return "WapMailer mailto: waynezheng@chinaren.com";

}

private boolean letterToNet(Hashtable hashtable, Hashtable hashtable1)

{

smtpMail smtpmail = new smtpMail();

String s1 = prepareString((String)hashtable1.get("MAILHOST"), hashtable);

String s2 = prepareString((String)hashtable1.get("DOMAIN"), hashtable);

int i;

try

{

i = Integer.parseInt(prepareString((String)hashtable1.get("PORT"),

hashtable));

}

catch(Exception _ex)

{

return false;

}

if(s1 == null)

return false;

String s;

String s3;

if((s = (String)hashtable1.get("FROM")) == null)

s3 = (String)hashtable.get("FROM");

else

if(s.indexOf("$") < 0)

s3 = (String)hashtable.get("FROM");

else

s3 = prepareString(s, hashtable);

String s4;

if((s = (String)hashtable1.get("TO")) == null)

s4 = (String)hashtable.get("TO");

else

if(s.indexOf("$") < 0)

s4 = (String)hashtable.get("TO");

else

s4 = prepareString(s, hashtable);

if(smtpmail.open(s1, i) != 1)

return false;

smtpmail.setDomain(s2);

smtpmail.setFrom(s3);

smtpmail.setTo(s4);

if((s = (String)hashtable1.get("SUBJECT")) == null)

s = (String)hashtable.get("SUBJECT");

else

if(s.indexOf("$") < 0)

s = (String)hashtable.get("SUBJECT");

else

s = prepareString(s, hashtable);

smtpmail.addHeader("Subject", s);

if((s = (String)hashtable1.get("CC")) == null)

s = "";

else

s = prepareString(s, hashtable);

if(s.length() > 0)

smtpmail.addHeader("Cc", s);

if((s = (String)hashtable1.get("BODY")) == null)

s = (String)hashtable.get("BODY");

else

if(s.indexOf("$") < 0)

s = (String)hashtable.get("BODY");

else

s = prepareString(insertFiles(s), hashtable);

smtpmail.addData(s);

if((s = (String)hashtable1.get("ATTACH")) != null)

{

s = prepareString(s, hashtable);

for(StringTokenizer stringtokenizer = new StringTokenizer(s, " ,");

stringtokenizer.hasMoreTokens(); smtpmail.addAttachment(s))

s = stringtokenizer.nextToken();

}

if(smtpmail.transmit() != 1)

{

return false;

}

else

{

smtpmail.close();

return true;

}

}

private String insertFiles(String s)

{

String s1 = s.toUpperCase();

int i;

if((i = s1.indexOf("$FILE=")) < 0)

return s;

String s2;

if(i == 0)

s2 = "";

else

s2 = s.substring(0, i) + NEWLINE;

s1 = s.substring(i + "$FILE=".length());

i = s1.indexOf(NEWLINE);

String s3;

if(i < 0)

{

s3 = "";

}

else

{

s3 = s1.substring(i + NEWLINE.length());

s1 = s1.substring(0, i);

}

if(s1.startsWith("\""))

s1 = s1.substring(1);

if(s1.endsWith("\""))

s1 = s1.substring(0, s1.length() - 1);

try

{

BufferedReader bufferedreader = new BufferedReader(new

InputStreamReader(new FileInputStream(s1)));

while((s1 = bufferedreader.readLine()) != null)

s2 = s2 + s1 + NEWLINE;

bufferedreader.close();

}

catch(Exception _ex)

{

return s;

}

return s2 + insertFiles(s3);

}

private String prepareString(String s, Hashtable hashtable)

{

int i = s.indexOf("$");

String s1 = "";

if(i < 0 || i == s.length() - 1)

return s;

for(StringTokenizer stringtokenizer = new StringTokenizer(s, "

,\t;:!@#%^~&*()-+_=|\\/?<>" + NEWLINE, true); stringtokenizer.hasMoreTokens();)

{

String s2 = stringtokenizer.nextToken();

String s3;

if(s2.startsWith("$"))

{

if(s2.length() == 1)

s1 = s1 + s2;

else

if((s3 = (String)hashtable.get(s2.substring(1).toUpperCase())) != null)

s1 = s1 + s3;

else

s1 = s1 + s2;

}

else

{

s1 = s1 + s2;

}

}

return s1;

}

||||||private boolean usedParameter(String s, Hashtable hashtable)

{

for(Enumeration enumeration = hashtable.keys(); enumeration.hasMoreElements();)

{

String s1 = (String)hashtable.get((String)enumeration.nextElement());

if(s1.indexOf(s) >= 0)

return true;

}

return false;

}

private void readTemplateFile(String s, Hashtable hashtable)

{

String s4 = "";

boolean flag = false;

try

{

BufferedReader bufferedreader = new BufferedReader(new

InputStreamReader(new FileInputStream(s)));

String s1;

while((s1 = bufferedreader.readLine()) != null)

{

s1 = s1.trim();

if(s1.length() > 0)

{

String s3 = s1.toUpperCase();

if(s3.startsWith("PORT:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("PORT");

hashtable.put("PORT", s1.substring("PORT:".length()).trim());

}

else

if(s3.startsWith("DOMAIN:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("DOMAIN");

hashtable.put("DOMAIN",

s1.substring("DOMAIN:".length()).trim());

}

else

if(s3.startsWith("TO:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("TO");

hashtable.put("TO", s1.substring("TO:".length()).trim());

}

else

if(s3.startsWith("CC:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("CC");

hashtable.put("CC", s1.substring("CC:".length()).trim());

}

else

if(s3.startsWith("SUBJECT:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("SUBJECT");

hashtable.put("SUBJECT",

s1.substring("SUBJECT:".length()).trim());

}

else

if(s3.startsWith("MAILHOST:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("MAILHOST");

hashtable.put("MAILHOST",

s1.substring("MAILHOST:".length()).trim());

}

else

if(s3.startsWith("FROM:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("FROM");

hashtable.put("FROM", s1.substring("FROM:".length()).trim());

}

else

if(s3.startsWith("REPLY:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("REPLY");

hashtable.put("REPLY", s1.substring("REPLY:".length()).trim());

}

else

if(s3.startsWith("ATTACH:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("ATTACH");

hashtable.put("ATTACH",

s1.substring("ATTACH:".length()).trim());

}

else

if(s3.startsWith("ENCODING:"))

{

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

flag = false;

s4 = "";

hashtable.remove("ENCODING");

hashtable.put("ENCODING",

s1.substring("ENCODING:".length()).trim());

}

else

if(s3.startsWith("BODY:"))

{

hashtable.remove("BODY");

flag = true;

s4 = s1.substring("BODY:".length()).trim();

}

else

if(flag)

s4 = s4 + NEWLINE + s1;

}

else

if(flag)

s4 = s4 + NEWLINE;

}

bufferedreader.close();

File file = new File(s);

hashtable.put("edt", String.valueOf(file.lastModified()));

}

catch(Exception _ex) { }

String s2;

if((s2 = (String)hashtable.get("PORT")) != null)

try

{

int i = Integer.parseInt(s2);

if(i <= 0)

hashtable.remove("PORT");

}

catch(Exception _ex)

{

hashtable.remove("PORT");

}

if((s2 = (String)hashtable.get("DOMAIN")) != null && s2.length() == 0)

hashtable.remove("DOMAIN");

if((s2 = (String)hashtable.get("REPLY")) != null && s2.length() == 0)

hashtable.remove("REPLY");

if((s2 = (String)hashtable.get("FROM")) != null && s2.length() == 0)

hashtable.remove("FROM");

if((s2 = (String)hashtable.get("TO")) != null && s2.length() == 0)

hashtable.remove("TO");

if((s2 = (String)hashtable.get("CC")) != null && s2.length() == 0)

hashtable.remove("CC");

if(hashtable.get("PORT") == null)

hashtable.put("PORT", "25");

if(hashtable.get("DOMAIN") == null)

hashtable.put("DOMAIN", "localhost");

if(hashtable.get("ENCODING") == null)

hashtable.put("ENCODING", "ISO-8859-1");

if(flag)

{

hashtable.remove("BODY");

hashtable.put("BODY", s4);

}

}

private String decodeString(String s, String s1, String s2)

{

String s3;

try

{

s3 = new String(s.getBytes(s1 != null ? s1 : "ISO-8859-1"), s2);

}

catch(Exception _ex)

{

s3 = s;

}

return s3;

}

public WapMailer()

{

}

private static final String MAILHOST = "MAILHOST";

private static final String PORT = "PORT";

private static final String DOMAIN = "DOMAIN";

private static final String FROM = "FROM";

private static final String TO = "TO";

private static final String CC = "CC";

private static final String SUBJECT = "SUBJECT";

private static final String BODY = "BODY";

private static final String ATTACH = "ATTACH";

private static final String REPLY = "REPLY";

private static final String FILE = "FILE";

private static final String EDITED = "edt";

private static final String ENCODING = "ENCODING";

private static final String DEFDOMAIN = "localhost";

private static final String DEFENCODING = "ISO-8859-1";

private static final String DEFPORT = "25";

private static String NEWLINE = "\n";

private static Hashtable cfgs;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有