zoukankan      html  css  js  c++  java
  • JavaMail发送邮件工具类

    SendMailUitls:

    package com.duocy.util;

    import java.net.InetAddress;

    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMessage.RecipientType;

    public class SendMailUitls {
    public static void senMail(String toMail, String code) throws Exception {
    InetAddress addrs = InetAddress.getLocalHost();
    String addr=addrs.getHostAddress();
    Properties properties = new Properties();
    properties.put("mail.transport.protocol", "smtp"); // 连接协议
    properties.put("mail.smtp.host", "smtp.163.com"); // 主机名
    properties.put("mail.smtp.port", 25); // 端口号
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.ssl.socketFactory", "true"); // 设置是否使用ssl安全连接 (一般都使用)
    properties.put("mail.debug", "false"); // 设置是否显示debug信息 true 会在控制台显示相关信息
    // 得到回话对象
    Session session = Session.getInstance(properties);
    // 获取邮件对象
    Message message = new MimeMessage(session);
    // 设置发件人邮箱地址
    message.setFrom(new InternetAddress("15823161396@163.com", "10086abc"));
    // 设置收件人地址
    message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress(toMail) });
    // 设置邮件标题
    message.setSubject("由JavaMail发出的测试邮件");
    // 设置邮件内容
    message.setContent("<h2>请点击以下链接址激活注册:</br>(若无法点击,请复制到浏览器中打开)</h2><h4><a herf='http://"+addr+":8080/duocy/SelectClientByCode?code="+code+"'>http://"+addr+":8080/duocy/SelectClientByCode?code="+code+"</a></h4>","text/html;Charset=UTF-8");
    // 得到邮差对象
    Transport transport = session.getTransport();
    // 连接自己的邮箱账户
    transport.connect("15823161396@163.com", "10086abc"); //密码为刚才得到的授权码
    // 发送邮件
    transport.sendMessage(message, message.getAllRecipients());
    System.out.println("用户激活码:"+code);
    System.out.println("主机地址:"+addr);
    }

    /*public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while(e.hasMoreElements()){
    System.out.println(e.nextElement());
    }
    }*/

    }

    个人记录之用

  • 相关阅读:
    利用Selenium自动化web测试
    【译】.NET中六个重要的概念:栈、堆、值类型、引用类型、装箱和拆箱
    SlickGrid example 8:折线图
    SlickGrid example 7:鼠标事件
    SlickGrid example 6:Ajax加载
    SlickGrid example 5:带子项的展开收缩
    SlickGrid example 4: 过滤
    CentOS下配置iptables防火墙 linux NAT(iptables)配置
    ntp server
    parted
  • 原文地址:https://www.cnblogs.com/juzijiang/p/9707578.html
Copyright © 2011-2022 走看看