SMTP协议,返回正确,但为什么收不到邮件呢?

butnet 2006-03-18 09:50:14
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也找不邮件.
这是为什么呢????
*/
...全文
379 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
butnet 2006-03-24
  • 打赏
  • 举报
回复
smtp是发邮件的协议 收邮件要用pop协议
这个我当然知道塞,我那个只是发邮件的程序。POP3收邮件的程序我写好了。
是SMTP发邮件的程序出了问题。
butnet 2006-03-24
  • 打赏
  • 举报
回复
我不用javax.mail,我自己写类似的类!
apple21 2006-03-21
  • 打赏
  • 举报
回复
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);
}
}
apple21 2006-03-21
  • 打赏
  • 举报
回复
smtp是发邮件的协议 收邮件要用pop协议
butnet 2006-03-21
  • 打赏
  • 举报
回复
Message-ID: <00a901c64cab$e629b750$d201a8c0@b10>
这个标签是什么意思
greenteanet 2006-03-18
  • 打赏
  • 举报
回复
楼主去下面这个网站看看,看能不能帮你解决问题。
http://www.huihoo.com/java/javamail/javamail_faq.html
butnet 2006-03-18
  • 打赏
  • 举报
回复
忍了,没人回答,不管了先自己顶一个.

62,626

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