Javamail Content-Type - image

I'm trying to send an Email via javamail adding embedded Images and an Outlook Invation which should be displayed showing up the buttons to accept/decline .... To make the buttons show up, i have to set the Content-Type of the MimeMultipart to related.
MimeMultipart multipart = new MimeMultipart("related");
Once this is done, the embedded Pictures wont show up again.
To Display the embedded images it should be alternative.
I have send myself a Mail from Outlook containing an Image as well as a invation to a meeting.
When I check the header there, it says:
Content-Type: multipart/related;
boundary="_005_CCF531A072806F489D02A6DD2CF322A01D88EEA2de35s004hst42wp_";
type="multipart/alternative"
I have already tried to set the Content-Type adding Parameters (type, charset ...) but it seems like their just getting ignored when sending the message. Here is the Method which I am calling to create the Message Part:
private Message generateMessage() throws Exception
{
Configuration conf = Configuration.getInstance();
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", conf.getProperty(Configuration.MAIL_SERVER));
// Get session
Session session = Session.getDefaultInstance(props, null);
// Create the message
Message message = new MimeMessage(session);
// Fill its headers
message.setSubject(this.subject);
message.setFrom(new InternetAddress(fromEmail));
Address[] replyTo = new Address[]{new InternetAddress(replyEmail)};
message.setReplyTo(replyTo);
// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
multipart.addBodyPart(messageBodyPart);
// ADD IMAGES
multipart = attachImages(multipart);
// ADD CALENDAR-FILE
multipart = attachCalendarFile(multipart);
// Set Content-Type
ContentType ct = new ContentType(message.getContentType());
ct.setParameter("charset", "utf-8");
ct.setParameter("type", "multipart/alternative");
// Set Multipart-Content to Message including the Content-Type
message.setContent(multipart, ct.toString());
return message;
}
The output of message.writeTo(System.out); is the following:
From: mail#sender.com
Reply-To: noreply#recipient.com
Message-ID: <1150322817.1.1397725164700.JavaMail.User#MacBooks-mailserver.local>
Subject: test
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="----=_Part_0_72769750.1397725164655"
------=_Part_0_72769750.1397725164655
Is there any chance to make it work with embedded images AND invations?
Thanks in advance!

There's three types of multiparts, each used for a different purpose. In some cases you might need to use all three. For example, a message with a calendar attachment, html content using images, and equivalent plain text content, might have a structure such as this:
multipart/mixed
multipart/alternative
text/plain
multipart/related
text/html
image/jpeg
text/calendar

Google search led me here, so for future searchers:
#bill-shannon is of course correct. However, please save yourself the trouble of figuring out the low level Mime RFC stuff, and just use something like Simple Java Mail (open source) which figures out which structure suits best based on the input you provide. You can interactively explore all the possible structures Simple Java Mail can come up with. Disclaimer: I maintain this library (but I actually came across this SO question in my research into including voting buttons in emails for Outlook :) )
In your case you would use the following. First produce a Calendar content string, using something like ical4j:
Calendar icsCalendar = new Calendar();
icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
icsCalendar.getProperties().add(Version.VERSION_2_0);
(..) // add attendees, organizer, end/start date and whatever else you need
// Produce calendar string
ByteArrayOutputStream bOutStream = new ByteArrayOutputStream();
new CalendarOutputter().output(icsCalendar, bOutStream);
String yourICalEventString = bOutStream.toString("UTF-8")
Then use Simple Java Mail's email builder to set the Calendar content alongside everything else such as images:
Email email = EmailBuilder
.startingBlank()
.withCalendarText(CalendarMethod.REQUEST, yourICalEventString)
.everythingElseLikeImagesHeadersAttachmentsEtcEtc()
.buildEmail();
Then send using the mailer builder:
MailerBuilder
.withSMTPServer("server", 25, "username", "password")
.buildMailer()
.sendMail(email);
Then depending on whether you provided text, html attachments or other things, Simple Java Mail will select the appropriate Mime structure with mixed/related/alternative parts.

Related

JMeter: how to send formatted text via SMPT sampler

Is there a way to send a formatted text with links via JMeter SMTP sampler?
Scenario:
I need to send an email, where one of the words e.g. My "Instagram" will have a link to my instagram page.
Option 1:
Create such email in Gmail, send it to myself, then download it as .eml file and send use "Send .eml" option in SMTP sampler.
However, my issue is that these links should be changed and lead to different instagram pages with each new email sent, thus I need to pass it as a variable from CSV file. This seems to be impossible to achieve with .eml file as it needs to be modified before each request. Unless there is a way?
Option 2 (preferred):
Somehow I need to format text in "Message body" of SMTP sampler. I've tried to copy/paste the same style and tags from "Original" .eml file, but it is always sent as a plain text and Gmail won't format it on client side.
Here is an example of RAW Gmail text with formatted link which I've tried to use in "Message" text box of SMTP Sampler:
Visit my account #dummyaccount<https://l.instagram.com/?u=https%3A%2F%2Ftaplink.> for more info.
Expecting to see the following in the email:
Visit my account #dummyaccount - where #dummyaccount is a hyperlink
Actual:
Visit my account #dummyaccounthttps://l.instagram.com/?u=https%3A%2F%2Ftaplink. for more info.
Any suggestion will be greatly appreciated.
I don't think you can customize the SMTP Sampler messages this way. You will need to either compose the message in an email application, save it to .eml file and send this .eml or if you want to build the message content dynamically you can use JSR223 Sampler and Groovy language for sending your message.
Example code:
Properties props = new Properties();
props.put("mail.smtp.host", "your-smtp-server-here");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
def session = javax.mail.Session.getInstance(props, new CredentialsAuthentication() as javax.mail.Authenticator)
def msg = new javax.mail.internet.MimeMessage(session)
def from = new javax.mail.internet.InternetAddress("your-email-address-here", "your first and last name");
msg.setFrom(from);
def toAddress = new javax.mail.internet.InternetAddress("your-recipient-here ");
msg.setRecipient(javax.mail.Message.RecipientType.TO, toAddress);
msg.setSubject("Test");
msg.setContent("<html>\n" +
"<body>\n" +
"\n" +
"<a href=\"http://link-to-your-instagram-profile\">\n" +
"Link to my Instagram</a>\n" +
"\n" +
"</body>\n" +
"</html>", "text/html")
javax.mail.Transport.send(msg);
class CredentialsAuthentication extends javax.mail.Authenticator {
#Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication("your-username#most-probably-with-email.com", "your-password-here")
}
}

