Sharing Text, i.e. the missing <CR> - c#

I am using "Sharing" to send text (string) by email.
the data I am sharing is created like this:
string data = "Log Data\n" + "========\n";
data += "Log Type : " + log.LogType.ToString() + "\n";
data += "Date/Time: " + log.TimeStamp.ToString( "MM/dd/yy - HH:mm:ss" ) + "\n";
data += "Result: " + log.Result.ToString() + "\n";
data += "Value1: " + log.Value1 + " Value2: " + log.Value2 + "\n";
data += "Note: " + log.Note + "\n\n";
the data is prepared and sent in an email. However the "\n" is not interpreted correctly neither before I send (by the Windows 8 mail program) or the receiving side GMail.
The data presented on the send side looks like this:
Log Data ======== Log Type : ToDo Date/Time: 06/05/12 - 08:00:00 Result: Normal Value1: 170 Value2: 0 Note: note 1
The ShareTextHandler is like this:
DataRequest request = e.Request;
request.Data.Properties.Title = "Share Data: ";
request.Data.Properties.Description = "Send Data to an email address";
request.Data.SetText( data );
Was the "\n" changed in Windows 8? or the SetText does something to remove it?
EitanB
UPdate....
Hi,
Tried to do it with HTML:
Tried some test data with HTML like this:
private string PrepareShareData()
{
string CR = System.Environment.NewLine;
string data = string.Empty;
data += "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Final//EN\">" + CR;
data += "<html>" + CR;
data += "<body>" + CR;
data += "<p>" + CR;
data += "Log Data" + "<br/>" + CR;
data += "========" + "<br/>" + CR;
data += "Line 1" + "<br/>" + CR;
data += "Line 2" + "<br/>" + CR;
data += "<p>" + CR;
data += "</body>" + CR;
data += "</html>" + CR;
return data;
}
Did not work...
Tried also:
private string PrepareShareData()
{
string CR = System.Environment.NewLine;
string data = string.Empty;
data += "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Final//EN\">";
data += "<html>";
data += "<body>";
data += "<p>";
data += "Log Data" + "<br/>";
data += "========" + "<br/>";
data += "Line 1" + "<br/>";
data += "Line 2" + "<br/>";
data += "<p>";
data += "</body>";
data += "</html>";
return data;
}
did not work either....
I did create a .HTML file with the notepad that worked fine:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN">
<html>
<body>
<p>
Log Data<br/>
========<br/>
Line 1<br/>
Line 2<br/>
</p>
</body>
</html>
Is there a mail problem with HTML too or my HTML is wrong?
Thanks,
EitanB

Used:
var htmlFormat = Windows.ApplicationModel.DataTransfer.HtmlFormatHelper.CreateHtmlFormat( data );
request.Data.SetHtmlFormat( htmlFormat );
Where data is:
string data = string.Empty;
string BR = #"<br/>";
data += #"<html>";
data += #"<head><title>";
data += #"<title>";
data += #"Log Data<br/>";
data += #"========<br/>";
data += #"</title>";
data += #"</head>";
data += #"<body>";
data += #"Date/Time: " + log.TimeStamp.ToString( "MM/dd/yy - HH:mm:ss" ) + BR;
data += #"Note: " + log.Note + BR + BR;
data += #"</body>";
data += #"</html>";
too much work to go around Microsoft problem....
EitanB

try args.Request.Data.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(html));

