EWS - FileAttachment Content is empty / byte[0] - exchangewebservices

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.

Related

Spring javaMail :Inline Images resources display like attachment in web client

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);

Send data changing page Windows Phone

I'm trying to change page and send some data to the new page just create. Basicaly, I have a page where the user could log, I'm gonna save the informations and then I'm going to open a new page by sendind the information to my "profil" page, where the user will have the information about his account. I'm using the MVVM patern, I don't know if it's going to change something but I ad this precision ;).
Edit: The only solution i have found is to pass the string of my object (gladly it is just strings). Because we can pass strings to another page. But I would prefer to give my object directly, or change my architecture if needed. Like don't create a page but replace my Usercontroles by others :(.
Thanks for the help.
For temp data:
If it is WP silverlight app, we can directly assign object to public property of destination page in OnNavigateFrom event of source page. For example, we can declare a public property A on Dest page, and implement the following in Source page to pass the user object:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Dest destPage = e.Content as Dest;
if(destPage != null)
{
User a = new User();
a.UserName = "aa";
a.ID = 1;
destPage.A = a;
}
}
For WP runtime app on WP 8.1, you can directly use Frame.Navigate(TypeName, Object) to pass the parameter.
For persist data:
I will suggest storing the data to local storage on source page and read it from destination page.
If I understand right you should use parameters in navigate URI
For example to send name you can use:
NavigationService.Navigate(new Uri("/View/Page.xaml?name=UserName", UriKind.Relative));
and then in OnNavigatedTo() check name parameter:
string name;
NavigationContext.QueryString.TryGetValue("name", out name);
You can use IsolatedStorageSettings.ApplicationSettings["keyname"]=yourobject;
And whenever you want to use this value just unbox this value as below,
if(IsolatedStorageSettings.ApplicationSettings.Contains("keyname"))
{
var obj=(yourobject)IsolatedStorageSettings.ApplicationSettings["keyname"];
}

SSRS streamIDs and chart images render with same id for different requests

Does anyone know if there is a way in the report definition to force a unique streamid for report images per request. I have a situation where I am rendering a report as a byte[] via the ReportExecutionsService web api. Whenever a report has images SSRS will give each image a streamid and I am required to save the parts to a temp web folder the client can access.
When I put a chart on a report the streamID never changes and this is causing a cache issue in browsers. The same chart displayed regardless of parameters, however, I can view the temp file on the server and see that the chart has been updated.
Posting a workable solution in case anyone is interested.
A. Calculate the physical and virtual mapping to a temp folder in your controller methods.
a. NOTE : For reports with charts you should create a folder per request for the report. I used:
private string GetVirtualTempFolder()
{
if (Url != null)
return Url.Content("~/Temp/");
else
return VirtualPathUtility.ToAbsolute("~/Temp/");
}
private string GetPhysicalTempFolder()
{
return AppDomain.CurrentDomain.BaseDirectory + #"Temp\";
}
...
string virtualTempFolder = "";
string physicalTempFolder = "";
if (makeUniqueTempFolder)
{
virtualTempFolder = this.GetVirtualTempFolder();
physicalTempFolder = this.GetPhysicalTempFolder();
string unique = Guid.NewGuid().ToString();
virtualTempFolder = virtualTempFolder + unique + "/";
physicalTempFolder = physicalTempFolder + unique + "/";
System.IO.Directory.CreateDirectory(physicalTempFolder);
}
B. Pass the directory names into your reporting services wrapper layer. I am using a DI so the signatures needed to be modified.
C. In the ReportingServices Rendering api you can pass the chosen virtual directory to pull temp files associated with streams via the DeviceInfo interface.
D. The streamIDs are returned in the Render method. Iterate and save them to the calculated physical location.
While I already had the temp folder functionality working, I had to add the ability to optionally create a temp location per request.

Get original message with headers when forwarding message before sending in EWS

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();

MSMQ - Message with Html

I'm working on project using MSMQ, messages are both sent and received.
However, when trying to access the message body I get an error noting "Root element is missing"
I can't see the problem, but wondered whether the Html in the message body could be causing it.
Can MSMQ deal with Html? What about Xml Serialisation with HTML in the body elements?
Thanks
Try using a BinaryMessageFormatter like this (and similarly on the receiving end):
using (MessageQueue queue = new MessageQueue(".\\Private$\\msmq1"))
{
queue.Formatter = new BinaryMessageFormatter();
using (Message message = new Message())
{
message.Body = "<html><body>my html here</body></html>;
message.Recoverable = true;
message.Formatter = new BinaryMessageFormatter();
message.TimeToBeReceived = TimeSpan.MaxValue;
queue.Send(message);
}
}
Or create a MsmqTransportObject with an Html String property and transfer that instead.
The XmlMessageFormatter makes no sense if both the send and receive ends are using .NET (in which case you can safely use BinaryMessageFormatter)