Retrieve plain text body from email

Right now I use this code to retrieve the body of an email from Exchange 2010:
i.Load(New PropertySet(BasePropertySet.IdOnly, ItemSchema.Body))
Dim strEmailBodyLines() As String = Split(i.Body.Text, vbCrLf)
Unfortunately I get a HTML response, but I only want plain text from the body in an array (each line). If I replace ItemSchema.Body with ItemSchema.Textbody I get an error it is support on Exchange 2013 and higher.
Can someone tell me how to get the plain text body?
I know the C# code for the same, you may convert this to VB:
// Create empty mail object for holding email details
EmailMessage mail;
// Bind properties from acual email message to mail object
mail = (EmailMessage)Item.Bind(_service, new ItemId(MessageUID));
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly);
// Convert the email body to plain text from HTML
PropertySet propertySets = new PropertySet(BasePropertySet.FirstClassProperties)
{
RequestedBodyType = Microsoft.Exchange.WebServices.Data.BodyType.Text
};
mail.Load(propertySets);

Get message content from mime message?

I have a java spring integration project that is receving emails through the below code:
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext(
"/integration/gmail-imap-idle-config.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
inputChannel.subscribe(message -> {
org.springframework.messaging.Message<MimeMailMessage> received =
(org.springframework.messaging.Message<MimeMailMessage>) message;
log.info("content" + message);
List<String> sentences = null;
try {
} catch (Exception e) {
}
I get the email, and I can get the subject, but I can never actually extract the message body. How do I do this?
Thank you!
You have to use this option on the channel adapter:
simple-content="true"
See its description:
When 'true', messages produced by the source will be rendered by 'MimeMessage.getContent()'
which is usually just the body for a simple text email. When false (default) the content
is rendered by the 'getContent()' method on the actual message returned by the underlying
javamail implementation.
For example, an IMAP message is rendered with some message headers.
This attribute is provided so that users can enable the previous behavior, which just
rendered the body.
But still it is doubtful, since I see in case of GMail message it is never simple. The content is a MimeMultipart and we need to read its parts to get access to the real body.
So, this is how you should change your code as well:
log.info("content" + ((MimeMultipart) ((MimeMessage) message.getPayload()).getContent()).getBodyPart(0).getContent());

HTML email is getting displayed as plain/text with javamail in Gmail and Yahoo

Trying to send HTML email with java-mail . I can see HTML format email in Outlook but Gmail and Yahoo don't show HTML format , they are showing email as plain texts without HTML formatting .
I am using company's SMTP server to send an email to users .
I tried following and msgcontent is StringBuilder in following code :
Properties prop = System.getProperties();
prop.put("mail.smtp.auth", "false");
prop.put("mail.smtp.starttls.enable","false");
prop.put("mail.smtp.host", SMTP_SERVER);
Session session = Session.getInstance(prop);
MimeMessage msg = new MimeMessage(session);
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart content = new MimeBodyPart();
content.setHeader("content-type", "text/html");
msg.setFrom(new InternetAddress(EMAIL_FROM));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(EMAIL_TO, false));
msg.setSubject(EMAIL_SUBJECT);
content.setContent(msgcontent.toString(), "text/html");
multipart.addBodyPart(content);
msg.setContent(multipart);
I want HTML email to be displayed in all email clients like Gmail , Yahoo, etc. Currently only Outlook can display HTML content.
You don't really need a multipart since your message has only one part.
You can simplify the content setting by replacing the two content.set* lines with:
content.setText(msgcontent.toString(), "utf-8", "html");
What version of JavaMail are you using?
If you examine the raw MIME content of the message you receive and compare it with the raw MIME content of the message you send, are they different?

Forward email using EWS managed api keeping the headers

I am looking for a sample code on how to forward an existing email message (one that is already in my inbox) using the managed api.
When forwarded is there some way to keep a message original headers while forwarding it?
for example someone sent an email to me -i would like that ews will forward it to another recipient without changing the headers (original receive time from ,bcc etc...).
Given an EmailMessage object, you can just call the CreateForwareMessage() method:
var forwareMessage = item.CreateForward();
Regarding the other question: Get the MIME content of the mail and attach it to a new message:
item.Load(new PropertySet(BasePropertySet.IdOnly, ItemSchema.MimeContent));
var mail = new EmailMessage(service);
var attachment = mail.Attachments.AddFileAttachment("Original message.eml", item.MimeContent.Content);
attachment.ContentType = string.Format("message/rfc822; charset={0}", item.MimeContent.CharacterSet);
mail.ToRecipients.Add("hkrause#infinitec.de");
mail.Subject = "testmail";
mail.SendAndSaveCopy();
EDIT:
Create forward message and set reply to header:
var fw = item.CreateForward();
var fwMsg = fw.Save(WellKnownFolderName.Drafts);
fwMsg.ReplyTo.Add("personA#company.com");
fwMsg.SendAndSaveCopy();

Resources