当前位置: 首页>>代码示例>>Java>>正文


Java MimeBodyPart.attachFile方法代码示例

本文整理汇总了Java中javax.mail.internet.MimeBodyPart.attachFile方法的典型用法代码示例。如果您正苦于以下问题:Java MimeBodyPart.attachFile方法的具体用法?Java MimeBodyPart.attachFile怎么用?Java MimeBodyPart.attachFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.mail.internet.MimeBodyPart的用法示例。


在下文中一共展示了MimeBodyPart.attachFile方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createMimeMessageWithAttachment

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
public static MimeMessage createMimeMessageWithAttachment(Session session, File attachment) throws MessagingException, AddressException, IOException {
  MimeMessage message = createMimeMessage(session);

  Multipart multiPart = new MimeMultipart();

  MimeBodyPart textPart = new MimeBodyPart();
  textPart.setText("text");
  multiPart.addBodyPart(textPart);

  MimeBodyPart filePart = new MimeBodyPart();
  filePart.attachFile(attachment);
  multiPart.addBodyPart(filePart);

  message.setContent(multiPart);

  return message;
}
 
开发者ID:camunda,项目名称:camunda-bpm-mail,代码行数:18,代码来源:MailTestUtil.java

示例2: addAttachFile

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
/**
 * 
 * 发送邮件时添加附件列表
 * @param mp
 * @param attachFiles
 * @return
 * @throws MailException
 * @throws IOException
 * @throws MessagingException
 */
private Multipart  addAttachFile(Multipart mp, Set<String> attachFiles) throws MailException, IOException, MessagingException{
	
	if(mp == null){
		throw new MailException("bean Multipart is null .");
	}
	
	//没有附件时直接返回
	if(attachFiles == null || (attachFiles.size())== 0){
		return mp;
	}
	Iterator<String> iterator = attachFiles.iterator();
	while (iterator.hasNext()) {
		String fileName  = iterator.next();
		MimeBodyPart mbp_file = new MimeBodyPart();
		mbp_file.attachFile(fileName);
		mp.addBodyPart(mbp_file);
		//防止乱码
		String encode = MimeUtility.encodeText(mbp_file.getFileName());
           mbp_file.setFileName(encode); 
		
	}
	
	return mp;
}
 
开发者ID:thinking-github,项目名称:nbone,代码行数:35,代码来源:MailServiceImpl.java

示例3: createAttachment

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
private MimeBodyPart createAttachment(File file, String filename, String desc) throws IOException, MessagingException {
    MimeBodyPart mbp = new MimeBodyPart();
    mbp.attachFile(file);
    FileDataSource fds = new FileDataSource(file);
    mbp.setDataHandler(new DataHandler(fds));
    mbp.setDescription(desc);
    mbp.setFileName(MimeUtility.encodeText(filename));
    return mbp;
}
 
开发者ID:TFdream,项目名称:okmail,代码行数:10,代码来源:Mail.java

示例4: sendMultipartMessage

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
public void sendMultipartMessage(String subject, String[] to, String text, String attach)
    throws MessagingException, IOException {
   
    MimeMessage message = new MimeMessage(senderSession);
    message.setFrom(new InternetAddress(pManager.get_SENDER_From())); // FROM
    
    for(int i=0; i < to.length; i++) {
        if(!to[i].equals("")) {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); // TO
        }
    }
    
    message.setSubject(subject); //SUBJECT
    
    Multipart mp = new MimeMultipart();
    
    BodyPart textPart = new MimeBodyPart();
    textPart.setText(text);
    mp.addBodyPart(textPart);  // TEXT
    
    MimeBodyPart attachPart = new MimeBodyPart();
    attachPart.attachFile(attach);
    mp.addBodyPart(attachPart); // ATTACH
    
    message.setContent(mp);
    transport.sendMessage(message, message.getAllRecipients());
}
 
开发者ID:adbenitez,项目名称:MailCopier,代码行数:28,代码来源:MailCopier.java

示例5: addMtomPart

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
private void addMtomPart(MimeMultipart mp, RequestableHttpVariable variable, Object httpVariableValue) throws IOException, MessagingException {
	String stringValue = ParameterUtils.toString(httpVariableValue);
	String filepath = Engine.theApp.filePropertyManager.getFilepathFromProperty(stringValue, getProject().getName());
	String cid = variable.getMtomCid(stringValue);
	
	Engine.logBeans.debug("(HttpConnector) Prepare the MTOM attachment with cid: " + cid + ". Converting the path '" + stringValue + "' to '" + filepath + "'");
	
	MimeBodyPart bp = new MimeBodyPart();
	bp.attachFile(filepath);
	bp.setContentID(cid);
	mp.addBodyPart(bp);
}
 
