當前位置: 首頁>>代碼示例>>Java>>正文


Java ByteArrayDataSource類代碼示例

本文整理匯總了Java中org.apache.commons.mail.ByteArrayDataSource的典型用法代碼示例。如果您正苦於以下問題:Java ByteArrayDataSource類的具體用法?Java ByteArrayDataSource怎麽用?Java ByteArrayDataSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ByteArrayDataSource類屬於org.apache.commons.mail包,在下文中一共展示了ByteArrayDataSource類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: fillEmail

import org.apache.commons.mail.ByteArrayDataSource; //導入依賴的package包/類
/**
 * Fill email.
 *
 * @param email
 *            the email
 * @throws EmailException
 *             the email exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public void fillEmail(final MultiPartEmail email) throws EmailException, IOException {
	email.setHostName(getHost());
	email.setSmtpPort(getSmtpPort());
	
	email.addTo(getTo());
	email.setFrom(getFrom());
	email.setSubject(getSubject());
	email.setMsg(getMsg());
	email.setSSLOnConnect(isSecured());
	if (isRequiresAuthentication()) {
		email.setAuthentication(getUsername(), getPassword());
	}
	for (int i = 0; i < this.attachements.size(); i++) {
		final Attachment attachment = this.attachements.get(i);
		final ByteArrayDataSource ds = new ByteArrayDataSource(attachment.getData(), attachment.getMimeType());
		email.attach(ds, attachment.getName(), attachment.getDescription());
	}
}
 
開發者ID:kiswanij,項目名稱:jk-util,代碼行數:29,代碼來源:MailInfo.java

示例2: testSendEmailAttachment

import org.apache.commons.mail.ByteArrayDataSource; //導入依賴的package包/類
@Test
public final void testSendEmailAttachment() throws Exception {

    final String expectedMessage = "This is just a message";
    final String expectedSenderName = "John Smith";
    final String expectedSenderEmailAddress = "[email protected]";
    String attachment = "This is a attachment.";
    String attachmentName = "attachment.txt";
    //Subject is provided inside the HtmlTemplate directly
    final String expectedSubject = "Greetings";

    final Map<String, String> params = new HashMap<String, String>();
    params.put("message", expectedMessage);
    params.put("senderName", expectedSenderName);
    params.put("senderEmailAddress", expectedSenderEmailAddress);

    final String recipient =  "[email protected]";

    Map<String, DataSource> attachments = new HashMap();
    attachments.put(attachmentName, new ByteArrayDataSource(attachment, "text/plain"));

    ArgumentCaptor<HtmlEmail> captor = ArgumentCaptor.forClass(HtmlEmail.class);

    List<String> failureList = emailService.sendEmail(emailTemplateAttachmentPath, params, attachments, recipient);

    verify(messageGatewayHtmlEmail, times(1)).send(captor.capture());

    assertEquals(expectedSenderEmailAddress, captor.getValue().getFromAddress().getAddress());
    assertEquals(expectedSenderName, captor.getValue().getFromAddress().getPersonal());
    assertEquals(expectedSubject, captor.getValue().getSubject());
    assertEquals(recipient, captor.getValue().getToAddresses().get(0).toString());
    Method getContainer = captor.getValue().getClass().getSuperclass().getDeclaredMethod("getContainer");
    getContainer.setAccessible(true);
    MimeMultipart mimeMultipart = (MimeMultipart) getContainer.invoke(captor.getValue());
    getContainer.setAccessible(false);
    assertEquals(attachment, mimeMultipart.getBodyPart(0).getContent().toString());

    //If email is sent to the recipient successfully, the response is an empty failureList
    assertTrue(failureList.isEmpty());
}
 
開發者ID:Adobe-Consulting-Services,項目名稱:acs-aem-commons,代碼行數:41,代碼來源:EmailServiceImplTest.java


注:本文中的org.apache.commons.mail.ByteArrayDataSource類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。