javax.mail.Multipart类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(13.6k)|赞(0)|评价(0)|浏览(308)

本文整理了Java中javax.mail.Multipart类的一些代码示例,展示了Multipart类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Multipart类的具体详情如下:
包路径:javax.mail.Multipart
类名称:Multipart

Multipart介绍

[英]Multipart is a container that holds multiple body parts. Multipart provides methods to retrieve and set its subparts.

Multipart also acts as the base class for the content object returned by most Multipart DataContentHandlers. For example, invoking getContent() on a DataHandler whose source is a "multipart/signed" data source may return an appropriate subclass of Multipart.

Some messaging systems provide different subtypes of Multiparts. For example, MIME specifies a set of subtypes that include "alternative", "mixed", "related", "parallel", "signed", etc.

Multipart is an abstract class. Subclasses provide actual implementations.
[中]Multipart是一个容纳多个身体部位的容器。Multipart提供了检索和设置其子部分的方法。
Multipart还充当大多数多部分DataContentHandler返回的内容对象的基类。例如,在源为“多部分/签名”数据源的DataHandler上调用getContent()可能会返回相应的多部分子类。
一些消息传递系统提供不同的多部分子类型。例如,MIME指定了一组子类型,包括“alternative”、“mixed”、“related”、“parallel”、“signed”等。
Multipart是一个抽象类。子类提供了实际的实现。

代码示例

代码示例来源:origin: stackoverflow.com

Multipart multipart = new MimeMultipart("mixed");
for (String str : attachment_PathList) {
  MimeBodyPart messageBodyPart = new MimeBodyPart();
  DataSource source = new FileDataSource(str);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(source.getName());
  multipart.addBodyPart(messageBodyPart);
}
msg.setContent(multipart);
Transport.send(msg);

代码示例来源:origin: oblac/jodd

/**
 * Process the {@link Multipart}.
 *
 * @param mp {@link Multipart}
 * @throws MessagingException if there is a failure.
 * @throws IOException        if there is an issue with the {@link Multipart}.
 */
private void processMultipart(final Multipart mp) throws MessagingException, IOException {
  final int count = mp.getCount();
  for (int i = 0; i < count; i++) {
    final Part innerPart = mp.getBodyPart(i);
    processPart(innerPart);
  }
}

代码示例来源:origin: stackoverflow.com

List<File> attachments = new ArrayList<File>();
for (Message message : temp) {
  Multipart multipart = (Multipart) message.getContent();
  // System.out.println(multipart.getCount());

  for (int i = 0; i < multipart.getCount(); i++) {
    BodyPart bodyPart = multipart.getBodyPart(i);
    if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) &&
        !StringUtils.isNotBlank(bodyPart.getFileName())) {
      continue; // dealing with attachments only
    } 
    InputStream is = bodyPart.getInputStream();
    File f = new File("/tmp/" + bodyPart.getFileName());
    FileOutputStream fos = new FileOutputStream(f);
    byte[] buf = new byte[4096];
    int bytesRead;
    while((bytesRead = is.read(buf))!=-1) {
      fos.write(buf, 0, bytesRead);
    }
    fos.close();
    attachments.add(f);
  }
}

代码示例来源:origin: spring-projects/spring-integration-samples

count = multipart.getCount();
  bp = multipart.getBodyPart(i);
  contentType = bp.getContentType();
  filename    = bp.getFileName();
  disposition = bp.getDisposition();
  subject     = mailMessage.getSubject();
    filename = ((MimeBodyPart) bp).getContentID();

代码示例来源:origin: blynkkk/blynk-server

@Override
public void sendHtmlWithAttachment(String to, String subj, String body, QrHolder[] attachments) throws Exception {
  MimeMessage message = new MimeMessage(session);
  message.setFrom(from);
  message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  message.setSubject(subj, "UTF-8");
  Multipart multipart = new MimeMultipart();
  MimeBodyPart bodyMessagePart = new MimeBodyPart();
  bodyMessagePart.setContent(body, TEXT_HTML_CHARSET_UTF_8);
  multipart.addBodyPart(bodyMessagePart);
  for (QrHolder qrHolder : attachments) {
    MimeBodyPart attachmentsPart = new MimeBodyPart();
    attachmentsPart.setDataHandler(new DataHandler(new ByteArrayDataSource(qrHolder.data, "image/jpeg")));
    attachmentsPart.setFileName(qrHolder.makeQRFilename());
    multipart.addBodyPart(attachmentsPart);
  }
  message.setContent(multipart);
  try (Transport transport = session.getTransport()) {
    transport.connect(host, username, password);
    transport.sendMessage(message, message.getAllRecipients());
  }
  log.debug("Mail sent to {}. Subj: {}", to, subj);
  log.trace("Mail body: {}", body);
}

