I have an email which has multipart/alternative part . Into this part there is a multipart/related part; Into this multipart/realted part I have an text/html and image/jpg. I'm trying to extract those two parts and adda tag to the html part message and compose it again. The html and image part should look the same. I have inserted only a tag into the html part. But when I try to compose the message again I somehow cannot manage. I don't know how to recreate this multipart nested into another multipart. I have this
into the original email and again into my htmlmessageBodyPart. But when my recreated e-mail is displayed the image is broken.
below is part of my sender class. It works ok with attachments, plain text and so on.Any help will be appreciated!
// create a multi-part to combine the parts
Multipart multipart = new MimeMultipart(
setSubtype(origMessage));
// create a new message part for body text
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
messageBodyPart.setDisposition(MimePart.INLINE);
multipart.addBodyPart(messageBodyPart);
BodyPart htmlmessageBodyPart = new MimeBodyPart();
htmlmessageBodyPart.setContent(html, content);
htmlmessageBodyPart.setDisposition("null");
multipart.addBodyPart(htmlmessageBodyPart);
MimeBodyPart imageBodyPart = new MimeBodyPart();
imageBodyPart.setHeader("Content_ID",
"<part1.40705.09505#something.com>");
imageBodyPart.setDisposition(MimePart.INLINE);
imageBodyPart.setContent(html,"image/png");
imageBodyPart.attachFile(file);
multipart.addBodyPart(imageBodyPart);
msg.setContent(multipart1);
Transport.send(msg);
Your original message has an extra level of nested multiparts that's missing from your code.
// add text/plain part to multipart, then...
Multipart innerMultipart = new MimeMulpart("related");
// add html and image parts to innerMultipart
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(innerMultipart);
multipart.addBodyPart(mbp);
Related
I use the JavaMail Spring implementation with Velocity template engine. Everything goes well when I send the mails, the pictures are displayed normally and the styles also apply.
The problem that I encounter is that at the reception the email client displays my email as containing attachments, which are actually the images that appear in my email.
I do not understand why the images sent with the mail are seen as pieces joined by the mail client. Somebody can help me please?
This is my code :
final MimeMessage message = mailSender.createMimeMessage();
final MimeMessageHelper helper = new MimeMessageHelper(message,true, "UTF-8");
try {
helper.setFrom(getSystemAddress());
if (replyTo != null) {
helper.setReplyTo(replyTo);
}
helper.setTo(to);
helper.setSubject(subject);
helper.setText(body, isHTML);
System.out.println("Helper message factory: "+helper);
FileSystemResource res = new FileSystemResource(new File(MailHandler.class.getResource("logo-email.png").getFile()));
FileSystemResource res1 = new FileSystemResource(new File(MailHandler.class.getResource("bg-header.PNG").getFile()));
helper.addInline("cid2", res1);
helper.addInline("cid1", res);
mailSender.send(message);
I am trying to attach the HTML file to the Gmail body, but the image which I have attached in the HTML file is not getting displayed in Gmail (working fine in Outlook).I have tried putting the base64 code of image but still the same issue (In this case even in Outlook the image is blocked). I even tried with "Content ID generator".
Please see following code what I have tried), but unable to display image.
Can some one tell me how to solve this issue?
(Note: I have used the free marker template to generate the HTML code)
Map<String, String> inlineImages = new HashMap<String, String>();
inlineImages.put("image2", "C:\\Users\\xxxxxxx\\Desktop\\Capture1.jpg");
model.put("image", inlineImages);
String text = geFreeMarkerTemplateContent(model);
System.out.println("Template content : "+text);
helper.setText(text, true);
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(text, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart);
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setHeader("Content-ID", "image2");
imagePart.setDisposition(MimeBodyPart.INLINE);
// attach the image file
imagePart.attachFile("C:\\Users\\xxxxx\\Desktop\\Capture1.jpg");
multipart.addBodyPart(imagePart);
mimeMessage.setContent(multipart);
I have written a WebAPI controller method that finds a mail by its unique ID from ExchangeOnline. I wrote a small model class in order to store some attributes of a mail like the subject, the sender, the date received and so on.
Now I also want to access file attachments if the mail has such attachments. Therefore, I wrote this code (just the relevant part):
List<AttachmentItem> attDataContainer = new List<AttachmentItem>();
EmailMessage originalMail = EmailMessage.Bind(service, new ItemId(uniqueID), new PropertySet(ItemSchema.Attachments));
foreach (Attachment att in originalMail.Attachments)
{
if (att is FileAttachment && !att.IsInline)
{
FileAttachment fa = att as FileAttachment;
fa.Load();
attDataContainer.Add(
new AttachmentItem
{
ID = fa.Id,
Name = fa.Name,
ContentID = fa.ContentId,
ContentType = fa.ContentType,
ContentLocation = fa.ContentLocation,
Content = Convert.ToBase64String(fa.Content),
Size = fa.Size
});
}
}
The method indeed finds the attachments and displays all of the attributes you can see in the "AttachmentItem" object - BUT NOT the fa.Content attribute.
I have crwaled almost any document I could find on this (especially the *.Load() part as well as much examples. But in my case I get "byte[0]" when debugging the output.
Do you have any idea for me what could be the reason for this?
PS: By the way, I have version v2.0.50727 of Microsoft.Exchange.WebServices referenced.
Best regards and thanks in advance,
Marco
When you call the load method on the Attachment that should make a GetAttachment request to the server which will return the data for that Attachment. If you enable tracing https://msdn.microsoft.com/en-us/library/office/dn495632(v=exchg.150).aspx that should allow you to follow the underlying SOAP requests which should help with troubleshooting (eg you can see what the server is returning which is important).
The other thing to check is that is this is a real attachment vs a One Drive attachment which could be the case on ExchangeOnline.
I want to upload a file using Multipart/related Content-Type via BackgroundUploader in windows 8.1
My code is as follows
BackgroundUploader uploader = new BackgroundUploader();
uploader.SetRequestHeader("Content-Type", "multipart/related; boundary=foo_bar_baz");
uploader.Method = "POST";
// Create upload content
List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>();
// File metadata
var part = new BackgroundTransferContentPart();
part.SetHeader("Content-Type", "text/plain");
part.SetText(file.DisplayName);
parts.Add(part);
// File
// Here file is of type StorageFile
part = new BackgroundTransferContentPart();
part.SetHeader("Content-Type", file.ContentType);
part.SetFile(file);
parts.Add(part);
UploadOperation upload = await uploader.CreateUploadAsync(new Uri("upload_url",UriKind.Absolute), parts);
await upload.StartAsync().AsTask(cts.token); // cts is CancellationTokenSource
However, when I run this code I get an Exception saying
WinRT information: 'boundary': If the 'Content-Type' header is set,
the boundary cannot be empty and must match the boundary set in the
'Content-Type' header.
What is wrong/missing in my code?
If you use the CreateUploadAsync with four parameters then the following might be helpful:
var uploader = new BackgroundUploader();
uploader.SetRequestHeader("Content-Type", "multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY-3770FB3C-534D-4E6B-9BE0-392FDA960F7B");
var upload = await uploader.CreateUploadAsync(uri, parts, "form-data", "0xKhTmLbOuNdArY-3770FB3C-534D-4E6B-9BE0-392FDA960F7B");
await upload.StartAsync();
Set the boundary in the "SetRequestHeader" without "" (quotes).
Set the same boundary later as the last parameter of CreateUploadAsync.
If you are passing a list of content parts, you don't need to set the Conent-Type header. It's set automatically.
However if you insist in doing so, try:
uploader.SetRequestHeader(
"Content-Type",
"multipart/form-data; boundary=\"foo_bar_baz\"");
UPDATE:
Try using the CreateUploadAsync overload that takes four parameters, where the fourth parameter is the boundary. See here: http://msdn.microsoft.com/en-us/library/ie/hh923975
I'm trying to provide a way to forward emails, just as we do in Outlook. When the user clicks forward, it opens a form with the original message and header information on top, giving the user an opportunity to modify the body. To get the message, I do the following.
var item = (EmailMessage)dataGridEmail.SelectedItem;
ResponseMessage forward = item.CreateForward();
textBlockForward.Text = forward.Body;
I get an ServiceObjectPropertyException with message "You must load or assign this property before you can read its value". If I try to load the value of the body property before accessing, I get NotSupportedException with message "Specified method is not supported". Is there a way to get the forwarding message before actually sending it?
Instead of using forward.Body, you should use item.Body to populate your textBlockForward control. See Forwarding Email Messages using EWS on MSDN for more details.
var item = (EmailMessage)dataGridEmail.SelectedItem;
ResponseMessage forward = item.CreateForward();
textBlockForward.Text = item.Body; // needs to come from original message source
forward.BodyPrefix = "new body contents"; // prepended body content
I am note sure if anyone could solve this. This is what i did.
ResponseMessage responseMessage = message.createForward();
// message is an EmailMessage object
responseMessage.setBodyPrefix(body);
responseMessage.save(WellKnownFolderName.Drafts);
EmailMessage saved = responseMessage.save();
saved.load(new
PropertySet(BasePropertySet.FirstClassProperties,ItemSchema.Body));
MessageBody messageBody = saved.getBody();
// do something with messageBody`enter code here`
//System.out.println(saved.getBody().toString());
saved.sendAndSaveCopy();