zoukankan      html  css  js  c++  java
  • java邮件发送(以163邮箱为例)

    java邮件发送(以163邮箱为例)

    学习了:http://www.cnblogs.com/tptptptp/p/5874538.html

    使用java mail jar包:http://www.jb51.net/softs/545313.html 

    package com.stono.test;
    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    public class TestMail {
        public static void main(String[] args) throws Exception {
            Properties prop = new Properties();
            prop.put("mail.host", "smtp.163.com");
            prop.put("mail.transport.protocol", "smtp");
            prop.put("mail.smtp.auth", true);
            // 使用java发送邮件5步骤
            // 1.创建sesssion
            Session session = Session.getInstance(prop);
            // 开启session的调试模式,可以查看当前邮件发送状态
            session.setDebug(true);
            // 2.通过session获取Transport对象(发送邮件的核心API)
            Transport ts = session.getTransport();
            // 3.通过邮件用户名密码链接
            ts.connect("此处应为用户名", "此处应为授权码");
            // 4.创建邮件
            Message msg = createSimpleMail(session);
            // 5.发送电子邮件
            ts.sendMessage(msg, msg.getAllRecipients());
        }
        public static MimeMessage createSimpleMail(Session session) throws AddressException, MessagingException {
            // 创建邮件对象
            MimeMessage mm = new MimeMessage(session);
            // 设置发件人
            mm.setFrom(new InternetAddress("发件人@163.com"));
            // 设置收件人
            mm.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人@163.com"));
            // 设置抄送人
            mm.setRecipient(Message.RecipientType.CC, new InternetAddress("抄送人@163.com"));
            mm.setSubject("第一封JAVA邮件!");
            mm.setContent("咱们开会把", "text/html;charset=gbk");
            return mm;
        }
    }
  • 相关阅读:
    拓扑排序
    最小费用最大流模板
    有上下界的网络流问题
    网络流模板
    LIS+LCS+LCIS
    【Vijos】1218 数字游戏
    【Vijos】1792 摆花
    【Vijos】1431 守望者的逃离
    【wikioi】1029 遍历问题
    背包模版
  • 原文地址:https://www.cnblogs.com/stono/p/7763656.html
Copyright © 2011-2022 走看看