java程序实现QQ邮件的发送:
1.要想远程登录QQ必须做如下处理:参考如下:
https://blog.****.net/Tangs_/article/details/79352310#commentBox
通过以上文章可以实现用telnet工具命令行发qq邮件。
2.如果telnet命令不可识别:以下是win10安装telnet方法参考如下:
https://jingyan.baidu.com/article/e73e26c09f6f4724adb6a7de.html
3.java实现:
package com.cl.socket2_12;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Inet4Address;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.MessageFormat;
public class MailSender {
private String smtpServer="smtp.qq.com";//SMTP邮件服务器的主机名
private int port=25;
public static void main(String[] args) throws UnknownHostException, IOException {
Message msg=new Message("[email protected]", //发件地址
"[email protected]", //收件方
"hello", //主题
"hi,I miss yuo very much."); //内容
new MailSender().sendMail(msg);
}
private void sendMail(Message msg) {
Socket socket = null;
try{
socket=new Socket(smtpServer,port);
BufferedReader br=getReader(socket); //从服务器读
PrintWriter pw=getWriter(socket); //写入aocket
String localhost=Inet4Address.getLocalHost().getHostName(); //本地主机名
//邮箱登录账号和密码,编码
String userID="[email protected]";
String pass="fbhcgilkeiapijge";
userID=new sun.misc.BASE64Encoder().encode(userID.getBytes());
pass=new sun.misc.BASE64Encoder().encode(pass.getBytes());
sendAndReceive(null,br,pw);
//qq邮箱服务器通信过程:客户端发送的“”通过pw写出
sendAndReceive("HELO "+localhost,br,pw);
//qq邮箱服务器特有的用户验证过程
sendAndReceive("STARTTLS auth login",br ,pw);
sendAndReceive("auth login",br,pw);
sendAndReceive(userID, br, pw);
sendAndReceive(pass, br, pw);
sendAndReceive("MAIL FROM:"+msg.from,br,pw);
sendAndReceive("RCPT TO:"+msg.to,br,pw);
sendAndReceive("DATA",br,pw);
pw.println(msg.data);
System.out.println("Cliet>"+msg.data);
sendAndReceive(".",br,pw);
sendAndReceive("QUIT",br,pw);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
*
* @param str:客户端发送信息
* @param br:读取服务器的响应消息流
* @param pw:发送到服务器的流
* @throws IOException
*/
private void sendAndReceive(String str, BufferedReader br, PrintWriter pw) throws IOException {
if(str!=null){
System.out.println("Client>"+str);
pw.println(str);
}
String response;
if((response=br.readLine())!=null){
System.out.println("Server>"+response);
}
}
private BufferedReader getReader(Socket scoket) throws IOException{
InputStream in=scoket.getInputStream();
return new BufferedReader(new InputStreamReader(in));
}
private PrintWriter getWriter(Socket socket) throws IOException{
OutputStream out=socket.getOutputStream();
return new PrintWriter(out,true);
}
}
class Message{
String from;
String to;
String subject;
String content;
String data;
public Message(String from, String to, String subject, String content) {
super();
this.from = from;
this.to = to;
this.subject = subject;
this.content = content;
this.data="Subject:"+subject+"\r\n"+content;
}
}
4.以下是结果: