sending a .rtf file with javaMail - jakarta-mail

I am trying to get a file with the .rtf extension as an attachment with an email. I cannot seem to get it in my mailbox.
the code I currently use
try
{
Message msg = new MimeMessage(session);
msg.setFrom( ); // this is filled in but hidden for this question
msg.addRecipient();// this is filled in but hidden for this question
msg.setSubject("test email");
msg.setText("body test content");
msg.setSentDate(new Date());
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(receiveFile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.attachFile(backupFile);
messageBodyPart.setFileName("reportFile.rtf");
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport.send(msg);
}
receiveFile is the rtf file in question that needs to be send as an attachement.
Do not bother wtih server settings and such. I have send emails using this code so that works all just fine :). and had success sending .txt or .doc files as well so I know my info is correct. just when I try to send it as reportFile.rtf the mail just does not arrive. and I have tried 2 systems both together (the datahandler + source path and the attachFile path) and both did not really give me what I wanted.
Is a rtf file as attachment possible using javaMail or am I looking in the wrong direction?

After looking at it more closely with my co-worker we figured out that the email I used as sender was interperted as a spam and thus dumped it in the spam folder. now in itself this is not a problem since it was fine for testing. However it seems that if you keep spamming with that address the mail server indeed blocks like a good percentage of the mails i was sending. So this made it hard to debug since it sometimes got through and sometimes did not. the issues has been resolved now.

Related

Unable to retrieve attachments from a signed mail having ContentType as "application/pkcs7-mime; name=smime.p7m"

