java使用Socket实现SMTP协议发送邮件
更新时间:2016年05月29日 11:01:34 作者:IamOkay
这篇文章主要为大家详细介绍了java使用Socket实现SMTP协议发送邮件的相关资料,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java 利用Socket实现SMTP协议发送邮件的具体代码,供大家参考,具体内容如下
package mail; 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.io.Reader; import java.net.Socket; import java.util.ArrayList; import java.util.List; import org.apache.commons.codec.binary.Base64; public class Mail { public static void main(String[] args) throws IOException { Mail mail = new Mail(); 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/><img src=\"https://www.baidu.com/img/baidu_jgylogo3.gif?v=39549282.gif\" />"); mail.setShowLog(true); mail.send(); System.out.println("程序结束"); } /** 邮件主题 **/ private String subject; /** 从此地址发出 **/ private String fromMail; /** 用户名 **/ private String userName; /** 登录密码 **/ private String password; /** SMTP 服务器地址 **/ private String smtpServer; /** SMTP 服务器端口(默认:25) **/ private int smtpPort = 25; /** 发送到 toMail 中的所有地址 **/ private List<String> toMail; /** 邮件内容 **/ private String content; /** 是否显示日志 **/ private boolean showLog; public void addToMail(String mail) { if (toMail == null) toMail = new ArrayList<String>(); toMail.add(mail); } public void send() { if (smtpServer == null) { throw new RuntimeException("smtpServer 不能为空"); } if (userName == null) { throw new RuntimeException("userName 不能为空"); } if (password == null) { throw new RuntimeException("password 不能为空"); } if (fromMail == null) { throw new RuntimeException("fromMail 不能为空"); } if (toMail == null || toMail.isEmpty()) { throw new RuntimeException("toMail 不能为空"); } if (content == null || toMail.isEmpty()) { throw new RuntimeException("content 不能为空"); } Socket socket = null; InputStream in = null; OutputStream out = null; try { socket = new Socket(smtpServer, smtpPort); socket.setSoTimeout(3000); in = socket.getInputStream(); out = socket.getOutputStream(); } catch (IOException e) { throw new RuntimeException("连接到 " + smtpServer + ":" + smtpPort + " 失败", e); } BufferedReaderProxy reader = new BufferedReaderProxy(new InputStreamReader(in), showLog); PrintWriterProxy writer = new PrintWriterProxy(out, showLog); reader.showResponse(); writer.println("HELO " + smtpServer); reader.showResponse(); writer.println("AUTH LOGIN"); reader.showResponse(); writer.println(new String(Base64.encodeBase64(userName.getBytes()))); reader.showResponse(); writer.println(new String(Base64.encodeBase64(password.getBytes()))); reader.showResponse(); writer.println("MAIL FROM:" + fromMail); reader.showResponse(); for (String mail : toMail) { writer.println("RCPT TO:" + 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 (String mail : toMail) { writer.print(mail + "; "); } writer.println(); writer.println(); writer.println(content); writer.println("."); reader.showResponse(); writer.println("QUIT"); reader.showResponse(); try { socket.close(); } catch (IOException e) { System.err.println("发送邮件完成,关闭 Socket 出错:" + e.getMessage()); } } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getFromMail() { return fromMail; } public void setFromMail(String fromMail) { this.fromMail = fromMail; } public String getSmtpServer() { return smtpServer; } public void setSmtpServer(String smtpServer) { this.smtpServer = smtpServer; } public int getSmtpPort() { return smtpPort; } public void setSmtpPort(int smtpPort) { this.smtpPort = smtpPort; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public List<String> getToMail() { return toMail; } public void setToMail(List<String> toMail) { this.toMail = toMail; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean getShowLog() { return showLog; } public void setShowLog(boolean showLog) { this.showLog = showLog; } static class PrintWriterProxy extends PrintWriter { private boolean showRequest; public PrintWriterProxy(OutputStream out, boolean showRequest) { super(out, true); this.showRequest = showRequest; } @Override public void println() { if (showRequest) System.out.println(); super.println(); } public void print(String s) { if (showRequest) System.out.print(s); super.print(s); } } static class BufferedReaderProxy extends BufferedReader { private boolean showResponse = true; public BufferedReaderProxy(Reader in, boolean showResponse) { super(in); this.showResponse = showResponse; } public void showResponse() { try { String line = readLine(); String number = line.substring(0, 3); int num = -1; try { num = Integer.parseInt(number); } catch (Exception e) { } if (num == -1) { throw new RuntimeException("响应信息错误 : " + line); } else if (num >= 400) { throw new RuntimeException("发送邮件失败 : " + line); } if (showResponse) { System.out.println(line); } } catch (IOException e) { System.out.println("获取响应失败"); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
IDEA中application.properties的图标显示不正常的问题及解决方法
这篇文章主要介绍了IDEA中application.properties的图标显示不正常的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-04-04Spring Boot利用Thymeleaf发送Email的方法教程
spring Boot默认就是使用thymeleaf模板引擎的,下面这篇文章主要给大家介绍了关于在Spring Boot中利用Thymeleaf发送Email的方法教程,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。2017-08-08Java中byte、byte数组与int、long的转换详解
这篇文章分别给大家介绍了Java中byte和int之间的转换、Java中 byte数组和int之间的转换、Java中byte数组和long之间的转换以及整理了整体工具类的源码,需要的朋友可以参考借鉴,下面来一起看看吧。2017-02-02Spring Boot实现异步请求(Servlet 3.0)
在spring 3.2 及以后版本中增加了对请求的异步处理,这篇文章主要介绍了Spring Boot实现异步请求(Servlet 3.0),感兴趣的小伙伴们可以参考一下。2017-04-04Spring Boot结合IDEA自带Maven插件如何快速切换profile
IDEA是目前 Java 开发者中使用最多的开发工具,它有着简约的设计风格,强大的集成工具,便利的快捷键,这篇文章主要介绍了Spring Boot结合IDEA自带Maven插件快速切换profile,需要的朋友可以参考下2023-03-03
最新评论