zoukankan      html  css  js  c++  java
  • javamail邮件发送demo code 全新时代

    需要引用的javax.mail.*类,在javamail mailapi.jar中。

    Properties props = System.getProperties();
    props.setProperty("mail.smtp.host", "smtp.qq.com");
    props.setProperty("mail.smtp.port", "25");
    props.put("mail.smtp.auth", "true");
    
    final String username = "xxx@qq.com";
            final String password = "xxx";
            Session session = Session.getDefaultInstance(props,
                    new Authenticator()  {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);
                        }
                    });
    
    try {
                // -- Create a new message --
                Message msg = new MimeMessage(session);
                // -- Set the FROM and TO fields --
                msg.setFrom(new InternetAddress(username));
                msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xxx@qq.com",false));
                msg.setSubject("邮件标题");
                
                MimeMultipart mp=new MimeMultipart();
                BodyPart bp = new MimeBodyPart();
                String html = "邮件内容<h1>Hello World</h1>";
                bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"   + html, "text/html;charset=UTF-8");
                mp.addBodyPart(bp);
                
                BodyPart bp1 = new MimeBodyPart();
                
                //附件
                FileDataSource fileds = new FileDataSource("C:/people.xml");
                bp1.setDataHandler(new DataHandler(fileds));
                bp1.setFileName(fileds.getName());
                mp.addBodyPart(bp1);
                
                msg.setSentDate(new Date());
                msg.setContent(mp);
                
                msg.saveChanges();
                Transport.send(msg);
                System.out.println("Message sent.");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
  • 相关阅读:
    二叉树的遍历
    深度优先遍历和广度优先遍历
    N的阶乘末尾有多少个0
    框架产生的历史
    Ansible--初始ansible
    日积跬步05
    日积跬步04
    日积跬步03
    日积跬步02
    日积跬步01
  • 原文地址:https://www.cnblogs.com/simpledev/p/2975933.html
Copyright © 2011-2022 走看看