javax.mail.internet.MimeMessage.getRecipients()方法的使用及代码示例

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

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

MimeMessage.getRecipients介绍

[英]Returns the recepients specified by the type. The mapping between the type and the corresponding RFC 822 header is as follows:

Message.RecipientType.TO		"To" 
Message.RecipientType.CC		"Cc" 
Message.RecipientType.BCC		"Bcc" 
MimeMessage.RecipientType.NEWSGROUPS	"Newsgroups"

Returns null if the header specified by the type is not found or if its value is empty.

This implementation uses the getHeader method to obtain the requisite header field.
[中]返回由类型指定的接收项。类型和相应的RFC 822头之间的映射如下:

Message.RecipientType.TO		"To" 
Message.RecipientType.CC		"Cc" 
Message.RecipientType.BCC		"Bcc" 
MimeMessage.RecipientType.NEWSGROUPS	"Newsgroups"

如果找不到类型指定的标头或其值为空,则返回null。
此实现使用getHeader方法来获取所需的头字段。

代码示例

代码示例来源:origin: cloudfoundry/uaa

public List<Address> getRecipients(Message.RecipientType recipientType) throws MessagingException {
  return Arrays.asList(mimeMessage.getRecipients(recipientType));
}

代码示例来源:origin: igniterealtime/Openfire

message.getRecipients(MimeMessage.RecipientType.TO));

代码示例来源:origin: apache/nifi

putAddressListInAttributes(attributes, EMAIL_HEADER_TO, originalMessage.getRecipients(Message.RecipientType.TO));
putAddressListInAttributes(attributes, EMAIL_HEADER_CC, originalMessage.getRecipients(Message.RecipientType.CC));
putAddressListInAttributes(attributes, EMAIL_HEADER_BCC, originalMessage.getRecipients(Message.RecipientType.BCC));

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

