Java MimeMessage to eml File with all attachments - email

I have an incoming MimeMessage in my JAMES mail server. I want to create an eml file dumping the message completely. I tried using the writeTo method of MimeMessage - resulting file contains only the text body of the email. The attachments are not written to the eml file. My code is something like
String logFileName = "dumpNow.eml";
incomingEmail.getMessage().writeTo(new FileOutputStream(new File(logFileName)));
I do not get any multipart content in the dump. Is there any Util available to do this? Apache Mimeutils is also giving the same result.

Try this :
// Create your attachement file
File emlFile = new File("myFile.eml");
emlFile.createNewFile();
incomingEmail.getMessage().writeTo(new FileOutputStream(emlFile));
MimeBodyPart attachment = new MimeBodyPart();
DataSource source = new FileDataSource(emlFile);
attachment.setDataHandler(new DataHandler(source));
attachment.setHeader("Content-Type", "application/octet-stream");
attachment.setFileName("myFileName.eml");
attachment.setDescription("My file description");
attachment.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(attachmentFile);
I think it is because you missed to set the header and the disposition in your code.
Hope it helps,

Related

Soap message encoded in base64 binary data multiple files

Recently I got a new project to work on a SOAP service and to Get and Post messages to a ASP.NET service based on xml.
The issue is that I managed to make the soap request and get the message.
The message looks like this:
UEsDBBQAAAAIAAdUe06+NXE0kR4AADLSAQALAAAAUHJvZHVzZS54bWzUXW1z48YN5k/h5EMnmbMsvomSpmkzFCXbjERJoSTb52/p9dq5mbxN2svczy/........
The message is Base64 Binary on RFC 4648 with multiple xml documents on it.
How I can construct this documents from the code in php?
The documents encrypted in this request are 3 xml files.
I managed to get them from an online decryptor called freeformatter with download function.
If I try to decode the result I get something like:
PKT{N�5q4�2�Produse.xml�]ms��
�O��C'��,����i3%یDI�$��o��ڹ��M����/�,��|vL�O�$�/�xv,,�u�s>9?;?....
Is there a solution for this?
I'm new to SOAP so I don't understand too much of it.
Thank you but i mannged to solve it.
I gonna post here the sollution so everyone who facing the same issue, get the response.
The first thing you need to do when you have an .zip file in a base64 binary string is to catch the response to a txt file.
Let's say the response from soap it's called ' $response ' and we need to catch this to an file. We do like this :
$response = $client -> _getLastResponse();
$fille = "response.xml";
fille_put_contents($fille,$response);
Now we got the response to an xml file.
The next thing to do is to get the response from xml values.
Lets say our value is <ResponseFromServer> .
`$b64 = "b64.txt";
$dom = new DomDocument();
$dom = load("response.xml");
$data = $dom->getElementByTagName("ResponseFromServer");
$catchb64 = $data;
fille_put_content($b64,$catchb64);`
Now we got the clean Base64 Binary string in one fille.
The next thing we need is to create the document ( in this case is a .zip fille)
`$input_fille = "response.txt"; // the fille with clean base64 binary data on it
$output_fille = "result.zip"; //the fille we need to create on system with the
documents decrypted
$content = fille_get_contents($input_fille); // Reading the content of input fille
$binary = base64_decode($content); // Decoding to binary
fille_put_contents($output_fille,$binary); // Writing to fille the vallues`
We dont need the ZipArchive() function, because is allready a zip archive, all we need to do is to create a empty document and after to send the binary data to it.
Cheer's and goodluck!

AppleScript sending emails with attachment between contents

