Free Support Forum - aspose.com

Email message is not converted in pdf

Hi Team,

I’m getting below error on document conversion service. I’m converting html body to pdf file.
Sometimes getting below exception.

com.aspose.email.system.exceptions.InvalidOperationException: Failed to set license. Details: Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks com.aspose.email.License.a(SourceFile:354)

@ashishyadava

You are getting license related exception on your end. Can you please ensure, if you are using the correct license file or a one which is not been modified? I request you to please share the license file privately with us by clicking my name icon and using Message option to send privately. Please do notify us here once you have shared the license privately.

Thanks for update. But we are not getting frequently this error. We are getting for 3 out 10 calls.

@ashishyadava
Can you please share the license file by adopting the mechanism shared. Please also share the sample project and environment details where you are using API and license file getting failed to load randomly.

Hi,
I have send you license file and sample code in private message.

@ashishyadava

In the sample code that you have shared, none of Aspose API has been referenced. You are using File class to load the license file and then converting it to ByteArrayInputStream. There is no sample code for any of Aspose API using the license. Secondly, instead of sharing the image of license file, please provide the license file that we may use on our end along with Aspose API using that license. I would appreciate if you may please provide the working sample code that is actually reproducing issue on your end and we may also try the same.

private ByteArrayInputStream getLicense() throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
ByteArrayInputStream bis = null;
try {
File file = new File(classLoader.getResource(“AsposeTotal.lic”).getFile());
bis = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));

	} catch (IOException e) {
		LOG.error("Aspose licence not found");
		throw new LicenceNotFoundException(ExceptionMessageConstant.ASPOSE_LICENSE_NOT_FOUND);
	}
	return bis;
}

private void removeAttachmentAndConvertEmlOrMsgFile(int asposeFormat, InputStream docInputStream, OutputStream os,
ByteArrayInputStream licenseFS, String timezone) throws Exception {
MailMessage mailMsg;
ByteArrayOutputStream arrayOutputStream = null;
MhtSaveOptions saveOptions;
com.aspose.email.License emailLicense = new com.aspose.email.License();
emailLicense.setLicense(licenseFS);
mailMsg = MailMessage.load(docInputStream, new EmlLoadOptions());
arrayOutputStream = new ByteArrayOutputStream();
saveOptions = SaveOptions.getDefaultMhtml();
saveOptions.setSaveAttachments(true);
AttachmentCollection attachmentCollection = mailMsg.getAttachments();
if (attachmentCollection != null) {
int size = attachmentCollection.size();
for (int i = 0; i < size; i++) {
Attachment att = attachmentCollection.get_Item(i);
String attFileName = att.getName().replace(":", " “).replace(”\", " “).replace(”/", " “)
.replace(”?", “”).replace(">", “”).replace("<", “”).replace("|", “”);
att.setName(attFileName);
att.setContentStream(new ByteArrayInputStream("".getBytes()));
}
}
mailMsg.setTimeZoneOffset(TimeZone.getTimeZone(timezone).getOffset(mailMsg.getDate().getTime()));
mailMsg.save(arrayOutputStream, saveOptions);
wordLicense(getLicense());
ByteArrayInputStream bis = new ByteArrayInputStream(arrayOutputStream.toByteArray());
com.aspose.words.LoadOptions loadOptions = new com.aspose.words.LoadOptions();
loadOptions.setLoadFormat(com.aspose.words.LoadFormat.MHTML);
com.aspose.words.PdfSaveOptions pso = new PdfSaveOptions();
pso.setJpegQuality(70);
Document doc = new Document(bis, loadOptions);

	// make inline image resize
	@SuppressWarnings("rawtypes")
	NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
	int imageIndex = 0;
	for (Shape shape : (Iterable<Shape>) shapes)
	{
	    if (shape.hasImage() && (shape.getWidth() > 700 || shape.getHeight() > 900))
	    {
	    	double largeDim= shape.getWidth() > shape.getHeight()? shape.getWidth(): shape.getHeight();
	    	double q= shape.getWidth() > shape.getHeight()? 700: 900;
	    	double divExp= q/largeDim;
	        String imageFileName = MessageFormat.format(
	                "Aspose.Images.{0}{1}", imageIndex, com.aspose.words.FileFormatUtil.imageTypeToExtension(shape.getImageData()
	                                .getImageType()));
	        shape.setWidth(shape.getWidth()*divExp);
	        shape.setHeight(shape.getHeight()*divExp);

	        shape.getImageData().save(imageFileName);
	        imageIndex++;
	    }
	}

	doc.save(os, pso);
	//doc.save(os, com.aspose.words.SaveFormat.PDF);
}

@ashishyadava

I have found the same code shared by you in another thread as well and have used that on my end by loading license file on my end with out any issue. Can you please try using the sample project shared over this link.

One mistake that I fixed in your code is that you have been using EmlOptions() class for loading MSG file that you have shared with us. In the attached project, please try setting the paths to license and source MSG files accordingly on your end and I hope it will work without any exception apart from truncated output issue that you have already reported to us.

Please find the attached eml file that causing the issue.
docset_pvai_10378803.zip (34.4 KB)

@ashishyadava

There is no exception in creating the MHTML and PDF for the shared file. Please observe the output for your reference.

docset_pvai_10378803_2.zip (170.8 KB)

@mudassir.fayyaz,

We have got this error while calling conversion API in bulk. It’s failed for like 3 out 100 API calls.
When we calling one by one it’s work fine for us.

@ashishyadava

It may be issue related to design of application that proper object dereferencing and memory clearing my not be done or you may be having a low heap size. As far as API is concerned, it has to work with a MSG/EML file. This doesn’t seems to be an API issue but still if you provide a working sample project (not Java snippet) along with source files and that we can use and are able to reproduce that on our end. But before that please first try increasing the heap size.

I have max available heap size 21333. I guess, It’s enough heap size.

image.png (3.0 KB)

Please share the requested information in form that I have requested. For your individual files, we have not encountered any issue and have shared results with you even.