use of javax.mail.MessagingException in project ats-framework by Axway.
the class MimePackage method getAllStreams.
@PublicAtsApi
public List<InputStream> getAllStreams() throws PackageException {
boolean storeReconnected = false;
try {
// store should be opened for actions including getting InputStream.
storeReconnected = reconnectStoreIfClosed();
ArrayList<InputStream> streams = new ArrayList<InputStream>();
try {
for (MimePart part : parts) {
streams.add(part.getInputStream());
}
} finally {
closeStoreConnection(storeReconnected);
}
return streams;
} catch (MessagingException me) {
throw new PackageException("Could not read mime parts", me);
} catch (IOException ioe) {
throw new PackageException("Could not read mime parts", ioe);
}
}
use of javax.mail.MessagingException in project ats-framework by Axway.
the class MimePackage method setAttachmentFileName.
/**
* Set/modify attachment file name
*
* @param attachmentPartIndex index among attachment parts only
* @param fileName the file name. Add one or reset existing one
* @throws NoSuchMimePartException if not such attachment part is found
* @throws PackageException in case of other mail messaging exception
*/
@PublicAtsApi
public void setAttachmentFileName(int attachmentPartIndex, String fileName) throws PackageException {
// first check if there is part at this position at all
if (attachmentPartIndex >= attachmentPartIndices.size()) {
throw new NoSuchMimePartException("No attachment at position '" + attachmentPartIndex + "'");
}
try {
MimePart part = getPart(attachmentPartIndices.get(attachmentPartIndex));
// set the attachment file name
part.setFileName(fileName);
// must save now
this.message.saveChanges();
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of javax.mail.MessagingException in project ats-framework by Axway.
the class StringInMimePartRule method performMatch.
@Override
public boolean performMatch(MetaData metaData) throws RbvException {
boolean actualResult = false;
//get the emailMessage
//the meta data type check already passed, so it is safe to cast
MimePackage emailMessage = getNeededMimePackage(metaData);
InputStream actualPartDataStream = null;
BufferedInputStream bufferedStream = null;
try {
List<MimePart> partsToCheck = new ArrayList<MimePart>();
if (partIndex == PART_MAIN_MESSAGE) {
partsToCheck = emailMessage.getMimeParts();
} else {
partsToCheck.add(emailMessage.getPart(partIndex, isPartAttachment));
}
for (MimePart partToCheck : partsToCheck) {
Object partContent = partToCheck.getContent();
ContentType partContentType = new ContentType(partToCheck.getContentType());
//skip if no content
if (partContent == null) {
log.debug("MIME part does not have any content");
continue;
}
String partContentAsString;
if (partContent instanceof String) {
//directly read the content of the part
partContentAsString = (String) partContent;
} else if (partContent instanceof InputStream && partContentType.getBaseType().toLowerCase().startsWith(TEXT_MIME_TYPE_LC)) {
// to be closed in finally
actualPartDataStream = (InputStream) partContent;
//get the charset of the part - default to us-ascii
String charset = partContentType.getParameter("charset");
if (charset == null) {
charset = "us-ascii";
}
//read stream by large chunks to minimize memory fragmentation
int bufLen = 4096;
byte[] buffer = new byte[bufLen];
StringBuffer dataStringBuffer = new StringBuffer();
bufferedStream = new BufferedInputStream(actualPartDataStream);
int numBytesRead = bufLen;
while (numBytesRead == bufLen) {
numBytesRead = bufferedStream.read(buffer, 0, bufLen);
if (numBytesRead != -1) {
dataStringBuffer.append(new String(buffer, 0, numBytesRead, charset));
} else {
//we've reached end of stream
break;
}
}
partContentAsString = dataStringBuffer.toString();
} else {
log.debug("Skipping MIME part as it is binary");
continue;
}
if (isValueRegularExpression) {
actualResult = Pattern.compile(expectedValue).matcher(partContentAsString).find();
} else {
actualResult = partContentAsString.indexOf(expectedValue) >= 0;
}
//continue anymore
if (actualResult) {
break;
}
}
return actualResult;
} catch (MessagingException me) {
throw new RbvException(me);
} catch (PackageException pe) {
throw new RbvException(pe);
} catch (IOException ioe) {
throw new RbvException(ioe);
} finally {
IoUtils.closeStream(actualPartDataStream);
IoUtils.closeStream(bufferedStream);
}
}
use of javax.mail.MessagingException in project Activiti by Activiti.
the class EmailServiceTaskTest method assertEmailSend.
// Helper
public static void assertEmailSend(WiserMessage emailMessage, boolean htmlMail, String subject, String message, String from, List<String> to, List<String> cc) throws IOException {
try {
MimeMessage mimeMessage = emailMessage.getMimeMessage();
System.out.println(mimeMessage.getContentType());
if (htmlMail) {
assertTrue(mimeMessage.getContentType().contains("multipart/mixed"));
} else {
assertTrue(mimeMessage.getContentType().contains("text/plain"));
}
assertEquals(subject, mimeMessage.getHeader("Subject", null));
assertEquals(from, mimeMessage.getHeader("From", null));
assertTrue(getMessage(mimeMessage).contains(message));
for (String t : to) {
assertTrue(mimeMessage.getHeader("To", null).contains(t));
}
if (cc != null) {
for (String c : cc) {
assertTrue(mimeMessage.getHeader("Cc", null).contains(c));
}
}
} catch (MessagingException e) {
fail(e.getMessage());
}
}
use of javax.mail.MessagingException in project nhin-d by DirectProject.
the class RefreshSecurityAndTrustStateMailet method service.
/**
* {@inheritDoc}
*/
@Override
public void service(Mail mail) throws MessagingException {
LOGGER.info("Gateway state refresh requested through RefreshSecurityAndTrustStateMailet. " + "Attempting to stop and restart the settings update manager.");
GatewayState gwState = GatewayState.getInstance();
try {
if (gwState.isAgentSettingManagerRunning())
gwState.stopAgentSettingsManager();
gwState.startAgentSettingsManager();
// flush the caches
CertCacheFactory.getInstance().flushAll();
} catch (Exception e) {
LOGGER.warn("Failed to restart the settings update manager.", e);
} finally {
mail.setState(Mail.GHOST);
}
}
Aggregations