国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java開發的郵件發送程序

2019-11-18 11:03:58
字體:
來源:轉載
供稿:網友

 


java的網絡功能非常強大,開發和使用也非常簡單,難怪microsoft極力要爭回程序語言的霸主地位。根據smtp協議使用java Socket寫了一個發送郵件的程序,
實現的原理非常簡單,首先建立和郵件服務器的Socket連接,然后進行和服務器握手,然后發送smtp指令,并封裝郵件體,然后發送即可。

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==nullsmtpServer.equals(""))return false;
if(from==nullfrom.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){}
}
}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 易门县| 海口市| 名山县| 安吉县| 固始县| 新竹县| 子长县| 新宁县| 兴和县| 左云县| 巴东县| 秭归县| 齐齐哈尔市| 周口市| 博野县| 永靖县| 青神县| 庆元县| 湟中县| 射洪县| 上饶市| 类乌齐县| 房产| 明水县| 临桂县| 望江县| 利辛县| 西乌珠穆沁旗| 司法| 阿合奇县| 正蓝旗| 彭山县| 宁国市| 昭苏县| 华容县| 金川县| 金川县| 德保县| 宝应县| 临安市| 莫力|