代码示例来源:origin: 0opslab/opslabJutil

try {
  InternetAddress from = new InternetAddress(sender_username);
  message.setFrom(from);
  InternetAddress to = new InternetAddress(receiveUser);
  message.setRecipient(Message.RecipientType.TO, to);
  message.setSubject(subject);
  Multipart multipart = new MimeMultipart();
  BodyPart contentPart = new MimeBodyPart();
  contentPart.setContent(sendHtml, "text/html;charset=UTF-8");
  multipart.addBodyPart(contentPart);
    BodyPart attachmentBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(attachment);
    attachmentBodyPart.setDataHandler(new DataHandler(source));
    attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
    multipart.addBodyPart(attachmentBodyPart);

代码示例来源:origin: networknt/light-4j

Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailConfg.getUser()));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(content);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

代码示例来源:origin: traccar/traccar

MimeMessage message = new MimeMessage(session);
  message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setSubject(subject);
message.setSentDate(new Date());
  Multipart multipart = new MimeMultipart();
  BodyPart messageBodyPart = new MimeBodyPart();
  messageBodyPart.setContent(body, "text/html; charset=utf-8");
  multipart.addBodyPart(messageBodyPart);
  multipart.addBodyPart(attachment);
  message.setContent(multipart);

代码示例来源:origin: stackoverflow.com

