java使用Socket实现SMTP协议发送邮件
本文实例为大家分享了java利用Socket实现SMTP协议发送邮件的具体代码,供大家参考,具体内容如下
packagemail;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.io.PrintWriter;
importjava.io.Reader;
importjava.net.Socket;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.commons.codec.binary.Base64;
publicclassMail{
publicstaticvoidmain(String[]args)throwsIOException{
Mailmail=newMail();
mail.setSmtpServer("smtp.qq.com");
mail.setFromMail("1344364****@qq.com");
mail.addToMail("105648****@qq.com");
mail.addToMail("long*****@sina.com");
mail.setUserName("134364****");
mail.setPassword("*************");
mail.setSubject("测试邮件");
mail.setContent("<h1>你好</h1><br/><imgsrc=\"https://www.baidu.com/img/baidu_jgylogo3.gif?v=39549282.gif\"/>");
mail.setShowLog(true);
mail.send();
System.out.println("程序结束");
}
/**邮件主题**/
privateStringsubject;
/**从此地址发出**/
privateStringfromMail;
/**用户名**/
privateStringuserName;
/**登录密码**/
privateStringpassword;
/**SMTP服务器地址**/
privateStringsmtpServer;
/**SMTP服务器端口(默认:25)**/
privateintsmtpPort=25;
/**发送到toMail中的所有地址**/
privateList<String>toMail;
/**邮件内容**/
privateStringcontent;
/**是否显示日志**/
privatebooleanshowLog;
publicvoidaddToMail(Stringmail){
if(toMail==null)
toMail=newArrayList<String>();
toMail.add(mail);
}
publicvoidsend(){
if(smtpServer==null){
thrownewRuntimeException("smtpServer不能为空");
}
if(userName==null){
thrownewRuntimeException("userName不能为空");
}
if(password==null){
thrownewRuntimeException("password不能为空");
}
if(fromMail==null){
thrownewRuntimeException("fromMail不能为空");
}
if(toMail==null||toMail.isEmpty()){
thrownewRuntimeException("toMail不能为空");
}
if(content==null||toMail.isEmpty()){
thrownewRuntimeException("content不能为空");
}
Socketsocket=null;
InputStreamin=null;
OutputStreamout=null;
try{
socket=newSocket(smtpServer,smtpPort);
socket.setSoTimeout(3000);
in=socket.getInputStream();
out=socket.getOutputStream();
}catch(IOExceptione){
thrownewRuntimeException("连接到"+smtpServer+":"+smtpPort+"失败",e);
}
BufferedReaderProxyreader=newBufferedReaderProxy(newInputStreamReader(in),showLog);
PrintWriterProxywriter=newPrintWriterProxy(out,showLog);
reader.showResponse();
writer.println("HELO"+smtpServer);
reader.showResponse();
writer.println("AUTHLOGIN");
reader.showResponse();
writer.println(newString(Base64.encodeBase64(userName.getBytes())));
reader.showResponse();
writer.println(newString(Base64.encodeBase64(password.getBytes())));
reader.showResponse();
writer.println("MAILFROM:"+fromMail);
reader.showResponse();
for(Stringmail:toMail){
writer.println("RCPTTO:"+mail);
reader.showResponse();
}
writer.println("DATA");
writer.println("Content-Type:text/html");
if(subject!=null){
writer.println("Subject:"+subject);
}
writer.println("From:"+fromMail);
writer.print("To:");
for(Stringmail:toMail){
writer.print(mail+";");
}
writer.println();
writer.println();
writer.println(content);
writer.println(".");
reader.showResponse();
writer.println("QUIT");
reader.showResponse();
try{
socket.close();
}catch(IOExceptione){
System.err.println("发送邮件完成,关闭Socket出错:"+e.getMessage());
}
}
publicStringgetSubject(){
returnsubject;
}
publicvoidsetSubject(Stringsubject){
this.subject=subject;
}
publicStringgetFromMail(){
returnfromMail;
}
publicvoidsetFromMail(StringfromMail){
this.fromMail=fromMail;
}
publicStringgetSmtpServer(){
returnsmtpServer;
}
publicvoidsetSmtpServer(StringsmtpServer){
this.smtpServer=smtpServer;
}
publicintgetSmtpPort(){
returnsmtpPort;
}
publicvoidsetSmtpPort(intsmtpPort){
this.smtpPort=smtpPort;
}
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(Stringcontent){
this.content=content;
}
publicList<String>getToMail(){
returntoMail;
}
publicvoidsetToMail(List<String>toMail){
this.toMail=toMail;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicbooleangetShowLog(){
returnshowLog;
}
publicvoidsetShowLog(booleanshowLog){
this.showLog=showLog;
}
staticclassPrintWriterProxyextendsPrintWriter{
privatebooleanshowRequest;
publicPrintWriterProxy(OutputStreamout,booleanshowRequest){
super(out,true);
this.showRequest=showRequest;
}
@Override
publicvoidprintln(){
if(showRequest)
System.out.println();
super.println();
}
publicvoidprint(Strings){
if(showRequest)
System.out.print(s);
super.print(s);
}
}
staticclassBufferedReaderProxyextendsBufferedReader{
privatebooleanshowResponse=true;
publicBufferedReaderProxy(Readerin,booleanshowResponse){
super(in);
this.showResponse=showResponse;
}
publicvoidshowResponse(){
try{
Stringline=readLine();
Stringnumber=line.substring(0,3);
intnum=-1;
try{
num=Integer.parseInt(number);
}catch(Exceptione){
}
if(num==-1){
thrownewRuntimeException("响应信息错误:"+line);
}elseif(num>=400){
thrownewRuntimeException("发送邮件失败:"+line);
}
if(showResponse){
System.out.println(line);
}
}catch(IOExceptione){
System.out.println("获取响应失败");
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
