zoukankan      html  css  js  c++  java
  • java 邮件(2)

    /**
      * 方法描述:发送带附件的邮件
      *
      * @throws UnsupportedEncodingException
      */
      public static boolean sendEmailWithAttachment(Email email)
          throws UnsupportedEncodingException {
        Properties prop = new Properties();
        prop.put("mail.smtp.host", mailServer);
        prop.put("mail.smtp.auth", "true");
        Authenticator auth = new MailAuthenticator();
        Session session = Session.getDefaultInstance(prop, auth);
        Message message = new MimeMessage(session);
        boolean flag = false;
        try {
          message.setSubject(email.getEmail_subject());// 设置邮件主题
          message.setHeader("Header:", email.getEmail_header()); // 设置邮件标题
          message.setSentDate(new Date()); // 设置发送时间
          Address addressFrom = new InternetAddress(mailAddress, mailAccount); // 设置发信人地址
          message.setFrom(addressFrom);
          BodyPart messageBodyPart = new MimeBodyPart();
          messageBodyPart.setContent(email.getEmail_content(),  "text/html;charset=utf-8");
          Multipart multipart = new MimeMultipart();
          multipart.addBodyPart(messageBodyPart);
          MimeBodyPart mimeBodyPart;
          //多个附件
          for (int i = 0; i < email.getEmail_file().size(); i++) {
            Map<String, String> map = email.getEmail_file().get(i);
            String fileSource="";
            String fileName="";
            for (String key : map.keySet()) {
              fileSource = key;
              fileName = map.get(key);
              }
            mimeBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(fileSource);
            mimeBodyPart.setDataHandler(new DataHandler(source));
            mimeBodyPart.setFileName(MimeUtility.encodeText(fileName));
            multipart.addBodyPart(mimeBodyPart);// Put parts in
          }
          message.setContent(multipart);
         
          // 设置多个收件人地址
          List<String> list = email.getEmail_to();
          String toAddress = SendEmail.getAddress(list);
         
          InternetAddress[] internetAddressTo = new InternetAddress().parse(toAddress);
          message.setRecipients(Message.RecipientType.TO, internetAddressTo);

          // 设置多个抄送地址
          String ccAddress = SendEmail.getAddress(email.getEmail_cc());
          InternetAddress[] internetAddressCC = new InternetAddress().parse(ccAddress);
          message.setRecipients(Message.RecipientType.CC, internetAddressCC);
         
          message.saveChanges();
          System.out.println("开始发送邮件……");

          Transport.send(message);
          System.out.println("发送成功!");
          flag = true;
        } catch (MessagingException e) {
          System.out.println("发送失败!");
          flag = false;
          e.printStackTrace();

        }
        return flag;
      }

  • 相关阅读:
    搜狗输入法——从繁体改成简体
    java官网下载
    eclipse官网下载
    java拷贝指定文件夹下的指定文件类型
    bat脚本——删除当前文件夹的所有指定文件类型
    windows系统下载地址大全&大白菜下载和教程
    struts2——文件下载自定义文件名,包括中文
    struts2——文件下载(简单的功能)
    nginx官网下载&百度云分享
    Apache HTTP Server——官网下载
  • 原文地址:https://www.cnblogs.com/northeastTycoon/p/5911736.html
Copyright © 2011-2022 走看看