电子邮件那点事儿

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  org.hvp.test;

import  java.util.Hashtable;

import  javax.naming.NamingException;
import  javax.naming.directory.Attributes;
import  javax.naming.directory.DirContext;
import  javax.naming.directory.InitialDirContext;

/**
 * 
@author  "惠万鹏"
 *
 
*/
public   class  Smtp
{
    
public   static  String getSmtpServer(String dns, String domain)
            
throws  NamingException
    {
        
/**  如果domain传入的是一个email  */
        
if  (domain.indexOf( " @ " >   0 )
        {
            domain 
=  domain.substring(domain.indexOf( " @ " +   1 );
        }
        Hashtable
< String, String >  properties  =   new  Hashtable < String, String > ();
        properties.put(
" java.naming.factory.initial " , " com.sun.jndi.dns.DnsContextFactory " );
        properties.put(
" java.naming.provider.url " " dns:// "   +  dns);
        DirContext ctx 
=   new  InitialDirContext(properties);
        Attributes attributes 
=  ctx.getAttributes(domain,  new  String[] {  " MX "  });
        String recordMx 
=  (String) attributes.get( " MX " ).get();
        
if  (recordMx  !=   null )
        {
            recordMx 
=  recordMx.substring(recordMx.indexOf( "   " +   1 );
        }
        System.out.println(
" --------> "   +  recordMx  +   " <------- " );
        
return  recordMx;
    }
    
    
    
    
public   static   void  main(String[] args)  throws  NamingException
    {
        Smtp.getSmtpServer(
" 10.1.3.210 " " hwpok@163.com " ); 
    }
}


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  org.hvp.test;

import  java.net. * ;
import  java.io. * ;

public   class  Test {

    
private   static  String END_FLAG  =   " \r\n " ;

    
public   static   void  main(String[] args)  throws  Exception {
        String mx 
=   " 163mx00.mxmail.netease.com " ;
        InetAddress addr 
=  InetAddress.getByName(mx);
        Socket socket 
=   new  Socket(addr,  25 );

        InputStream in 
=  socket.getInputStream();
        OutputStream out 
=  socket.getOutputStream();

        
//  连接成功后服务器会响应:
        response(in);

        
//  首先发送HELO命令:
        send( " HELO www.javasprite.com "   +  END_FLAG, out);
        response(in);

        
//  然后发送发件人地址:
        send( " MAIL FROM: someone@somewhere.com "   +  END_FLAG, out);
        response(in);

        
//  设置收件人地址:
        send( " RCPT TO: hwpok@163.com "   +  END_FLAG, out);
        response(in);

        
//  开始发送邮件正文:
        send( " DATA "   +  END_FLAG, out);
        response(in);

        send(
" From: someone@somewhere.com "   +  END_FLAG, out);
        send(
" To: hwpok@163.com "   +  END_FLAG, out);
        send(
" Subject: Test without smtp server "   +  END_FLAG, out);
        send(
" Content-Type: text/plain; "   +  END_FLAG, out);
        send(END_FLAG 
+  END_FLAG, out);

        
//  发送邮件正文,如果用中文,需要BASE64编码:
        send( " text message body! "   +  END_FLAG, out);
        
//  每行以\r\n结束,不可过长,可拆成多行。

        
//  以"\r\n.\r\n"作为结束标志:
        send(END_FLAG  +   " . "   +  END_FLAG, out);
        response(in);

        
//  结束并确认发送:
        send( " QUIT "   +  END_FLAG, out);
        response(in);
        in.close();
        out.close();
        socket.close();
    }

    
public   static   void  response(InputStream in)  throws  Exception {
        
byte [] buffer  =   new   byte [ 102400 ];
        
int  n  =  in.read(buffer);
        
if (n > 0 ){
            String s 
=   new  String(buffer,  0 , n);
            System.out.println(s);
        }
    }

    
public   static   void  send(String s, OutputStream out)  throws  Exception {
        
byte [] buffer  =  s.getBytes();
        
if (buffer  !=   null   &&  buffer.length > 0 )
        {
            out.write(buffer);
            out.flush();
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值