I'd like to send and email with apple script to do like this :
hi,
here is the attachment file
[The attachment.pdf]
As you can see, it is a good attachment
Thanks
Bye
I know how to use applescript to send an email with attachment at the end of the message, but not between 2 sentences.
I tried something like this, but it doesn't work :
set _attachment to "Macintosh HD:Users:usr:Desktop:Form.pdf"
set msg to make new outgoing message with properties {visible:true, subject:"Here it is", content: "hi,
here is the attachment file"}
tell msg to make new attachment with properties {file name:_attachment as alias}
tell msg to add "As you can see, it is a good attachment
Thanks
Bye" as content
Thanks you all for your support.
Dam
Assuming we are talking about Apple Mail, you can specify the location of the attachment in the content of the message.
The text of body is passed instantly and the attachment is inserted afterwards.
set _attachment to (path to desktop as text) & "Form.pdf"
tell application "Mail"
set msg to make new outgoing message with properties {visible:true, subject:"Here it is", content:"hi,
here is the attachment file
As you can see, it is a good attachment
Thanks
Bye"}
tell content of msg to make new attachment with properties {file name:_attachment as alias} at after third paragraph
end tell

Attach a file to a mail and send it

I have the following code, which tries to send a mail with a .pdf file attached. This .pdf is located in my Google drive.
var files = DriveApp.getFilesByName('file_test.pdf');
GmailApp.sendEmail(email, subject, message, {attachments:files[0]});
I've found that "getFilesByName" returns an array, so that's why I'm using files[0], but it's still not working. I don't know how to debug it, so when I'm testing my code, I am able to send the mail, but I receive it with no attached files.
When you use getFilesByName you are receiving a FileIterator, so you should use the hasNext() and next() functions to retrieve your file.
var files = DriveApp.getFilesByName('file_test.pdf');
if(files.hasNext()){
var file = files.next();
GmailApp.sendEmail(email, subject, message, {attachments:file});
}
In the GmailApp docs, they use the getAs() function on the file to return it as a PDF, this may or may not be necessary in your case.
See:
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)
https://developers.google.com/apps-script/reference/drive/drive-app

System.Net.Mail alternative without message size increase

We are using the System.Net.Mail to send email messages as text with attachments. The attachments are Excel and Powerpoint files. The content types are set to the MIME types before sending the email.
A test done with three emails proved that the Exchange server recorded a 26% increase in each of the cases.
Is there a way to stop this increase in message size?
If not, is there a another .NET or open source alternative for this?
Would SMTP Drop overcome this issue?
UPDATE:
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress(string.Empty, fromEmailAddress));
mimeMessage.To.Add(new MailboxAddress(string.Empty, toEmailAddress));
mimeMessage.Cc.Add(new MailboxAddress(string.Empty, copyEmailAddress));
mimeMessage.Subject = subject;
var builder = new BodyBuilder { HtmlBody = bodyText };
MvcApplication.Logger.Info("SendEmail:attachmentFiles:Count=" + attachmentFiles.Count);
foreach (var attachmentFile in attachmentFiles)
{
var attachment = new MimePart()
{
ContentObject = new ContentObject(File.OpenRead(attachmentFile)),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Binary,
FileName = Path.GetFileName(attachmentFile)
};
builder.Attachments.Add(attachment);
}
MvcApplication.Logger.Info("SendEmail:attachmentFiles:Added Count=" + builder.Attachments.Count);
mimeMessage.Body = builder.ToMessageBody();
using (var client = new SmtpClient())
{
client.Connect("smtp.domain.com", 25, false);
client.Send(mimeMessage);
MvcApplication.Logger.Info(
client.Capabilities.HasFlag(SmtpCapabilities.BinaryMime)
? "SMTP Server supports BinaryMime"
: "SMTP Server does NOT support BinaryMime");
client.Disconnect(true);
}
The above code sends a HTML message successfully.
The SMTP Server Capabilities flag for BinaryMime returns true.
If the ContentTransferEncoding is Base64 it works(9 excel and powerpoint files attached). If I change it Binary then just one corrupt excel file is attached. What am I missing here?
If the SMTP server supports the BINARYMIME extension, you could use MailKit to send email and set the Content-Transfer-Encoding of the attachments to ContentEncoding.Binary.
I don't think that System.Net.Mail supports the BINARYMIMNE SMTP extension (at least it didn't look like it did when I reviewed the referencesource on GitHub), hence System.Net.Mail.SmtpClient will always base64 or quoted-printable encode attachments.
The BINARYMIME SMTP extension allows clients to send messages without the need to abse64 or quoted-printable encode them.

Hebrew chars in email sent with JavaMail appears as question marks

What can be done?
Are you setting the character encoding correctly?
try this:
mimemessage.setText(s6,"utf-8");
you may need utf-16, cant remember what char set hebrew is on off the top of my head.
try here http://www.i18nguy.com/unicode/codepages.html
3 years late, but if someone hits this one, I found the answer:
MimeMessage message = new MimeMessage(mailSession);
Multipart multipart = new MimeMultipart("alternative");
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(new String(messageHtml.getBytes("UTF8"),"ISO-8859-1"), "text/html");
multipart.addBodyPart(htmlPart);
message.setContent(multipart);
message.setFrom(new InternetAddress(from));
message.setSubject(subject, "UTF-8");
The trick was to convert my html from UTF-8 (the way it came from my message_iw.properties), and then transform it into ISO format, so that there's no need to set any headers.