现在的位置: 首页 > 综合 > 正文

以流的形式发送邮件很霸道的…

2014年01月09日 ⁄ 综合 ⁄ 共 1629字 ⁄ 字号 评论关闭

 

 
1.   学习了怎样通过以流的形式发送邮件在这里记下来。这里用到了base64 Encoder编码。在上一篇写到了如何编码这里就没有写了。
2.   代码如下:
3.   import java.io.BufferedReader;
4.   import java.io.IOException;
5.   import java.io.InputStreamReader;
6.   import java.io.OutputStream;
7.   import java.net.Socket;
 
8.   publicclass SocketTest {
9.   publicstaticvoid main(String[] args) throws IOException {
 
10.Socket sk=new Socket("smtp.sohu.com",25);//spmt协议例如:搜狐smpt.sohu.com
11.OutputStream os=sk.getOutputStream();
12.BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream()));
13.System.out.println(br.readLine());
14.os.write("ehlo name/r/n".getBytes());//计算机名称
15.System.out.println(br.readLine());
16.System.out.println(br.readLine());
17.System.out.println(br.readLine());
 
18.os.write("auth login/r/n".getBytes());
19.System.out.println(br.readLine());
 
20.os.write("user/r/n".getBytes()); //base64 Encoder编码的搜狐用户名
21.System.out.println(br.readLine());
22.os.write("pass/r/n".getBytes());//base64 Encoder编码的搜狐密码
23.System.out.println(br.readLine());
 
24.os.write("mail from:<邮箱名>/r/n".getBytes());//自己搜狐的邮箱
25.System.out.println(br.readLine());
 
26.os.write("rcpt to:<邮箱名>/r/n".getBytes());//要接收用户的邮箱
27.System.out.println(br.readLine());
 
 
28.os.write("data/r/n".getBytes());
29.System.out.println(br.readLine());
30.os.write("from:<邮箱名>/r/n".getBytes());//自己搜狐的邮箱
31.os.write("to:<邮箱名>/r/n".getBytes());//要接收用户的邮箱
32.os.write("subject:你好/r/n".getBytes());//邮箱主题
33.os.write("/r/n".getBytes());
34.os.write("小李同志你好/r/n".getBytes());//邮箱内容
 
35.os.write("./r/n".getBytes());//以点结束邮箱内容
36.System.out.println(br.readLine());
37.os.write("quit/r/n".getBytes());//退出邮箱
 
 
 
38.br.close();
39.os.close();
40.sk.close();
 
 
 
41.}
42.}
 

抱歉!评论已关闭.