| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 710 人关注过本帖
标题:求可以收发电子邮件的一段程序
只看楼主 加入收藏
鼻涕虫
Rank: 1
等 级:新手上路
威 望:1
帖 子:36
专家分:0
注 册:2008-5-4
收藏
 问题点数:0 回复次数:3 
求可以收发电子邮件的一段程序
小弟初学JAVA,做一个小小小的课程系统,但要实现收发邮件的功能,希望哪为大侠举手相助啊。
搜索更多相关主题的帖子: 电子邮件 程序 收发 JAVA 课程系统 
2008-05-27 21:45
witchery
Rank: 1
来 自:西安
等 级:新手上路
帖 子:205
专家分:0
注 册:2005-8-6
收藏
得分:0 
JavaMail
2008-05-28 19:21
鼻涕虫
Rank: 1
等 级:新手上路
威 望:1
帖 子:36
专家分:0
注 册:2008-5-4
收藏
得分:0 
回复 2# witchery 的帖子
这个和用javaMail实现有什么区别吗
import *;
import *;
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.);
smtp.setMailFrom("root@,"???","???");
smtp.addMailTo("root@);
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){}
}
}
2008-05-28 22:50
鼻涕虫
Rank: 1
等 级:新手上路
威 望:1
帖 子:36
专家分:0
注 册:2008-5-4
收藏
得分:0 
回复 2# witchery 的帖子
还有我看过貌似JAVAMAIL这个接口并没有包含在JDK中,
2008-05-29 16:20
快速回复:求可以收发电子邮件的一段程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012447 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved