java mail socket版本.docx_第1页
java mail socket版本.docx_第2页
java mail socket版本.docx_第3页
java mail socket版本.docx_第4页
java mail socket版本.docx_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论