Environment.Newline is certainly a step in the right direction, but I don't know if it will solve your problems.
This was an issue in the inbuilt mail app in Consumer Preview (and I haven't checked if it was fixed in RTM). I noticed that it didn't seem to want to respect line breaks. I confirmed that the correct data was being sent by writing a small dummy share target and ensuring that the text was rendered correctly and it was. Maybe you could send HTML in the share request - mail supports HTML as well as Text.

First of all as a general rule of thumb we should always be using a StringBuilder object to be appending strings together. The reason for this is: the string variable is immutable so every time we add a "+" sign we actually end up creating an entirely new instance of the string variable.
string data = string.Empty;
data += "Some Text Creates 1 variable";
data += "This creates a second variable" + "and this a third";
return data;
is better suited as
StringBuilder builder = new StringBuilder()
builder.Append("Some Text Creates 1 variable");
builder.Append("Since we havent yet created a string no 2nd variable is created");
builder.AppendLine("Should create a return before this line");
builder.AppendLine(Environment.NewLine);
builder.AppendLine("Should have an empty line above it");
return builder.ToString();
// only creates 1 string variable
Or you could wrap the single variable in the HTML Encode method described
return System.Net.WebUtillity.UrlEncode(builder.ToString());

Related

Using apostrophe in email subject while sent email via gmail api creating an issue

While sent email using below subject which apostrophe replacing with another characters
Actual Subject : We’ll make 100,800 cold calls for you
Mail Shows Subject : We’ll make 100,800 cold calls for you
Issue happens when I'm sent email via api , when sent email from SMTP it's working fine
Please check my api code below
string msg = "From: " + FromName + "<" + From + ">" + " \r\n" +
"To: " + ToName + "<" + To + ">" + " \r\n" +
"BCC: " + BCCEmail + " \r\n" +
"Subject: " + Subject + " \r\n" +
"Message-ID: mID_" + messageID + "\r\n" +
"References: "+encryptMessageID + "\r\n" +
"In-Reply-To: " + encryptMessageID + "\r\n" +
"Content-Type: " + contentType + "; charset=us-ascii\r\n\r\n" + Body;
dynamic objSendMsg = new { raw = commonFunction.Base64UrlEncode(msg) };
if (!string.IsNullOrEmpty(messageThreadID))
objSendMsg = new { raw = commonFunction.Base64UrlEncode(msg), threadId = messageThreadID };
var _objSendMsg = JsonConvert.SerializeObject(objSendMsg);
var strSendMsg = new StringContent(_objSendMsg, UnicodeEncoding.UTF8, "application/json");
When same content i'm applying in body with apostrophe working fine for body
Please check attached screenshot
Email copy
You need to base64_encode of the subject header your sending it as plain text. the API is getting confused.
Subject: " + Convert.ToBase64String(Subject) + " \r\n" +

MailKit receive emails doesn't show message.Body

I'm receiving emails OK, except that message.TextBody is showing a blank when there is a message present.
message.HtmlBody shows the body text amongst a whole lot of html stuff, obviously, but I'm looking for message.TextBody.
message.TextBody.ToString() shows an error
Object reference not set to an instance of an object
I'm using the following code:
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
client.Connect("pop.gmail.com", 995, true);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate("aaaa#gmail.com", "ssss");
gstrEmailMessages = gstrEmailMessages + client.Count + "\n";
//Fetch emails:
for (int i = 0; i < client.Count; i++)
{
var message = client.GetMessage(i);
gstrEmailMessages = gstrEmailMessages + "Subject: " + message.Subject + "\n";
gstrEmailMessages = gstrEmailMessages + "TextBody: " + message.TextBody + "\n";
gstrEmailMessages = gstrEmailMessages + "HtmlBody: " + message.HtmlBody + "\n";
}
//Disconnect connection:
client.Disconnect(true);
Why does message.TextBody show a blank?
Not all messages will have both an HTML and a plain-text body. In fact, it's possible for some messages to have neither.
In general, though, most messages will have at least 1 of those.

Properly formatted text output to .txt file

I'am exporting some data to a .txt file as follows:
String content;
String path=#"e:\coding\";
String name="test.txt";
path+=name;
System.IO.File.Delete(path);
for (i=0;i<row-1;i++)
{
try
{
if (r[i].points.Count() > 2)
{
content = "Route " + (i + 1).ToString() +" Truck_id:"+trk[i].truck_name.ToString()+ " Max_load="+trk[i].capacity.ToString()+ "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
System.IO.File.AppendAllText(path, "Points Load Reached_AT Max_load" + Environment.NewLine);
System.IO.File.AppendAllText(path, "========================================" + Environment.NewLine);
for (int j = 0; j < (r[i].points.Count()); j++)
{
content = r[i].points[j].ToString() + " " + c[r[i].points[j]].load.ToString() +" "+ r[i].time_list[j].ToString()+" "+c[r[i].points[j]].max_load.ToString()+"\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
}
content = "Total " + r[i].ld.ToString() + "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine );
content = "Route Complete: " + r[i].reach_at.ToString();
System.IO.File.AppendAllText(path, content + Environment.NewLine+Environment.NewLine);
}
}
catch (IndexOutOfRangeException e)
{
break;
}
}
As expected the output I get is not properly formatted:
The spaces are causing the text to be jumbled and not arranged. My reputation does'nt allow me to post a screenshot but I guess It can be understood what is happening.
Is there way so that the text is properly formatted neatly column wise without looking jumbled.
If you need a text, you can use tabs:
System.IO.File.AppendAllText(path, "Points\t\tLoad\t\tReached_AT\t\tMax_load" + Environment.NewLine);
// ...
content = r[i].points[j].ToString() + "\t\t " + c[r[i].points[j]].load.ToString() +"\t\t"+ r[i].time_list[j].ToString()+"\t\t"+c[r[i].points[j]].max_load.ToString()+"\n";
Just play with amount of tabs (\t for one, \t\t for two, etc...). Hope it can help.
Another solution would be to use commas:
System.IO.File.AppendAllText(path, "Points,Load,Reached_AT,Max_load" + Environment.NewLine);
and save to CSV-file (comma-separated values). Then you can import the data to Microsoft Excel or to other software.
You can find bunch full of good information on how to format the string contents in the The format item MSDN but for quick answer, an example for your string
content = "Route " + (i + 1).ToString() + " Truck_id:" + trk[i].truck_name.ToString() + " Max_load=" + trk[i].capacity.ToString() + "\n";
If we assume,
i maximum 10 digits,
Truck_name max 45 characters
capacity max 10 digits
content = String.Format("{0,-20}{1,55}{2,20} " + Environment.NewLine, "Route " + (i + 1).ToString(), " Truck_id:" + trk[i].truck_name.ToString(), " Max_load=" + trk[i].capacity.ToString());

New line in textbox

I have a textbox1 that lets the user input a text, a button that adds the text to textbox2.
this is my code but it doesnt create a new line when I add another text.
string date = DateTime.Now.ToString();
txt_details.Text = date + " " + txt_summary.Text.ToString() + Environment.NewLine + Environment.NewLine ;
Notice the += operator.
txt_details.Text += "\n" + date + " " + txt_summary.Text.ToString();
Looks like you should be appending (use +=); instead you are overwriting.
string date = DateTime.Now.ToString();
txt_details.Text += date + " " + txt_summary.Text.ToString() + Environment.NewLine + Environment.NewLine
Make sure Multiline is enabled.
Ensure that TextBox.Multiline property is set to true
txt_details.Multiline = true;

Jquery dynamic TextBoxes with Request.form Iteration and Save to DB

i am Adding multiple TextBoxes with Jquery in my Application, then in code behind file i want can access the values by Request.form[name]. I want to iterate these textboxes and read values of whatever Text is entered by the user, so i can store it in database.
any idea how can i save the value of these textboxes in Database spliting each textbox values by a Comma(,)
Please guide me how to get all these textbox values in loop and then save them in DB Table
$(document).ready(function () {
var counter = 2;
$(\"#addButton\").click(function () {
if (counter > 10) {
alert(\"Only 10 textboxes allow\");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr(\"id\", 'TextBoxDiv' + counter);
newTextBoxDiv.html('<table><tr><td><input type=\"text\" name=\"textbox' + counter +
'\" id=\"textbox' + counter + '\" value=\"\" ></td><td><input type=\"text\" name=\"textbox' + counter +
'\" id=\"textbox' + counter + '\" value=\"\" ></td><td><input type=\"text\" name=\"textbox' + counter +
'\" id=\"textbox' + counter + '\" value=\"\" ></td></tr></table>');
newTextBoxDiv.appendTo(\"#TextBoxesGroup\");
return false;
counter++;
});
Probably you have do something like this, you could easily debug by using chrome debugger tool to retrieve all values from your input boxes.
$('table tr td').each(function() {
var values = "";
values = $(this).find('input').val() + ",";
});

Categories