MSMQ - Message with Html - 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)

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

EWS - FileAttachment Content is empty / byte[0]

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.

Get HTML from Frame using WebBrowser control - unauthorizedaccessexception

I'm looking for a free tool or dlls that I can use to write my own code in .NET to process some web requests.
Let's say I have a URL with some query string parameters similar to http://www.example.com?param=1 and when I use it in a browser several redirects occur and eventually HTML is rendered that has a frameset and a frame's inner html contains a table with data that I need. I want to store this data in the external file in a CSV format. Obviously the data is different depending on the querystring parameter param. Let's say I want to run the application and generate 1000 CSV files for param values from 1 to 1000.
I have good knowledge in .NET, javascript, HTML, but the main problem is how to get the final HTML in the server code.
What I tried is I created a new Form Application, added a webbrowser control and used code like this:
private void FormMain_Shown(object sender, EventArgs e)
{
var param = 1; //test
var url = string.Format(Constants.URL_PATTERN, param);
WebBrowserMain.Navigated += WebBrowserMain_Navigated;
WebBrowserMain.Navigate(url);
}
void WebBrowserMain_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (e.Url.OriginalString == Constants.FINAL_URL)
{
var document = WebBrowserMain.Document.Window.Frames[0].Document;
}
}
But unfortunately I receieve unauthorizedaccessexception because probably frame and the document are in different domains. Does anybody has an idea of how to work around this and maybe another brand new approach to implement functionality like this?
Thanks to the Noseratio's comments I managed to do that with the WebBrowser control. Here are some major points that might help others who have similar questions:
1) DocumentCompleted event should be used. For Navigated event body of the document is NULL.
2) Following answer helped a lot: WebBrowserControl: UnauthorizedAccessException when accessing property of a Frame
3) I was not aware about IHTMLWindow2 similar interfaces, for them to work correctly I added references to following COM libs: Microsoft Internet Controls (SHDocVw), Microsoft HTML Object Library (MSHTML).
4) I grabbed the html of the frame with the following code:
void WebBrowserMain_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.OriginalString == Constants.FINAL_URL)
{
try
{
var doc = (IHTMLDocument2) WebBrowserMain.Document.DomDocument;
var frame = (IHTMLWindow2) doc.frames.item(0);
var document = CrossFrameIE.GetDocumentFromWindow(frame);
var html = document.body.outerHTML;
var dataParser = new DataParser(html);
//my logic here
}
5) For the work with Html, I used the fine HTML Agility Pack that has some pretty good XPath search.

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

Is it possible to embed images in e-mail message in Sharepoint?

Currently I'm sending E-Mail messages by SPUtility.SendMail. I'd like to embed images into my message so i can give it a little bit style (div backgrounds, logo images etc.).
Is this possible?
P.S. I can't give direct URL addresses to image SRCs because the resources are located in a site which belongs to a private network which requires authentication for accessing to the files.
Edit:
I did some research before asking here, ofcourse the first thing i encountered was the System.Net.Mail (do you know that there is a whole web site devoted to it). But the Sharepoint Deployment team in my client's company has some strict rules about custom coding. They have coding guide lines and everything. I'm trying to stick with the SP SDK as hard as i can.
The most straighforward way for me has been through using System.Net.Mail, since you can inline your own content.
Here's a sample usage
using (MailMessage msg = new MailMessage("fromaddress", "toaddress"))
{
msg.Subject = "subject";
msg.Body = "content";
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp server name");
smtp.Send(msg);
}
Same concept applies to using SPUtility.SendMail (aside from the fact that you'll need a reference to your SPWeb:
From http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.sendemail.aspx
try
{
SPWeb thisWeb = SPControl.GetContextWeb(Context);
string toField = "someone#microsoft.com";
string subject = "Test Message";
string body = "Message sent from SharePoint";
bool success = SPUtility.SendEmail(thisWeb,true, false, toField, subject, body);
}
catch (Exception ex)
{
// handle exception
}
The second boolean parameter in SendMail is false to disable HTML encoding, so you can use your <img > and <div > tags in the message body.