开发者ID:convertigo,项目名称:convertigo-engine,代码行数:13,代码来源:HttpConnector.java

示例6: createMessageContent

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
protected void createMessageContent(Message message, SendMailRequest request) throws MessagingException, IOException {
  if (isTextOnlyMessage(request)) {
    message.setText(request.getText());

  } else {
    Multipart multiPart = new MimeMultipart();

    if (request.getText() != null) {
      MimeBodyPart textPart = new MimeBodyPart();
      textPart.setText(request.getText());
      multiPart.addBodyPart(textPart);
    }

    if (request.getHtml() != null) {
      MimeBodyPart htmlPart = new MimeBodyPart();
      htmlPart.setContent(request.getHtml(), MailContentType.TEXT_HTML.getType());
      multiPart.addBodyPart(htmlPart);
    }

    if (request.getFileNames() != null) {
      for (String fileName : request.getFileNames()) {
        MimeBodyPart part = new MimeBodyPart();
        part.attachFile(fileName);
        multiPart.addBodyPart(part);
      }
    }

    message.setContent(multiPart);
  }
}
 
开发者ID:camunda,项目名称:camunda-bpm-mail,代码行数:31,代码来源:SendMailConnector.java

示例7: sendEmail

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
/**
 * 用来发送邮件
 *
 * @param session 与发送邮件的主机的连接(会话)
 * @param email   写好的邮件
 * @throws Exception 出现异常
 */
public static void sendEmail(Session session, Email email) throws Exception {
    // 获取发送邮件的信息类
    MimeMessage message = new MimeMessage(session);

    // 设置发送方邮件地址
    message.setFrom(new InternetAddress(email.getFrom()));
    // 设置发送类型和被发送方的邮件地址
    if (!email.getTo().isEmpty()) {
        message.setRecipients(RecipientType.TO, email.getTo());
    }
    if (!email.getCc().isEmpty()) {
        message.setRecipients(RecipientType.CC, email.getCc());
    }
    if (!email.getBcc().isEmpty()) {
        message.setRecipients(RecipientType.BCC, email.getBcc());
    }
    // 设置邮件主题
    message.setSubject(email.getSubject(), "utf-8");
    // 设置邮件内容
    MimeMultipart content = new MimeMultipart();
    // 邮件正文
    MimeBodyPart text = new MimeBodyPart();
    text.setContent(email.getContent(), email.getType());
    content.addBodyPart(text);
    // 设置附件
    if (email.getAttachments() != null) {
        for (AttachmentBean attachment : email.getAttachments()) {
            MimeBodyPart part = new MimeBodyPart();
            part.attachFile(attachment.getFile());
            part.setFileName(MimeUtility.encodeText(attachment
                    .getFileName()));
            if (attachment.getCid() != null) {
                part.setContentID(attachment.getCid());
            }
            content.addBodyPart(part);
        }
    }
    // 将邮件内容添加到信息中
    message.setContent(content);
    // 发送邮件
    Transport.send(message);
}
 
开发者ID:FlyingHe,项目名称:UtilsMaven,代码行数:50,代码来源:MailUtils.java

示例8: sendEmail

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
/**
 * <p>Send email.</p>
 * @param pAddParam additional param
 * @param pMsg msg to mail
 * @throws Exception - an exception
 **/
@Override
public final void sendEmail(final Map<String, Object> pAddParam,
  final EmailMsg pMsg) throws Exception {
  Properties props = new Properties();
  for (EmailStringProperty esp
    : pMsg.getEmailConnect().getStringProperties()) {
    props.put(esp.getPropertyName(), esp.getPropretyValue());
  }
  for (EmailIntegerProperty eip
    : pMsg.getEmailConnect().getIntegerProperties()) {
    props.put(eip.getPropertyName(), eip.getPropretyValue());
  }
  Session sess = Session.getInstance(props);
  Message msg = new MimeMessage(sess);
  msg.setFrom(new InternetAddress(pMsg.getEmailConnect().getUserEmail()));
  if (pMsg.getErecipients().size() == 1) {
    msg.setRecipient(Message.RecipientType.TO,
      new InternetAddress(pMsg.getErecipients().get(0).getItsEmail()));
  } else {
    InternetAddress[] address =
      new InternetAddress[pMsg.getErecipients().size()];
    for (int i = 0; i < pMsg.getErecipients().size(); i++) {
      address[i] = new InternetAddress(pMsg.getErecipients().get(i)
        .getItsEmail());
    }
    msg.setRecipients(Message.RecipientType.TO, address);
  }
  msg.setSubject(pMsg.getEsubject());
  if (pMsg.getEattachments().size() > 0) {
    MimeBodyPart mbpt = new MimeBodyPart();
    mbpt.setText(pMsg.getEtext());
    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbpt);
    for (Eattachment attch : pMsg.getEattachments()) {
      MimeBodyPart mbp = new MimeBodyPart();
      mbp.attachFile(attch.getItsPath());
      mp.addBodyPart(mbp);
    }
    msg.setContent(mp);
  } else {
    msg.setText(pMsg.getEtext());
  }
  msg.setSentDate(new Date());
  Transport.send(msg, pMsg.getEmailConnect().getUserEmail(),
    pMsg.getEmailConnect().getUserPassword());
}
 
