java开发的邮件发送程序
来源:互联网 发布:知乎实现梦想后 编辑:程序博客网 时间:2024/05/01 19:28
导读:
Java的网络功能非常强大,开发和使用也非常简单,难怪microsoft极力要争回程序语言的霸主地位。笔者根据smtp协议使用java Socket写了一个发送邮件的程序,将此与各位分享。
实现的原理非常简单,首先建立和邮件服务器的Socket连接,然后进行和服务器握手,然后发送smtp指令,并封装邮件体,然后发送即可。大家不妨一试。由什么问题可以和我联系:Email:linlichao@163.com;Homepage:http://www.ehawa.com。
import java.net.*;
import java.io.*;
import java.util.*;
public class SMTPSender{
Socket socket=null;
PrintWriter outData=null;
BufferedReader inData=null;
String smtpServer="";
String user="";
String pass="";
String from="";
String LINEFEED="/r/n";
boolean isNeedAuthLogin=false;
Vector to=new Vector();
public static void main(String[] args){
SMTPSender smtp=new SMTPSender();
smtp.setMailServer("mail.ehawa.com");
smtp.setMailFrom("root@ehawa.com","???","???");
smtp.addMailTo("root@ehawa.com");
if(smtp.send("hello","这是一个测试!")){
System.out.println("邮件发送成功!");
}else System.out.println("邮件发送失败!");
}
public void setMailServer(String s){
smtpServer=s;
}
public void setMailFrom(String s,String uid,String pwd){
this.from=s;
this.user=uid;
this.pass=pwd;
this.isNeedAuthLogin=(this.user!=null&&this.pass!=null&&!this.user.equals("")&&!this.pass.equals(""));
}
public boolean addMailTo(String mailAddr){
to.addElement(mailAddr);
return true;
}
public boolean send(String subject,String content){
try{
if(smtpServer==null||smtpServer.equals(""))return false;
if(from==null||from.equals(""))return false;
if(to.size()<1)return false;
socket=new Socket(smtpServer,25);
outData=new PrintWriter(socket.getOutputStream());
inData=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//与邮件服务器连接成功
readResponse("220");
//HELO host
sendRequest("HELO "+smtpServer+LINEFEED);
readResponse("250");
if(isNeedAuthLogin){
//AUTH LOGIN
sendRequest("AUTH LOGIN"+LINEFEED);
readResponse("334");
//USERNAME:
sendRequest(new String(Base64.encodeString(user))+LINEFEED);
readResponse("334");
//PASSWORD:
sendRequest(new String(Base64.encodeString(pass))+LINEFEED);
readResponse("235");
}
//MAIL FROM:<..>
sendRequest("MAIL FROM:<"+from+">"+LINEFEED);
readResponse("250");
//RCPT TO:<..>
for(Enumeration enu=to.elements();enu.hasMoreElements();){
String to1=(String)enu.nextElement();
sendRequest("RCPT To:<"+to1+">"+LINEFEED);
readResponse("250");
}
//DATA
sendRequest("DATA"+LINEFEED);
readResponse("354");
//邮件内容
StringBuffer s1=new StringBuffer("From: <"+from+">"+LINEFEED);
s1.append("To: <"+to+">"+LINEFEED);
s1.append("Subject: "+subject+LINEFEED);
s1.append("Date: "+new java.util.Date().toLocaleString()+LINEFEED);
s1.append("Content-Type: text/plain;charset=/"GB2312/""+LINEFEED);
s1.append(LINEFEED);
s1.append(content);
s1.append(LINEFEED+"."+LINEFEED);//发送
sendRequest(s1.toString());
readResponse("250");
//QUIT退出
sendRequest("QUIT"+LINEFEED);
readResponse("221");
try{
inData.close();
inData=null;
}catch(Exception ex){}
try{
outData.close();
outData=null;
}catch(Exception ex){}
try{
socket.close();
socket=null;
}catch(Exception ex){}
}catch(Exception e){
return false;
//e.printStackTrace();
}
return true;
}
private void readResponse(String cmd)throws Exception{
String tmp=inData.readLine();
if(tmp.startsWith(cmd));//System.out.println(" [S:]"+tmp);
else throw new Exception("##########邮件发送失败!##########"+tmp);
while(tmp.startsWith(cmd+"-"))tmp=inData.readLine();
}
private void sendRequest(String msg){
//System.out.print("***[C:]"+msg);
outData.write(msg);
outData.flush();
}
public void close(){
try{
inData.close();
inData=null;
}catch(Exception ex){}
try{
outData.close();
outData=null;
}catch(Exception ex){}
try{
socket.close();
socket=null;
}catch(Exception ex){}
}
}
Java的网络功能非常强大,开发和使用也非常简单,难怪microsoft极力要争回程序语言的霸主地位。笔者根据smtp协议使用java Socket写了一个发送邮件的程序,将此与各位分享。实现的原理非常简单,首先建立和邮件服务器的Socket连接,然后进行和服务器握手,然后发送smtp指令,并封装邮件体,然后发送即可。大家不妨一试。由什么问题可以和我联系:Email:linlichao@163.com;Homepage:http://www.ehawa.com。 import java.net.*; import java.io.*; import java.util.*; public class SMTPSender{ Socket socket=null; PrintWriter outData=null; BufferedReader inData=null; String smtpServer=""; String user=""; String pass=""; String from=""; String LINEFEED="/r/n"; boolean isNeedAuthLogin=false; Vector to=new Vector(); public static void main(String[] args){ SMTPSender smtp=new SMTPSender(); smtp.setMailServer("mail.ehawa.com"); smtp.setMailFrom("root@ehawa.com","???","???"); smtp.addMailTo("root@ehawa.com"); if(smtp.send("hello","这是一个测试!")){ System.out.println("邮件发送成功!"); }else System.out.println("邮件发送失败!"); } public void setMailServer(String s){ smtpServer=s;} public void setMailFrom(String s,String uid,String pwd){ this.from=s; this.user=uid; this.pass=pwd; this.isNeedAuthLogin=(this.user!=null&&this.pass!=null&&!this.user.equals("")&&!this.pass.equals(""));} public boolean addMailTo(String mailAddr){ to.addElement(mailAddr); return true;} public boolean send(String subject,String content){ try{ if(smtpServer==null||smtpServer.equals(""))return false; if(from==null||from.equals(""))return false; if(to.size()<1)return false; socket=new Socket(smtpServer,25); outData=new PrintWriter(socket.getOutputStream()); inData=new BufferedReader(new InputStreamReader(socket.getInputStream()));//与邮件服务器连接成功 readResponse("220");//HELO host sendRequest("HELO "+smtpServer+LINEFEED); readResponse("250"); if(isNeedAuthLogin){//AUTH LOGIN sendRequest("AUTH LOGIN"+LINEFEED); readResponse("334");//USERNAME: sendRequest(new String(Base64.encodeString(user))+LINEFEED); readResponse("334");//PASSWORD: sendRequest(new String(Base64.encodeString(pass))+LINEFEED); readResponse("235");}//MAIL FROM:<..> sendRequest("MAIL FROM:<"+from+">"+LINEFEED); readResponse("250");//RCPT TO:<..> for(Enumeration enu=to.elements();enu.hasMoreElements();){ String to1=(String)enu.nextElement(); sendRequest("RCPT To:<"+to1+">"+LINEFEED); readResponse("250");}//DATA sendRequest("DATA"+LINEFEED); readResponse("354");//邮件内容 StringBuffer s1=new StringBuffer("From: <"+from+">"+LINEFEED); s1.append("To: <"+to+">"+LINEFEED); s1.append("Subject: "+subject+LINEFEED); s1.append("Date: "+new java.util.Date().toLocaleString()+LINEFEED); s1.append("Content-Type: text/plain;charset=/"GB2312/""+LINEFEED); s1.append(LINEFEED); s1.append(content); s1.append(LINEFEED+"."+LINEFEED);//发送 sendRequest(s1.toString()); readResponse("250");//QUIT退出 sendRequest("QUIT"+LINEFEED); readResponse("221"); try{ inData.close(); inData=null;}catch(Exception ex){} try{ outData.close(); outData=null;}catch(Exception ex){} try{ socket.close(); socket=null;}catch(Exception ex){}}catch(Exception e){ return false;//e.printStackTrace();} return true;} private void readResponse(String cmd)throws Exception{ String tmp=inData.readLine(); if(tmp.startsWith(cmd));//System.out.println(" [S:]"+tmp); else throw new Exception("##########邮件发送失败!##########"+tmp); while(tmp.startsWith(cmd+"-"))tmp=inData.readLine();} private void sendRequest(String msg){//System.out.print("***[C:]"+msg); outData.write(msg); outData.flush();} public void close(){ try{ inData.close(); inData=null;}catch(Exception ex){} try{ outData.close(); outData=null;}catch(Exception ex){} try{ socket.close(); socket=null;}catch(Exception ex){}}}
本文转自
http://www.cn-java.com/www1/?action-viewnews-itemid-2600
Java的网络功能非常强大,开发和使用也非常简单,难怪microsoft极力要争回程序语言的霸主地位。笔者根据smtp协议使用java Socket写了一个发送邮件的程序,将此与各位分享。
实现的原理非常简单,首先建立和邮件服务器的Socket连接,然后进行和服务器握手,然后发送smtp指令,并封装邮件体,然后发送即可。大家不妨一试。由什么问题可以和我联系:Email:linlichao@163.com;Homepage:http://www.ehawa.com。
import java.net.*;
import java.io.*;
import java.util.*;
public class SMTPSender{
Socket socket=null;
PrintWriter outData=null;
BufferedReader inData=null;
String smtpServer="";
String user="";
String pass="";
String from="";
String LINEFEED="/r/n";
boolean isNeedAuthLogin=false;
Vector to=new Vector();
public static void main(String[] args){
SMTPSender smtp=new SMTPSender();
smtp.setMailServer("mail.ehawa.com");
smtp.setMailFrom("root@ehawa.com","???","???");
smtp.addMailTo("root@ehawa.com");
if(smtp.send("hello","这是一个测试!")){
System.out.println("邮件发送成功!");
}else System.out.println("邮件发送失败!");
}
public void setMailServer(String s){
smtpServer=s;
}
public void setMailFrom(String s,String uid,String pwd){
this.from=s;
this.user=uid;
this.pass=pwd;
this.isNeedAuthLogin=(this.user!=null&&this.pass!=null&&!this.user.equals("")&&!this.pass.equals(""));
}
public boolean addMailTo(String mailAddr){
to.addElement(mailAddr);
return true;
}
public boolean send(String subject,String content){
try{
if(smtpServer==null||smtpServer.equals(""))return false;
if(from==null||from.equals(""))return false;
if(to.size()<1)return false;
socket=new Socket(smtpServer,25);
outData=new PrintWriter(socket.getOutputStream());
inData=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//与邮件服务器连接成功
readResponse("220");
//HELO host
sendRequest("HELO "+smtpServer+LINEFEED);
readResponse("250");
if(isNeedAuthLogin){
//AUTH LOGIN
sendRequest("AUTH LOGIN"+LINEFEED);
readResponse("334");
//USERNAME:
sendRequest(new String(Base64.encodeString(user))+LINEFEED);
readResponse("334");
//PASSWORD:
sendRequest(new String(Base64.encodeString(pass))+LINEFEED);
readResponse("235");
}
//MAIL FROM:<..>
sendRequest("MAIL FROM:<"+from+">"+LINEFEED);
readResponse("250");
//RCPT TO:<..>
for(Enumeration enu=to.elements();enu.hasMoreElements();){
String to1=(String)enu.nextElement();
sendRequest("RCPT To:<"+to1+">"+LINEFEED);
readResponse("250");
}
//DATA
sendRequest("DATA"+LINEFEED);
readResponse("354");
//邮件内容
StringBuffer s1=new StringBuffer("From: <"+from+">"+LINEFEED);
s1.append("To: <"+to+">"+LINEFEED);
s1.append("Subject: "+subject+LINEFEED);
s1.append("Date: "+new java.util.Date().toLocaleString()+LINEFEED);
s1.append("Content-Type: text/plain;charset=/"GB2312/""+LINEFEED);
s1.append(LINEFEED);
s1.append(content);
s1.append(LINEFEED+"."+LINEFEED);//发送
sendRequest(s1.toString());
readResponse("250");
//QUIT退出
sendRequest("QUIT"+LINEFEED);
readResponse("221");
try{
inData.close();
inData=null;
}catch(Exception ex){}
try{
outData.close();
outData=null;
}catch(Exception ex){}
try{
socket.close();
socket=null;
}catch(Exception ex){}
}catch(Exception e){
return false;
//e.printStackTrace();
}
return true;
}
private void readResponse(String cmd)throws Exception{
String tmp=inData.readLine();
if(tmp.startsWith(cmd));//System.out.println(" [S:]"+tmp);
else throw new Exception("##########邮件发送失败!##########"+tmp);
while(tmp.startsWith(cmd+"-"))tmp=inData.readLine();
}
private void sendRequest(String msg){
//System.out.print("***[C:]"+msg);
outData.write(msg);
outData.flush();
}
public void close(){
try{
inData.close();
inData=null;
}catch(Exception ex){}
try{
outData.close();
outData=null;
}catch(Exception ex){}
try{
socket.close();
socket=null;
}catch(Exception ex){}
}
}
Java的网络功能非常强大,开发和使用也非常简单,难怪microsoft极力要争回程序语言的霸主地位。笔者根据smtp协议使用java Socket写了一个发送邮件的程序,将此与各位分享。实现的原理非常简单,首先建立和邮件服务器的Socket连接,然后进行和服务器握手,然后发送smtp指令,并封装邮件体,然后发送即可。大家不妨一试。由什么问题可以和我联系:Email:linlichao@163.com;Homepage:http://www.ehawa.com。 import java.net.*; import java.io.*; import java.util.*; public class SMTPSender{ Socket socket=null; PrintWriter outData=null; BufferedReader inData=null; String smtpServer=""; String user=""; String pass=""; String from=""; String LINEFEED="/r/n"; boolean isNeedAuthLogin=false; Vector to=new Vector(); public static void main(String[] args){ SMTPSender smtp=new SMTPSender(); smtp.setMailServer("mail.ehawa.com"); smtp.setMailFrom("root@ehawa.com","???","???"); smtp.addMailTo("root@ehawa.com"); if(smtp.send("hello","这是一个测试!")){ System.out.println("邮件发送成功!"); }else System.out.println("邮件发送失败!"); } public void setMailServer(String s){ smtpServer=s;} public void setMailFrom(String s,String uid,String pwd){ this.from=s; this.user=uid; this.pass=pwd; this.isNeedAuthLogin=(this.user!=null&&this.pass!=null&&!this.user.equals("")&&!this.pass.equals(""));} public boolean addMailTo(String mailAddr){ to.addElement(mailAddr); return true;} public boolean send(String subject,String content){ try{ if(smtpServer==null||smtpServer.equals(""))return false; if(from==null||from.equals(""))return false; if(to.size()<1)return false; socket=new Socket(smtpServer,25); outData=new PrintWriter(socket.getOutputStream()); inData=new BufferedReader(new InputStreamReader(socket.getInputStream()));//与邮件服务器连接成功 readResponse("220");//HELO host sendRequest("HELO "+smtpServer+LINEFEED); readResponse("250"); if(isNeedAuthLogin){//AUTH LOGIN sendRequest("AUTH LOGIN"+LINEFEED); readResponse("334");//USERNAME: sendRequest(new String(Base64.encodeString(user))+LINEFEED); readResponse("334");//PASSWORD: sendRequest(new String(Base64.encodeString(pass))+LINEFEED); readResponse("235");}//MAIL FROM:<..> sendRequest("MAIL FROM:<"+from+">"+LINEFEED); readResponse("250");//RCPT TO:<..> for(Enumeration enu=to.elements();enu.hasMoreElements();){ String to1=(String)enu.nextElement(); sendRequest("RCPT To:<"+to1+">"+LINEFEED); readResponse("250");}//DATA sendRequest("DATA"+LINEFEED); readResponse("354");//邮件内容 StringBuffer s1=new StringBuffer("From: <"+from+">"+LINEFEED); s1.append("To: <"+to+">"+LINEFEED); s1.append("Subject: "+subject+LINEFEED); s1.append("Date: "+new java.util.Date().toLocaleString()+LINEFEED); s1.append("Content-Type: text/plain;charset=/"GB2312/""+LINEFEED); s1.append(LINEFEED); s1.append(content); s1.append(LINEFEED+"."+LINEFEED);//发送 sendRequest(s1.toString()); readResponse("250");//QUIT退出 sendRequest("QUIT"+LINEFEED); readResponse("221"); try{ inData.close(); inData=null;}catch(Exception ex){} try{ outData.close(); outData=null;}catch(Exception ex){} try{ socket.close(); socket=null;}catch(Exception ex){}}catch(Exception e){ return false;//e.printStackTrace();} return true;} private void readResponse(String cmd)throws Exception{ String tmp=inData.readLine(); if(tmp.startsWith(cmd));//System.out.println(" [S:]"+tmp); else throw new Exception("##########邮件发送失败!##########"+tmp); while(tmp.startsWith(cmd+"-"))tmp=inData.readLine();} private void sendRequest(String msg){//System.out.print("***[C:]"+msg); outData.write(msg); outData.flush();} public void close(){ try{ inData.close(); inData=null;}catch(Exception ex){} try{ outData.close(); outData=null;}catch(Exception ex){} try{ socket.close(); socket=null;}catch(Exception ex){}}}
本文转自
http://www.cn-java.com/www1/?action-viewnews-itemid-2600
- java开发的邮件发送程序
- java开发的邮件发送程序
- JAVA发送邮件的程序
- 邮件开发:发送程序
- 【Java邮件开发】5.编写一个JavaMail发送邮件的程序
- Java发送邮件程序
- java程序发送邮件
- java发送邮件程序
- Java邮件发送程序
- java程序发送邮件
- java发送邮件程序
- Java程序发送邮件
- java程序发送邮件
- java发送邮件程序
- 基于SMTP的JAVA邮件发送程序
- 基于SMTP的JAVA邮件发送程序!
- 自动发送邮件程序开发
- Java程序发送简单邮件
- Java I/O API之性能分析 (下)
- 用Java编写通过代理访问的应用程序
- Java socket编程入门[1]
- Java解析网络数据流的三种特殊方法
- 通过套接字传递对象
- java开发的邮件发送程序
- 一个Socket服务的例子! 请高手指点无法捕捉的输出如何修改?
- ecos 安装
- 用Socket类实现HTTP协议客户端应用
- swt、eclipse RCP与“Java All in One”
- asp.net hyperlink传递两个参数
- C++ 最强大的 .NET Framework编程语言
- 像163网站顶部展出的大幅广告
- 从socket中读取数据时使用超时