此文章为servlet写的

首先需要先下载一个jar包:mail jar


package sendemail;import java.util.Properties;import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;public class MailInfo {// 发送邮件的服务器的IP和端口    private String mailServerHost;  // 发送邮件的服务器的Smtp协议  private String mailServerPort="smtp.mxhichina.com";    // 邮件发送者的地址    private String fromAddress="";    // 邮件接收者的地址    private String toAddress;    // 登陆邮件发送服务器的用户名和密码    private String userName="";  private String password="";// 是否需要身份验证    private boolean validate = false;    // 邮件主题    private String subject;    // 邮件的文本内容    private String content;    public String getMailServerHost() {return mailServerHost;}public void setMailServerHost(String mailServerHost) {this.mailServerHost = mailServerHost;}public String getMailServerPort() {return mailServerPort;}public void setMailServerPort(String mailServerPort) {this.mailServerPort = mailServerPort;}public String getFromAddress() {return fromAddress;}public void setFromAddress(String fromAddress) {this.fromAddress = fromAddress;}public String getToAddress() {return toAddress;}public void setToAddress(String toAddress) {this.toAddress = toAddress;}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 isValidate() {return validate;}public void setValidate(boolean validate) {this.validate = validate;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public void senemail(){//发送邮件Properties props=new Properties();props.setProperty("mail.smtp.auth", "true");props.setProperty("mail.transport.protocol", "smtp");//设置邮箱smtp协议//"smtp.mxhichina.com"props.setProperty("mail.host", mailServerPort);//设置邮箱账号密码Session session1 =Session.getInstance(props,new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {//return new PasswordAuthentication("usadoctor@edoctordata.cn ","Edoctor1234");  return new PasswordAuthentication(userName,password);}});       session1.setDebug(true);        Message msg = new MimeMessage(session1);//创建message对象      try {//发件人msg.setFrom(new InternetAddress(fromAddress));//发件标题msg.setSubject(subject);//发件正文msg.setContent("<p><img src=\"http://edoctordata.vip:8080/img/logo.png\" /><div align=\"center\" style=\" border:#58CCE4 solid 1px;width:500px;align=\"center\" \"></p><font color=\"#0099cc\"><h2>邮件通知</h2><font size=\"2px;\"><div align=\"left\">"+content+"</div></font></br><div align=\"left\"><p>这是一封邮件</p></div></div>","text/html;charset=UTF-8");    //指定收件人msg.setRecipients(RecipientType.TO, InternetAddress.parse(toAddress));Transport.send(msg);/** 添加附件Multipart mainPart = new MimeMultipart();BodyPart body = new MimeBodyPart();body.setContent(content, "text/html;charset=UTF-8");BodyPart attachbody = new MimeBodyPart();//文件名System.out.println("fileName:"+fileName);FileDataSource fds = new FileDataSource(fileName);DataHandler dh = new DataHandler(fds);String fileNames = fileName.substring(fileName.lastIndexOf("\\"));System.out.println("fileNamess"+fileNames);BASE64Encoder enc = new BASE64Encoder(); attachbody.setFileName("=?GBK?B?" + enc.encode(fileNames.getBytes("GBK")) + "?=");attachbody.setDataHandler(dh);mainPart.addBodyPart(body);mainPart.addBodyPart(attachbody);msg.setContent(mainPart);       //指定收件人         msg.setRecipients(RecipientType.TO, InternetAddress.parse(toAddress));Transport.send(msg);*/} catch (MessagingException e) {// TODO Auto-generated catch blocke.printStackTrace();}     }
}
/**
有时会把一些参数放在xml配置文件中,下面是利用dom读取xml的一种方式Element element = null;// 可以使用绝对路劲File f = new File("edoctor-email.xml");// documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件)DocumentBuilder db = null;DocumentBuilderFactory dbf = null;try {// 返回documentBuilderFactory对象dbf = DocumentBuilderFactory.newInstance();// 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象db = dbf.newDocumentBuilder();// 得到一个DOM并返回给document对象Document dt = db.parse(f);// 得到一个elment根元素element = dt.getDocumentElement();// 获得根元素下的子节点NodeList childNodes = element.getChildNodes();// 遍历这些子节点for (int i = 0; i < childNodes.getLength(); i++) {// 获得每个对应位置i的结点Node node1 = childNodes.item(i);if ("email".equals(node1.getNodeName())) {// 获得<emails>下的节点NodeList nodeDetail = node1.getChildNodes();// 遍历<emails>下的节点for (int j = 0; j < nodeDetail.getLength(); j++) {// 获得<emails>元素每一个节点Node detail = nodeDetail.item(j);if ("formAddress".equals(detail.getNodeName())){fromAddress=detail.getTextContent();} else if ("port".equals(detail.getNodeName())){mailServerPort=detail.getTextContent();}else if ("userName".equals(detail.getNodeName())){userName=detail.getTextContent();}else if ("userPwd".equals(detail.getNodeName())){password=detail.getTextContent();}}}}}catch (Exception e) {e.printStackTrace();}*/

