SMTP协议,返回正确,但为什么收不到邮件呢?
import java.io.*;
import java.net.*;
class sendmail
{
public static void main(String args[])throws Exception
{
String ip;
String sstr;
String from=new String("From: <");
String to=new String("To: ");
byte[] r=new byte[500];
int num;
Socket s=new Socket("smtp.163.com",25);
ip=s.getLocalAddress().toString();
ip=ip.substring(1);
System.out.println("本地IP:"+ip);
DataInputStream in=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
String lin;
lin=in.readLine();
System.out.println(lin);
if(lin.indexOf("220")==-1)
{System.out.println("连接服务器错误");System.exit(0);}
System.out.println("连接成功");//接收连接是返回信息
sstr=new String("EHLO ");
System.out.println("要连接的服务器:");
num=System.in.read(r);
sstr+=new String(r,0,num);
out.write(sstr.getBytes());
out.flush();//发送HELO
lin=in.readLine();
System.out.println("发送:"+sstr+"返回:"+lin);
if(lin.indexOf("250")==-1)
{System.out.println("邮件会话失败");System.exit(0);}
sstr=new String("MAIL FROM:<");
System.out.println("发件人:");
num=System.in.read(r);
from+=new String(r,0,num-2)+">\r\n";
sstr+=new String(r,0,num-2)+">\r\n";
out.write(sstr.getBytes());
out.flush();//发送mail from
lin=in.readLine();
System.out.println("发送:"+sstr+"返回:"+lin);
if(lin.indexOf("250")==-1)
{System.out.println("发件人地址错误");System.exit(0);}
sstr=new String("RCPT TO:");
System.out.println("收件人:");
num=System.in.read(r);
to+=new String(r,0,num);
sstr+=new String(r,0,num-2)+">\r\n";
out.write(sstr.getBytes());
out.flush();//发送rcpt to
lin=in.readLine();
System.out.println("发送:"+sstr+"返回:"+lin);
if(lin.indexOf("250")==-1)
{System.out.println("收件人地址错误");System.exit(0);}
sstr=new String("DATA\r\n");
out.write(sstr.getBytes());
out.flush();
lin=in.readLine();
System.out.println("发送:"+sstr+"返回:"+lin);
if(lin.indexOf("250")==-1)
{System.out.println("邮件内容传输失败");System.exit(0);}
sstr=new String("Subject: ");
System.out.println("主题:");
num=System.in.read(r);
sstr+=new String(r,0,num);
sstr+=from;
sstr+=to;
System.out.println("内容:");
num=System.in.read(r);
sstr+=new String(r,0,num);
sstr+="\r\n.\r\n";
out.write(sstr.getBytes());
out.flush();
lin=in.readLine();
System.out.println("发送邮件:\r\n"+sstr+"返回:"+lin);
if(lin.indexOf("250")==-1)
{System.out.println("发送邮件失败");System.exit(0);}
System.out.println("发送成功");
sstr=new String("QUIT");
out.write(sstr.getBytes());
out.flush();
System.out.println("发送离开信息:"+sstr+"返回:"+lin);
if(lin.indexOf("250")==-1)
{System.out.println("关闭失败");System.exit(0);}
s.close();
}//main
}//class
/*
输入下面的:
smtp.163.com
user1@163.com
user2@163.com
标题 你好
内容 收到了吗?
返回的值是250,表示对了的噻.
但为什么我在WEB邮箱中找不到邮件.
用POP3也找不邮件.
这是为什么呢????
*/
问题点数:80、回复次数:7Top
1 楼butnet(子石长成 http://vsplay.spaces.live.com/)回复于 2006-03-18 10:00:47 得分 0
忍了,没人回答,不管了先自己顶一个.Top
2 楼greenteanet(扎扎实实打基础,保持一颗平常心。)回复于 2006-03-18 11:05:17 得分 40
楼主去下面这个网站看看,看能不能帮你解决问题。
http://www.huihoo.com/java/javamail/javamail_faq.htmlTop
3 楼butnet(子石长成 http://vsplay.spaces.live.com/)回复于 2006-03-21 14:46:01 得分 0
Message-ID: <00a901c64cab$e629b750$d201a8c0@b10>
这个标签是什么意思Top
4 楼apple21(编姑娘的小花篮)回复于 2006-03-21 14:51:40 得分 0
smtp是发邮件的协议 收邮件要用pop协议Top
5 楼apple21(编姑娘的小花篮)回复于 2006-03-21 14:55:54 得分 40
import javax.mail.*;
import javax.swing.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.awt.*;
import java.io.*;
import javax.mail.search.*;
public class POPM
{
public static void main(String[] args)
{
try{ Authenticator auth = new PopupAuthenticator();
Properties props = new Properties();
props.put("mail.store.protocol","pop3");
props.put("mail.host","pop3.163.com");
// props.put("mail.pop3.auth", "true");
Session sess=Session.getInstance(props,null);
Store store = sess.getStore("pop3");
store.connect("pop3.163.com","","");//第2个填你的信箱名 第3个添你的信箱密码
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] msgs=inbox.getMessages();
// Message[] msgs=inbox.search(new FlagTerm(new Flags(Flags.Flag.DELETED),false));
for(int i=0;i<mags.length;i++)
{
System.out.println("msg "+msgs[i].getMessageNumber());
System.out.println("Sent "+msgs[i].getSentDate());
System.out.println("From "+msgs[i].getFrom()[0]);
System.out.println("Subject "+msgs[i].getSubject());
if(msgs[i].getContent() instanceof MimeMultipart)
{
MimeMultipart mp=(MimeMultipart)msgs[i].getContent();
for(int j=0,count=mp.getCount();j<count;j++)
{
Part p=mp.getBodyPart(j);
System.out.println("########################"+"BEGIN MIME"+"################################");
System.out.println(p.getContent());
System.out.println("########################"+"END MIME"+"################################");
}
}
else
{
System.out.println("########################"+"BEGIN content"+"################################");
System.out.println(msgs[i].getContent());
System.out.println("########################"+"END content"+"################################");
}
}
System.out.println("*******************************************************************");
System.out.println("总共"+msgs.length+"封信笺");
inbox.close(false);
store.close();
//inbox.setFlags(msgs,new Flags(Flags.Flag.DELETED),true);
}catch(MessagingException m)
{
m.printStackTrace();
}catch(IOException io)
{
io.printStackTrace();
}
System.exit(0);
}
}
class PopupAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String name,password;
String result = JOptionPane.showInputDialog("Enter name password");
StringTokenizer st = new StringTokenizer(result,",");
name = st.nextToken();
password = st.nextToken();
return new PasswordAuthentication(name,password);
}
}Top
6 楼butnet(子石长成 http://vsplay.spaces.live.com/)回复于 2006-03-24 09:48:03 得分 0
我不用javax.mail,我自己写类似的类!Top
7 楼butnet(子石长成 http://vsplay.spaces.live.com/)回复于 2006-03-24 09:51:35 得分 0
smtp是发邮件的协议 收邮件要用pop协议
这个我当然知道塞,我那个只是发邮件的程序。POP3收邮件的程序我写好了。
是SMTP发邮件的程序出了问题。Top