message.setFrom(new InternetAddress(from));
String[] emails = { recipient };
InternetAddress dests[] = new InternetAddress[emails.length];
for (int i = 0; i < emails.length; i++) {
  dests[i] = new InternetAddress(emails[i].trim().toLowerCase());
message.setRecipients(Message.RecipientType.TO, dests);
message.setSubject(subject, "UTF-8");
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(body, "text/html;charset=utf-8");
mp.addBodyPart(mbp);
message.setContent(mp);
message.setSentDate(new java.util.Date());

代码示例来源:origin: stackoverflow.com

private Multipart _multipart; 
_multipart = new MimeMultipart(); 

public void addAttachment(String filename,String subject) throws Exception { 
  BodyPart messageBodyPart = new MimeBodyPart(); 
  DataSource source = new FileDataSource(filename); 
  messageBodyPart.setDataHandler(new DataHandler(source)); 
  messageBodyPart.setFileName(filename); 
  _multipart.addBodyPart(messageBodyPart);

  BodyPart messageBodyPart2 = new MimeBodyPart(); 
  messageBodyPart2.setText(subject); 

  _multipart.addBodyPart(messageBodyPart2); 
} 


message.setContent(_multipart);

代码示例来源:origin: blynkkk/blynk-server

@Override
public void sendHtmlWithAttachment(String to, String subj, String body,
                  QrHolder[] attachmentData) throws Exception {
  MimeMessage message = new MimeMessage(session);
  message.setFrom(from);
  message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  message.setSubject(subj, "UTF-8");
  Multipart multipart = new MimeMultipart();
  MimeBodyPart bodyMessagePart = new MimeBodyPart();
  bodyMessagePart.setContent(body, TEXT_HTML_CHARSET_UTF_8);
  multipart.addBodyPart(bodyMessagePart);
  attachQRs(multipart, attachmentData);
  attachCSV(multipart, attachmentData);
  message.setContent(multipart);
  Transport.send(message);
  log.trace("Mail to {} was sent. Subj : {}, body : {}", to, subj, body);
}

代码示例来源:origin: crashub/crash

MimeMessage message = new MimeMessage(session);
 message.setFrom(new InternetAddress(smtpFrom));
message.setRecipients(Message.RecipientType.TO, addresses);
if (subject != null) {
 message.setSubject(subject);
 Multipart multipart = new MimeMultipart();
 bodyPart = new MimeBodyPart();
 bodyPart.setContent(body, type);
 for (DataSource attachment : attachments) {
  MimeBodyPart attachmentPart = new MimeBodyPart();
  attachmentPart.setDataHandler(new DataHandler(attachment));
  attachmentPart.setFileName(attachment.getName());
  multipart.addBodyPart(attachmentPart);
 message.setContent(multipart);

代码示例来源:origin: SeanDragon/protools

MimeMessage message = new MimeMessage(session);
  Address[] addresses = new Address[toList.size()];
  for (int i = 0; i < toList.size(); i++) {
    addresses[i] = new InternetAddress(toList.get(i));
  message.setRecipients(Message.RecipientType.TO, addresses);
message.setSubject(mailSend.getSubject());
message.setSentDate(new DatePlus().toDate());
Multipart multipart = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(mailSend.getContent(), StrConst.DEFAULT_CHARSET_NAME);
multipart.addBodyPart(mbp, 0);
    mbp = new MimeBodyPart();
    mbp.setDataHandler(new DataHandler(fds));
    mbp.setFileName(filename);
    multipart.addBodyPart(mbp);

代码示例来源:origin: shopizer-ecommerce/shopizer

mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
InternetAddress inetAddress = new InternetAddress();
inetAddress.setPersonal(eml);
inetAddress.setAddress(from);
mimeMessage.setFrom(inetAddress);
mimeMessage.setSubject(subject);
Multipart mp = new MimeMultipart("alternative");
BodyPart textPart = new MimeBodyPart();
freemarkerMailConfiguration.setClassForTemplateLoading(HtmlEmailSenderImpl.class, "/");
Template textTemplate = freemarkerMailConfiguration.getTemplate(new StringBuilder(TEMPLATE_PATH).append("").append("/").append(tmpl).toString());
      "Can't generate text mail", e);
mp.addBodyPart(textPart);
      "Can't generate HTML mail", e);
htmlContent.addBodyPart(htmlPage);
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlContent);
mp.addBodyPart(htmlPart);
mimeMessage.setContent(mp);

代码示例来源:origin: stackoverflow.com

message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject("JavaMail Attachment");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Here's the file");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);

代码示例来源:origin: kiegroup/jbpm

EmailNotificationHeader header = headers.get(lang);
Message msg = new MimeMessage(mailSession);
Set<String> toAddresses = new HashSet<String>();
for (User user : entry.getValue()) {
  if (emailAddress != null) {                        	
    if (toAddresses.add(emailAddress)) {
      msg.addRecipients( Message.RecipientType.TO, InternetAddress.parse( emailAddress, false));
  User user = TaskModelProvider.getFactory().newUser();
  ((InternalOrganizationalEntity) user).setId(header.getFrom());
  msg.setFrom( new InternetAddress(userInfo.getEmailForEntity(user)));
} else {
  msg.setFrom( new InternetAddress(mailSession.getProperty("mail.from")));
  Multipart multipart = new MimeMultipart();
  MimeBodyPart messageBodyPart = new MimeBodyPart();
  messageBodyPart.setDataHandler( new DataHandler( new ByteArrayDataSource( body, "text/html" ) ) );         
  multipart.addBodyPart(messageBodyPart);
    multipart.addBodyPart(attachementBodyPart);
  msg.setDataHandler( new DataHandler( new ByteArrayDataSource( body, "text/html" ) ) );

代码示例来源:origin: rakam-io/rakam

public void sendMail(List<String> toEmail, String title, String textContent, Optional<String> richText, Stream<MimeBodyPart> parts)
    throws MessagingException {
  Message msg = new MimeMessage(session);
  msg.setFrom(fromAddress);
  msg.addRecipients(MimeMessage.RecipientType.TO, toEmail.stream().map(e -> {
    try {
      return new InternetAddress(e);
    } catch (AddressException e1) {
      throw Throwables.propagate(e1);
    Multipart mp = new MimeMultipart();
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(richText.get(), "text/html");
    mp.addBodyPart(htmlPart);
    parts.forEach(part -> {
      try {
        mp.addBodyPart(part);
      } catch (MessagingException e) {
        throw Throwables.propagate(e);

代码示例来源:origin: javamelody/javamelody

assert subject != null;
assert message != null;
final InternetAddress[] toAddresses = InternetAddress.parse(toAddress, false);
final Message msg = new MimeMessage(getSession());
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
final MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(message);
final Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mbp);
if (attachments != null && !attachments.isEmpty()) {
  for (final File attachment : attachments) {
    final DataSource source = new FileDataSource(attachment);
    final MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(attachment.getName());
    multipart.addBodyPart(messageBodyPart);

代码示例来源:origin: stackoverflow.com

MimeMessage message = new MimeMessage(mailSession);
Multipart multipart = new MimeMultipart("alternative");
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(new String(messageHtml.getBytes("UTF8"),"ISO-8859-1"), "text/html");
multipart.addBodyPart(htmlPart);
message.setContent(multipart);
message.setFrom(new InternetAddress(from));
message.setSubject(subject, "UTF-8");

代码示例来源:origin: stackoverflow.com

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
for(String str : to){
  message.addRecipient(Message.RecipientType.TO, new InternetAddress(str));
}        
message.setSubject(subject);
// Create the message part 
MimeBodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText(messageBody,"UTF-8","html");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send(message);

相关文章