开发者ID:demidenko05,项目名称:beige-software,代码行数:53,代码来源:MailSenderStd.java

示例9: sendEmailWithAttachments

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
public static void sendEmailWithAttachments(String host, String port, final String userName, final String password, String toAddress,
		String subject, String message, String[] attachFiles) throws AddressException, MessagingException {
	// sets SMTP server properties
	Properties properties = new Properties();
	properties.put("mail.smtp.host", host);
	properties.put("mail.smtp.port", port);
	properties.put("mail.smtp.auth", "true");
	properties.put("mail.smtp.starttls.enable", "true");
	properties.put("mail.user", userName);
	properties.put("mail.password", password);

	// creates a new session with an authenticator
	Authenticator auth = new Authenticator() {
		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(userName, password);
		}
	};
	Session session = Session.getInstance(properties, auth);

	// creates a new e-mail message
	Message msg = new MimeMessage(session);

	msg.setFrom(new InternetAddress(userName));
	InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
	msg.setRecipients(Message.RecipientType.TO, toAddresses);
	msg.setSubject(subject);
	msg.setSentDate(new Date());

	// creates message part
	MimeBodyPart messageBodyPart = new MimeBodyPart();
	messageBodyPart.setContent(message, "text/html");

	// creates multi-part
	Multipart multipart = new MimeMultipart();
	multipart.addBodyPart(messageBodyPart);

	// adds attachments
	if (attachFiles != null && attachFiles.length > 0) {
		for (String filePath : attachFiles) {
			MimeBodyPart attachPart = new MimeBodyPart();

			try {
				attachPart.attachFile(filePath);
			} catch (IOException ex) {
				ex.printStackTrace();
			}

			multipart.addBodyPart(attachPart);
		}
	}

	// sets the multi-part as e-mail's content
	msg.setContent(multipart);

	// sends the e-mail
	Transport.send(msg);

}
 
开发者ID:callakrsos,项目名称:Gargoyle,代码行数:59,代码来源:EmailAttachmentSender.java

示例10: sendEmailWithAttachment

import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
/**
 * Sends an e-mail message from a SMTP host with a list of attached files.
 * 
 * @param subject Mail subject
 * @param message Mail content
 * @param attachedFiles Attached files
 */
public static void sendEmailWithAttachment(String subject, String message, List<File> attachedFiles)
        throws MessagingException {
    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", ApplicationUtils.getSmtpHost());
    properties.put("mail.smtp.port", ApplicationUtils.getSmtpPort());
    properties.put("mail.smtp.auth", ApplicationUtils.getSmtpAuth());
    properties.put("mail.smtp.starttls.enable", ApplicationUtils.getSmtpStarttlsEnable());
    properties.put("mail.user", ApplicationUtils.getSmtpUser());
    properties.put("mail.password", ApplicationUtils.getSmtpPass());
 
    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(ApplicationUtils.getSmtpUser(), ApplicationUtils.getSmtpPass());
        }
    };
    Session session = Session.getInstance(properties, auth);
 
    // creates a new e-mail message
    Message msg = new MimeMessage(session);
 
    msg.setFrom(new InternetAddress(ApplicationUtils.getSmtpUser()));
    InternetAddress[] toAddresses = { new InternetAddress(ApplicationUtils.getAdminAddress()) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    ((MimeMessage)msg).setSubject(subject,"UTF-8");
    msg.setSentDate(new Date());
    msg.setHeader("Content-Transfer-Encoding", "7bit"); 
 
    // creates message part
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(message, "text/html;charset=UTF-8");
 
    // creates multi-part
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
 
    // adds attachments
    if (attachedFiles != null && !attachedFiles.isEmpty()) {
        for (File aFile : attachedFiles) {
            MimeBodyPart attachPart = new MimeBodyPart();
             try {
                attachPart.attachFile(aFile);
            } catch (IOException e) {
                log.error("IOException occurs: ", e);
            }
             multipart.addBodyPart(attachPart);
        }
    }
 
    // sets the multi-part as e-mail's content
    msg.setContent(multipart);
 
    // sends the e-mail
    Transport.send(msg);
}
 
开发者ID:k-tamura,项目名称:easybuggy,代码行数:65,代码来源:EmailUtils.java


注:本文中的javax.mail.internet.MimeBodyPart.attachFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。