use of javax.mail.internet.MimeBodyPart in project disconf by knightliao.
the class SimpleMailSender method sendHtmlMail.
/**
* 以HTML格式发送邮件
*
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
try {
LOG.info("send to " + mailInfo.getFromAddress());
// 设置一些通用的数据
Message mailMessage = setCommon(mailInfo);
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
LOG.info("send to " + mailInfo.getFromAddress() + " dnoe.");
return true;
} catch (MessagingException ex) {
LOG.error(ex.toString(), ex);
}
return false;
}
use of javax.mail.internet.MimeBodyPart in project rest.li by linkedin.
the class TestMIMEReaderDrain method testSinglePartialTopRemaining.
@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSinglePartialTopRemaining(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
//Execute the request, verify the correct header came back to ensure the server took the proper drain actions
//and return the payload so we can assert deeper.
executeRequestWithDrainStrategy(chunkSize, bodyPartList, SINGLE_PARTIAL_TOP_REMAINING, "onDrainComplete");
//Single part drains the first 6 then the top level drains all of remaining
List<SinglePartMIMEDrainReaderCallbackImpl> singlePartMIMEReaderCallbacks = _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks();
Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 6);
for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
//Actual
final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
//Expected
final BodyPart currentExpectedPart = _currentMimeMultipartBody.getBodyPart(i);
//Construct expected headers and verify they match
final Map<String, String> expectedHeaders = new HashMap<String, String>();
@SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
while (allHeaders.hasMoreElements()) {
final Header header = allHeaders.nextElement();
expectedHeaders.put(header.getName(), header.getValue());
}
Assert.assertEquals(currentCallback.getHeaders(), expectedHeaders);
//Verify that the bodies are empty
Assert.assertNull(currentCallback.getFinishedData());
}
}
use of javax.mail.internet.MimeBodyPart in project rest.li by linkedin.
the class TestMIMEReaderDrain method testSingleAll.
@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSingleAll(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
//Execute the request, verify the correct header came back to ensure the server took the proper drain actions
//and return the payload so we can assert deeper.
executeRequestWithDrainStrategy(chunkSize, bodyPartList, SINGLE_ALL, "onFinished");
//Single part drains all, one by one
List<SinglePartMIMEDrainReaderCallbackImpl> singlePartMIMEReaderCallbacks = _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks();
Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 12);
//Verify everything was drained
for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
//Actual
final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
//Expected
final BodyPart currentExpectedPart = _currentMimeMultipartBody.getBodyPart(i);
//Construct expected headers and verify they match
final Map<String, String> expectedHeaders = new HashMap<String, String>();
@SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
while (allHeaders.hasMoreElements()) {
final Header header = allHeaders.nextElement();
expectedHeaders.put(header.getName(), header.getValue());
}
Assert.assertEquals(currentCallback.getHeaders(), expectedHeaders);
//Verify that the bodies are empty
Assert.assertNull(currentCallback.getFinishedData());
}
}
use of javax.mail.internet.MimeBodyPart in project rest.li by linkedin.
the class TestMIMEReaderDrain method testSingleAlternate.
@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSingleAlternate(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
//Execute the request, verify the correct header came back to ensure the server took the proper drain actions
//and return the payload so we can assert deeper.
executeRequestWithDrainStrategy(chunkSize, bodyPartList, SINGLE_ALTERNATE, "onFinished");
//Single part alternates between consumption and draining for all 12 parts.
//This means that parts 0, 2, 4, etc.. will be consumed and parts 1, 3, 5, etc... will be drained.
List<SinglePartMIMEDrainReaderCallbackImpl> singlePartMIMEReaderCallbacks = _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks();
Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 12);
//First the consumed
for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i = i + 2) {
//Actual
final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
//Expected
final BodyPart currentExpectedPart = _currentMimeMultipartBody.getBodyPart(i);
//Construct expected headers and verify they match
final Map<String, String> expectedHeaders = new HashMap<String, String>();
@SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
while (allHeaders.hasMoreElements()) {
final Header header = allHeaders.nextElement();
expectedHeaders.put(header.getName(), header.getValue());
}
Assert.assertEquals(currentCallback.getHeaders(), expectedHeaders);
//Verify the body matches
if (currentExpectedPart.getContent() instanceof byte[]) {
Assert.assertEquals(currentCallback.getFinishedData().copyBytes(), currentExpectedPart.getContent());
} else {
//Default is String
Assert.assertEquals(new String(currentCallback.getFinishedData().copyBytes()), currentExpectedPart.getContent());
}
}
//Then the drained
for (int i = 1; i < singlePartMIMEReaderCallbacks.size(); i = i + 2) {
//Actual
final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
//Expected
final BodyPart currentExpectedPart = _currentMimeMultipartBody.getBodyPart(i);
//Construct expected headers and verify they match
final Map<String, String> expectedHeaders = new HashMap<String, String>();
@SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
while (allHeaders.hasMoreElements()) {
final Header header = allHeaders.nextElement();
expectedHeaders.put(header.getName(), header.getValue());
}
Assert.assertEquals(currentCallback.getHeaders(), expectedHeaders);
//Verify that the bodies are empty
Assert.assertNull(currentCallback.getFinishedData());
}
}
use of javax.mail.internet.MimeBodyPart in project rest.li by linkedin.
the class TestMIMEReaderExceptions method incorrectHeaderStart.
@Test(dataProvider = "multiplePartsDataSource")
public void incorrectHeaderStart(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
//Use Javax to create a multipart payload. Then we just modify the location of the consecutive CRLFs.
MimeMultipart multiPartMimeBody = new MimeMultipart();
//Add your body parts
for (final MimeBodyPart bodyPart : bodyPartList) {
multiPartMimeBody.addBodyPart(bodyPart);
}
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
multiPartMimeBody.writeTo(byteArrayOutputStream);
final byte[] mimePayload = byteArrayOutputStream.toByteArray();
//but Javax mail doesn't do this.
for (int i = 0; i < mimePayload.length - 2; i++) {
final byte[] currentWindow = Arrays.copyOfRange(mimePayload, i, i + 2);
if (Arrays.equals(currentWindow, MultiPartMIMEUtils.CRLF_BYTES)) {
mimePayload[i] = 15;
break;
}
}
final ByteString requestPayload = ByteString.copy(mimePayload);
executeRequestWithDesiredException(requestPayload, chunkSize, multiPartMimeBody.getContentType(), "Malformed multipart mime request. Headers are improperly constructed.");
//No single part readers should have been created.
Assert.assertEquals(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().size(), 0);
}
Aggregations