public ValidationBean send(String subject, String body, String to) {
  Transport transport = null;
  try {
    LOGGER.debug("Sending email [{}] to [{}]", subject, to);
    Properties props = mailProperties();
    MailSession session = MailSession.getInstance().createWith(props, username, password);
    transport = session.getTransport();
    transport.connect(host, port, nullIfEmpty(username), nullIfEmpty(password));
    MimeMessage msg = session.createMessage(from, to, subject, body);
    transport.sendMessage(msg, msg.getRecipients(TO));
    return ValidationBean.valid();
  } catch (Exception e) {
    LOGGER.error("Sending failed for email [{}] to [{}]", subject, to, e);
    return ValidationBean.notValid(ERROR_MESSAGE);
  } finally {
    if (transport != null) {
      try {
        transport.close();
      } catch (MessagingException e) {
        LOGGER.error("Failed to close transport", e);
      }
    }
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testMessageFactorySetRecipients() throws MessagingException {
  final MimeMessageBuilder builder = new MimeMessageBuilder(null);
  final String addresses = "testing1@example.com,testing2@example.com";
  assertNull(builder.build().getRecipients(
      Message.RecipientType.TO));
  builder.setRecipients(Message.RecipientType.TO, null);
  assertNull(builder.build().getRecipients(
      Message.RecipientType.TO));
  builder.setRecipients(Message.RecipientType.TO, addresses);
  assertArrayEquals(InternetAddress.parse(addresses), builder
      .build().getRecipients(Message.RecipientType.TO));
}

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

@Test
public void javaMailSenderWithSimpleMessages() throws MessagingException {
  MockJavaMailSender sender = new MockJavaMailSender();
  sender.setHost("host");
  sender.setUsername("username");
  sender.setPassword("password");
  SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
  simpleMessage1.setTo("he@mail.org");
  SimpleMailMessage simpleMessage2 = new SimpleMailMessage();
  simpleMessage2.setTo("she@mail.org");
  sender.send(simpleMessage1, simpleMessage2);
  assertEquals("host", sender.transport.getConnectedHost());
  assertEquals("username", sender.transport.getConnectedUsername());
  assertEquals("password", sender.transport.getConnectedPassword());
  assertTrue(sender.transport.isCloseCalled());
  assertEquals(2, sender.transport.getSentMessages().size());
  MimeMessage sentMessage1 = sender.transport.getSentMessage(0);
  List<Address> tos1 = Arrays.asList(sentMessage1.getRecipients(Message.RecipientType.TO));
  assertEquals(1, tos1.size());
  assertEquals("he@mail.org", ((InternetAddress) tos1.get(0)).getAddress());
  MimeMessage sentMessage2 = sender.transport.getSentMessage(1);
  List<Address> tos2 = Arrays.asList(sentMessage2.getRecipients(Message.RecipientType.TO));
  assertEquals(1, tos2.size());
  assertEquals("she@mail.org", ((InternetAddress) tos2.get(0)).getAddress());
}

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

List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
assertEquals("reply@mail.org", ((InternetAddress) replyTos.get(0)).getAddress());
List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
assertEquals(1, tos.size());
assertEquals("you@mail.org", ((InternetAddress) tos.get(0)).getAddress());
List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
assertEquals(2, ccs.size());
assertEquals("he@mail.org", ((InternetAddress) ccs.get(0)).getAddress());
assertEquals("she@mail.org", ((InternetAddress) ccs.get(1)).getAddress());
List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
assertEquals(2, bccs.size());
assertEquals("us@mail.org", ((InternetAddress) bccs.get(0)).getAddress());

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * @return the 'to' recipients of the message
 * @throws Exception determining the recipients failed
 */
public List<javax.mail.Address> getTo() throws Exception
{
  final javax.mail.Address[] recipients = this.mimeMessage.getRecipients(Message.RecipientType.TO);
  return recipients != null ? Arrays.asList(recipients) : new ArrayList<javax.mail.Address>();
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * @return the 'bcc' recipients of the message
 * @throws Exception determining the recipients failed
 */
public List<javax.mail.Address> getBcc() throws Exception
{
  final javax.mail.Address[] recipients = this.mimeMessage.getRecipients(Message.RecipientType.BCC);
  return recipients != null ? Arrays.asList(recipients) : new ArrayList<javax.mail.Address>();
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * @return the 'cc' recipients of the message
 * @throws Exception determining the recipients failed
 */
public List<javax.mail.Address> getCc() throws Exception
{
  final javax.mail.Address[] recipients = this.mimeMessage.getRecipients(Message.RecipientType.CC);
  return recipients != null ? Arrays.asList(recipients) : new ArrayList<javax.mail.Address>();
}

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

assertEquals("replyTo@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress());
final Address[] recipients = msg.getRecipients(RecipientType.TO);
assertNotNull(recipients);
assertTrue(recipients.length == 2);

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

assertEquals("replyTo@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress());
final Address[] recipients = msg.getRecipients(RecipientType.TO);
assertNotNull(recipients);
assertTrue(recipients.length == 2);

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * Get all the recipient addresses for the message.
 * Extracts the TO, CC, BCC, and NEWSGROUPS recipients.
 *
 * @return          array of Address objects
 * @exception       MessagingException for failures
 * @see        javax.mail.Message.RecipientType#TO
 * @see        javax.mail.Message.RecipientType#CC
 * @see        javax.mail.Message.RecipientType#BCC
 * @see        javax.mail.internet.MimeMessage.RecipientType#NEWSGROUPS
 */
public Address[] getAllRecipients() throws MessagingException {
Address[] all = super.getAllRecipients();
Address[] ng = getRecipients(RecipientType.NEWSGROUPS);
if (ng == null)
  return all;		// the common case
if (all == null)
  return ng;		// a rare case
Address[] addresses = new Address[all.length + ng.length];
System.arraycopy(all, 0, addresses, 0, all.length);
System.arraycopy(ng, 0, addresses, all.length, ng.length);
return addresses;
}

代码示例来源:origin: com.sun.mail/javax.mail

/**
 * Get all the recipient addresses for the message.
 * Extracts the TO, CC, BCC, and NEWSGROUPS recipients.
 *
 * @return          array of Address objects
 * @exception       MessagingException for failures
 * @see        javax.mail.Message.RecipientType#TO
 * @see        javax.mail.Message.RecipientType#CC
 * @see        javax.mail.Message.RecipientType#BCC
 * @see        javax.mail.internet.MimeMessage.RecipientType#NEWSGROUPS
 */
@Override
public Address[] getAllRecipients() throws MessagingException {
Address[] all = super.getAllRecipients();
Address[] ng = getRecipients(RecipientType.NEWSGROUPS);
if (ng == null)
  return all;		// the common case
if (all == null)
  return ng;		// a rare case
Address[] addresses = new Address[all.length + ng.length];
System.arraycopy(all, 0, addresses, 0, all.length);
System.arraycopy(ng, 0, addresses, all.length, ng.length);
return addresses;
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * Get the desired Recipient type.
 */
public Address[] getRecipients(Message.RecipientType type)
      throws MessagingException {
checkExpunged();
if (bodyLoaded)
  return super.getRecipients(type);
loadEnvelope();
if (type == Message.RecipientType.TO)
  return aaclone(envelope.to);
else if (type == Message.RecipientType.CC)
  return aaclone(envelope.cc);
else if (type == Message.RecipientType.BCC)
  return aaclone(envelope.bcc);
else
  return super.getRecipients(type);
}

代码示例来源:origin: com.sun.mail/javax.mail

/**
 * Get the desired Recipient type.
 */
@Override
public Address[] getRecipients(Message.RecipientType type)
      throws MessagingException {
checkExpunged();
if (bodyLoaded)
  return super.getRecipients(type);
loadEnvelope();
if (type == Message.RecipientType.TO)
  return aaclone(envelope.to);
else if (type == Message.RecipientType.CC)
  return aaclone(envelope.cc);
else if (type == Message.RecipientType.BCC)
  return aaclone(envelope.bcc);
else
  return super.getRecipients(type);
}

代码示例来源:origin: TEAMMATES/teammates

@Test
public void testConvertToMimeMessage() throws Exception {
  EmailWrapper wrapper = getTypicalEmailWrapper();
  MimeMessage email = new JavamailService().parseToEmail(wrapper);
  assertEquals(new InternetAddress(wrapper.getSenderEmail(), wrapper.getSenderName()), email.getFrom()[0]);
  assertEquals(new InternetAddress(wrapper.getReplyTo()), email.getReplyTo()[0]);
  assertEquals(new InternetAddress(wrapper.getRecipient()), email.getRecipients(Message.RecipientType.TO)[0]);
  assertEquals(new InternetAddress(wrapper.getBcc()), email.getRecipients(Message.RecipientType.BCC)[0]);
  assertEquals(wrapper.getSubject(), email.getSubject());
  assertEquals(wrapper.getContent(), email.getContent().toString());
}

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

@Test
public void testImap() throws Exception {
  Message<?> message = this.imapChannel.receive(10000);
  assertNotNull(message);
  MimeMessage mm = (MimeMessage) message.getPayload();
  assertEquals("Foo <foo@bar>", mm.getRecipients(RecipientType.TO)[0].toString());
  assertEquals("Bar <bar@baz>", mm.getFrom()[0].toString());
  assertEquals("Test Email", mm.getSubject());
  assertThat(mm.getContent(), equalTo(TestMailServer.MailServer.MailHandler.BODY + "\r\n"));
}

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

@Test
public void byteArrayMessage() throws Exception {
  byte[] payload = {1, 2, 3};
  org.springframework.messaging.Message<byte[]> message =
      MessageBuilder.withPayload(payload)
      .setHeader(MailHeaders.ATTACHMENT_FILENAME, "attachment.txt")
      .setHeader(MailHeaders.TO, MailTestsHelper.TO)
      .build();
  this.handler.handleMessage(message);
  byte[] buffer = new byte[1024];
  MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0);
  assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart);
  int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer);
  assertEquals("buffer size does not match", payload.length, size);
  byte[] messageContent = new byte[size];
  System.arraycopy(buffer, 0, messageContent, 0, payload.length);
  assertArrayEquals("buffer content does not match", payload, messageContent);
  assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
}

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

@Test
public void byteArrayMessage() throws Exception {
  byte[] payload = {1, 2, 3};
  org.springframework.messaging.Message<?> message =
      MessageBuilder.withPayload(payload)
      .setHeader(MailHeaders.ATTACHMENT_FILENAME, "attachment.txt")
      .setHeader(MailHeaders.TO, MailTestsHelper.TO)
      .build();
  this.handler.handleMessage(message);
  assertEquals("no mime message should have been sent",
      1, this.mailSender.getSentMimeMessages().size());
  assertEquals("only one simple message must be sent",
      0, this.mailSender.getSentSimpleMailMessages().size());
  byte[] buffer = new byte[1024];
  MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0);
  assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart);
  int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer);
  assertEquals("buffer size does not match", payload.length, size);
  byte[] messageContent = new byte[size];
  System.arraycopy(buffer, 0, messageContent, 0, payload.length);
  assertArrayEquals("buffer content does not match", payload, messageContent);
  assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
}

相关文章

MimeMessage类方法