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());
    }
    }*/

    }

    个人记录之用

  • 相关阅读:
    MS SQL发生死锁以及tempdb的优化资源总结
    MS SQL SERVER 簡易取得資料表實體檔案大小
    jquery选择器(转载)
    [資源]RAID是什么意思?RAID的应用
    WIN2003下安裝PHP+MYSQL資源
    MS SQL 錯誤: 15457,重要性: 0,狀態: 1
    [轉]如何修改bootini文件的/pae/awe/3gb参数
    [資源]PHP防止SQL注入
    [收藏]CSS,JS控制Table的行顏色,以及邊框
    檢查php文件中是否含有bom的php文件
  • 原文地址:https://www.cnblogs.com/juzijiang/p/9707578.html
Copyright © 2011-2022 走看看