I am trying to read a digitally signed mail from java code using multipart and mime messaging and fetch the attachments (xml, pdf, txt etc.,) and message details.
My code is working fine for mails having Content-Type as : multipart/signed; protocol="application/x-pkcs7-signature";
But For few mails having Content-Type as : application/pkcs7-mime; smime-type=signed-data; name=smime.p7m it is not fetching the attachments and message details. Can anyone explain what is the difference between both of them and how to resolve it.
I recently came across this issue myself, and although this question is three month old, I leave an answer with my findings, just in case.
Both kinds of messages are instances of S/MIME signed messages as specified in RFC2633 (https://www.rfc-editor.org/rfc/rfc2633).
The multipart/signed; protocol="application/x-pkcs7-signature" indicates a clear-signed message (section 3.4.3.3 of the RFC), meaning you can read the original message content without having S/MIME capabilities in your client code. Hence no problem with these.
The application/pkcs7-mime; smime-type=signed-data; name=smime.p7m indicates an S/MIME signedData email (section 3.4.2) Your client code needs S/MIME capability in order to read the original message (even if you don’t care about the signature).
Easiest way (worked for me) is to use bouncycastle's SMIMESigned class (from the S/MIME API, https://mvnrepository.com/artifact/org.bouncycastle/bcmail-jdk15on), like this:
byte[] content = <the signed data's content as byte[]>;
ByteArrayDataSource dataSource = new ByteArrayDataSource(content,"multipart/signed");
SMIMESigned signedData = new SMIMESigned(new MimeMultipart(dataSource));
MimeBodyPart bodyPart = signedData.getContent();
<you can process the body part as normal from here>

Sending attachments as an email from a form - NOT from local files

I have created a HTML email form which allows a user to enter To, subject, message, content and attachments however I cannot get the attachments to send.
I have researched online and came across many variations of this code:
messageBodyPart = new MimeBodyPart();
String filename = "/home/manisha/file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
But is there a way of sending attachments input into the form instead of adding the file path to a file in the code?
Thanks
First, the files need to be uploaded from the browser to the server using the html form. Depending on what you're using to manage the uploaded data, you can store the file data in memory or in files on the server. If you store it in memory, you can use a ByteArrayDataSource instead of FileDataSource in your code above.

java mail attachments getting corrupted

This is my code for attaching the files to the mail:
Multipart mp=new MimeMultipart("mixed");
BodyPart mbody=new MimeBodyPart();
mbody.setHeader("Content-Type", "text/html; charset=us-ascii");
mbody.setHeader("Content-Transfer-Encoding","7bit");
mbody.setContent(content2, "text/html");
mp.addBodyPart(mbody);
for(File file:f){
BodyPart mbody2=new MimeBodyPart();
DataSource ds=new FileDataSource(file.getAbsolutePath());
mbody2.setDataHandler(new DataHandler(ds));
mbody2.setFileName(ds.getName());
mbody2.setHeader("Content-Type", "multipart/mixed");
mbody2.setHeader("Content-Transfer-Encoding", "base64");
mp.addBodyPart(mbody2);
}
m.setContent(mp);
content2 is the html content I am embedding in the E-mail, and I am adding files from an arraylist f.
The problem here is that although the files get attached and I receive the E-mail fine, I am unable to open the attachments because the data is corrupt. This happens for all the files I've tried to attach like jpegs, pdfs, spreadsheets, word docs and txt files.
I read here: https://community.oracle.com/thread/1589120 that this could happen because JavaMail uses encoding that messes up the binary data of the file and adding mbody2.setHeader("Content-Transfer-Encoding", "base64"); should fix the problem but that doesn't work for me.
Any ideas on what could be wrong?
Thanks
Time for some debugging...
First, remove all of the setHeader calls; some of them are wrong and none of them should be necessary.
Next, determine if the problem is on the sending end or the receiving end. Try multiple mail readers to see if they all have problems with the attachments.
Try sending plain text attachments. Are they also corrupted?
Post the protocol trace showing what happens when you send a simple message with a simple attachment that fails, so we can see if the message is being constructed correctly.
What version of JavaMail are you using?
What mail reader are you using to view the attachments?

wpf "emailto" - with attachments

I am developing a WPF app in MVVM presentation pattern. I have a grid and I am trying to have an "emailto" hyperlink and when the user clicks that i am trying to export all the data to an excel and open the default email client with a draft new message window(email client could be Lotus/Outlook) and attach the excel as an attachment to the mail. I am able to define a "Mailto" hyperlink and when i click that i am able to open the draft message email window. But i am not sure how to send the excel as an attachment. Any help is greatly appreciated.
By sending an Excel file you don't mean generating of this file somehow. Right? So you just need to attach a file.
I always use Andrew Baker's MAPI wrapper class which seems to be very reliable and has never failed for last 6 years. It's just 18Kb of C# code and it does exactly what you need.
var message = new MapiMailMessage(subject, body);
message.Recipients.Add(mailAddress);
message.Files.Add(filePath);
message.ShowDialog();
You could use Simple MAPI API to solve your problem:
var mapi = new Mapi();
mapi.Logon(IntPtr.Zero);
foreach (var filePath in files)
mapi.Attach(filePath);
mapi.Send("subject", "body text", true /* show send message dialog to user */);
mapi.Logoff();

silverlight mailmessage embedde image is gray

My silverlight application is supposed to send an e-mail at some point to the preset e-mail address.
So far so good, it actually sends the e-mail.
My problem starts when I want to embed an image. This is one of many sample codes I'm actually using:
string Body = "<b>This is a test E-Mail</b><br><BR>This E-mail is being sent as part of beta testing...<BR><img alt=\"\" src=\"cid:imageId\" >";
ContentType ct = new ContentType(MediaTypeNames.Image.Jpeg);
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
LinkedResource imagelink = new LinkedResource(#"C:\\Users\\David\\Desktop\\Projectos\\WorkIt\\Resources\\Imagens\\workitlogosmall.jpg", ct);
imagelink.ContentId = "imageId";
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink);
mail.AlternateViews.Add(htmlView);
mail.IsBodyHtml = true;
I'm receiving the e-mail with the text formatted, but instead of the image, I get a gray box.
What's going on? I've tried different approaches to get the path for the image, but always with the same result!!
Can anyone explain what I'm doing wrong???
Forget about it... I've solved the issue.
It has nothing to do with with the code, the code WORKS fine.
The e-mail account that I'm using to receive the email's is one of my oldest and I don't use it that often, so I've completely forgotten that I had the "Show Content" deactivated for every mail that I receive (duh) and that's why it blocked the image I was sending.
I actually felt pretty stupid after realizing that.
Anyway, hope this helps anyone who is as distracted as I am :)

Resources