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

    public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
    {
    boolean debug = false;

    //Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", true);

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++)
    {
    addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");

     

    //Most Important 

    Transport transport = session.getTransport("smtps");// smtp(QQmail) vs smtps(gmail)

    transport.connect("smtp.gmail.com","username","passwd");//TODO
    transport.sendMessage(msg, new Address[]{new InternetAddress(to)});

    System.out.println("DONE");
    }

  • 相关阅读:
    java过滤器 Fliter
    input标签name、value与id属性
    python 简单的数据库操作之转账
    正则表达式基本语法
    适合新手的Python爬虫小程序
    如何使用EditPlus将json格式字符串默认为UTF-8格式
    codeforces 527C:STL set
    codeforces 527B:瞎搞
    HDU 3397 线段树
    HDU 3436:splay tree
  • 原文地址:https://www.cnblogs.com/star4knight/p/javamail_intro.html
Copyright © 2011-2022 走看看