SMTP发送邮件接口相关推荐

  1. Lumen / Laravel 使用网易邮箱 SMTP 发送邮件

    Laravel 是目前最流行的PHP框架,而Lumen 是 Laravel 的精简版,主要用于接口开发. Laravel 邮件发送服务基于 Symfony 组件 Swift Mailer. 本文记录了 ...

  2. Lumen / Laravel 5.5 使用网易邮箱 SMTP 发送邮件

    2019独角兽企业重金招聘Python工程师标准>>> Laravel 是目前最流行的PHP框架,而Lumen 是 Laravel 的精简版,主要用于接口开发. Laravel 邮件 ...

  3. 【Email】Java发送邮件接口与配置类

    说明 这文章17年的,不知道为啥,被放到草稿箱了.现在发表下. 转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 可能最近写博客时间不 ...

  4. php5.3发送邮件,Lumen / Laravel 5.5 使用网易邮箱 SMTP 发送邮件

    Laravel 是目前最流行的PHP框架,而Lumen 是 Laravel 的精简版,主要用于接口开发. Laravel 邮件发送服务基于 Symfony 组件 Swift Mailer. 本文记录了 ...

  5. JAVA定时发送邮件接口

    定时发送邮件接口,只需要传入发送邮件的时间,邮箱账号,邮件内容,即可定时发送邮件 一.定时任务 二.多线程 三.发送邮件 邮件依赖 <!--email--><dependency&g ...

  6. Python使用SMTP发送邮件

    1.目标 使用SMTP发送邮件 2.系统环境 操作系统:Win7 64 Python版本:3.7.0 3.代码参考 #config:utf-8                         #强制使 ...

  7. Centos 配置mailx使用外部smtp发送邮件

    今天写定时脚本时,用到监控服务器是否备份成功,配置sentmail和postfix总是出问题,原本想只是接受个信息,没必要那么麻烦,直接配置mailx就能满足了,具体配置如下: 1.安装mailx y ...

  8. python发送qq邮件列表_Python SMTP发送邮件

    发送邮件是个很常用的功能.比如自己写个脚本获取并分析股票或期货数据,如果发现有交易机会.此时可以发个邮件来提醒自己. SMTP即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控 ...

  9. 在.NET中使用SMTP发送邮件

    这是一篇转载,可能对大家很有用啊,放首页看看是否有参考价值.本文提到的方案仍然不能算是完全解决所有问题,最佳的dotNET下通过SMTP(带验证)发送邮件的机制是什么,不知道大家有什么好的看法! 摘要 ...

最新文章

  1. 【转】使用JDK自带jvisualvm监控tomcat
  2. sql limit 子句_Java 8流中的常见SQL子句及其等效项
  3. linux sh expr冒号,linux expr命令参数及用法详解
  4. php微信网页开发实现自动登录注册功能实例
  5. Service Unavailable解决方法
  6. 微课|中学生可以这样学Python(例8.21):选择法排序
  7. codeforces 984 A. Game
  8. 一文带你了解如何排查内存泄漏导致的页面卡顿现象
  9. [c++]在C++中定义常量的两种方法的比较
  10. linux生成密码文本,Linux下用makepasswd和passwordmaker生成密码
  11. Win10技巧:如何确定电脑是否适用Hyper-V虚拟机?
  12. caffe中 solver.prototxt文件
  13. 从web层运作流程认识Struts2
  14. linux安装eclipse教程,Linux下的Eclipse安装
  15. 武汉科技大学计算机学院研究生复试,2019年武汉科技大学硕士研究生复试及录取工作方案...
  16. https工作原理及CA证书部署
  17. Python 爬虫——爬取文章自动发送QQ群
  18. 智慧城市构建的核心问题:数据信息的安全性与隐私性
  19. 第二章 Selenium-API操作
  20. JavaSE (9)

热门文章

  1. java 正则表达式空格无法匹配
  2. 投资大师经典名人名言
  3. android机清理存储卡,越用越卡!Android手机需要清理内存吗?
  4. maven打包忽略注解_maven打包包含注释
  5. python去除句子中所有数字
  6. 记载_扩展欧几里得求模逆
  7. Google 数据可视化团队总结的可视化硬核指南!
  8. 电脑开机速度慢怎么解决 电脑启动速度慢怎么办
  9. rm命令——删除文件或目录
  10. dllhost.exe病毒清除办法