已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
使用 java 发送邮件本身是可以使用 已经存在 的 第三方的jar包,javaMail的,但是这里使用的java 的socket 版本,本质就是利用 smtp 的协议。因为 socket 就是相当于 telnet。当你使用了 telnet 的命令连上了smtp 的服务器之后,接下来就是完全的,smtp 的 命令。所以socket也是一样的,首先要连接 smtp 的服务器,然后使用 io 将命令写进去。代码如下:客户端的 代码public class Test public static void main(String args) JMail mail=new JMail(); mail.setServerName(smtp. com); mail.setDate(new Date().toString(); mail.setAccount(mengdejun_520); mail.setFrom_address(mengdejun_520 com); mail.setTo_address(280759843); mail.setPassword(*); mail.setFrom_name(mengdejun_520 com); mail.setPort(25); mail.setSubject(test); mail.setTo_name(mengdejun_168 com); mail.addBody(你好吗?java编程!+new Date(); try mail.send(); mail.destory(); catch (Exception e) e.printStackTrace(); Socket实现的代码public class JMail implements Send private Socket client = null; private String from_address = null, account = null, password = null,to_address; private StringBuffer body = new StringBuffer(); private PrintWriter out = null; private BufferedReader in = null; private String date = new Date().toLocaleString(); private String from_name = null, to_name = null, subject = null, content_Type = Content-Type: text/plain;charset=GB2312; private boolean isAuthLogin = true; private String serverName = null; private int port = 25; public boolean destory() throws Exception client.close(); in.close(); out.close(); response(Done); return true; public JMail(String from_address, String account, String password, String serverName) super(); this.from_address = from_address; this.account = account; this.password = password; this.serverName = serverName; this.from_name = this.from_address; public JMail() public int getPort() return port; public void setPort(int port) this.port = port; public String getServerName() return serverName; public void setServerName(String serverName) this.serverName = serverName; public String getFrom_address() return from_address; public void setFrom_address(String from_address) this.from_address = from_address; public String getTo_address() return to_address; public void setTo_address(String to_address) this.to_address = to_address; public String getAccount() return account; public void setAccount(String account) this.account = account; public String getPassword() return password; public void setPassword(String password) this.password = password; public void addBody(String content) if (isAuthLogin) body.append(From: rn); body.append(To: rn); body.append(Subject: + this.subject + rn); body.append(this.content_Type + rn); body.append(rn); body.append(content + rn); body.append(.rn); public String getDate() return date; public void setDate(String date) this.date = date; public String getFrom_name() return from_name; public void setFrom_name(String from_name) this.from_name = from_name; public String getTo_name() return to_name; public void setTo_name(String to_name) this.to_name = to_name; public String getSubject() return subject; public void setSubject(String subject) this.subject = subject; public String getContent_Type() return content_Type; public void setContent_Type(String content_Type) this.content_Type = content_Type; public boolean isAuthLogin() return (isAuthLogin != (this.account = null & this.password = null & this.from_address = null & this.to_address=null & this.serverName = null & this.body.length() 1); public void send() throws Exception if (parse() out.println(QUIT); private boolean parse() boolean isReady = false; if (this.account = null & this.password = null & this.from_address = null & this.to_address =null & this.serverName = null & this.body.length() 1) isAuthLogin = false; isReady = false; return isReady; try client = new Socket(this.serverName, this.port); out = new PrintWriter(client.getOutputStream(); in = new BufferedReader(new InputStreamReader(client .getInputStream(); response(in.readLine(); out.println(ehlo MDJ); out.flush(); response(in.readLine(); out.println(AUTH LOGIN); out.flush(); response(in.readLine(); response(in.readLine(); response(in.readLine(); response(in.readLine(); out.println(Encode(this.account); out.flush(); response(in.readLine(); out.println(Encode(this.password); out.flush(); response(in.readLine(); out.println(mail from: ); out.flush(); response(in.readLine(); out.println(rcpt to: ); out.flush(); response(in.readLine(); out.println(data); out.flush(); response(in.readLine(); /* * out.print(from: n); * out.print(to: ); * out.print(Subject: n); out.print(nn); */ out.println(body.toString(); out.flush(); response(in.readLine(); response(in.readLine(); / out.println(.); isReady = true; catch (UnknownHostException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); return isReady; publi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 普京和中国签署协议书
- 2025年RISC-VI2C接口设计考核试卷
- 播放连言协议书
- 宜宾比熊领养协议书
- 材料款回款协议书
- 2025年茶艺师语言表达与文化传播能力茶艺师操作技能考核试卷
- 2025年疫苗接种规范操作考试:冷链技术创新考核试卷
- 2025年共享经济行业共享经济模式与共享平台发展研究报告及未来发展趋势预测
- 2025年环保科技创新与产业转型研究报告及未来发展趋势预测
- 2025年半导体行业芯片技术与集成电路发展研究报告及未来发展趋势预测
- 【MOOC答案】《光纤光学》(华中科技大学)章节作业期末慕课答案
- 大学化学元素周期性试题及答案
- 2025-2030年中国铜精粉产业营运走势与投资前景展望研究报告
- 江苏扬州大数据集团子公司招聘笔试题库2025
- DB37-T5321-2025 居住建筑装配式内装修技术标准
- 深圳协议二手车合同模板
- 酒厂车间员工管理制度
- DB21T 3722.7-2025高标准农田建设指南 第7部分:高标准农田工程施工质量评定规范
- 2025年工地监护员考试题及答案
- 跨国企业IT架构创新-深度研究
- 2025中国远洋海运集团校园招聘1484人笔试参考题库附带答案详解
评论
0/150
提交评论