zoukankan      html  css  js  c++  java
  • 发送邮件

    1.导入jar包

    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.3.3</version>
    </dependency>
    <dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
    </dependency>

    2、写实体类

    // 发送服务器
    private String host;
    // 发送主题
    private String subject;
    // 发送用户名
    private String fromName;
    // 发送密码
    private String fromPassword;
    // 接受的用户
    private String toAddress;
    // 发送内容
    private String content;

    3、配置文件

    host=smtp.163.com
    fromName=157****5505@163.com
    fromPassword=*******

    4、邮件发送

    //发送html邮件
    public void SendHtmlEmail(CommonEmail email) {
    HtmlEmail htmlEmail = new HtmlEmail();
    try {
    // 设置邮件的各个参数
    htmlEmail.setAuthentication(email.getFromName(), email.getFromPassword());
    htmlEmail.setFrom(email.getFromName());
    htmlEmail.setHostName(email.getHost());
    htmlEmail.setCharset("UTF-8");
    htmlEmail.setSubject(email.getSubject());
    htmlEmail.setHtmlMsg(email.getContent());
    htmlEmail.addTo(email.getToAddress());
    htmlEmail.addCc(email.getFromName());
    // 发送邮件
    htmlEmail.send();
    System.out.println("发送邮件成功!!");
    } catch (EmailException e) {
    System.out.println("邮件发送失败!!");
    e.printStackTrace();
    }

    }

    6、测试邮件发送

    static {
    properties = new Properties();
    try {
    String path = SendEmailTest.class.getClassLoader().getResource("emailConfig.properties").getPath();
    File file = new File(path);
    properties.load(new FileInputStream(file));
    } catch (Exception e) {
    System.out.println("配置文件加载失败");
    }
    }
    public static void main(String[] args) {
    CommonEmail email = new CommonEmail();
    email.setFromName(properties.getProperty("fromName"));
    email.setFromPassword(properties.getProperty("fromPassword"));
    email.setHost(properties.getProperty("host"));
    email.setSubject("第一次使用邮件发送功能");
    email.setToAddress("115****630@qq.com");
    email.setContent("<a href='www.baidu.com'>百度一下</a>");

    CommonEmailSender sender = new CommonEmailSender();
    sender.SendHtmlEmail(email);

    }

    其中遇到的问题:

          ①com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163 smtp7,C8CowACX1L3Z9n9a5uczGQ--.37216S2 1518335705,please see http://mail.163.com/help/help_spam_16.htm?ip=58.246.226.97&hostid=smtp7&time=1518335705

    解决方法:邮件抄送给自己一份就可以了

    发邮件报错535 Error:authentication failed解决方法

    解决方法:可能有的原因:①你有授权码,所以密码是你的授权码,而不是你的密码 ②你的密码输入错误

  • 相关阅读:
    Pixel XL编译和烧录Android 8.0
    公式编辑器CVE-2018-0798样本分析
    CVE-2021-33739 EOP漏洞分析
    Firefox 设置 Burpsuite 代理抓取本地数据包
    前端ECharts框架绘制各种图形
    c 除法反汇编算法
    IDA sig签名批量脚本
    从零构建自己的远控•客户端设计面向对象(13)
    从零构建自己的远控•AES加解密Demo(12)
    从零构建自己的远控•图像切割算法构思(11)
  • 原文地址:https://www.cnblogs.com/df1151112630/p/8442471.html
Copyright © 2011-2022 走看看