Javamail - How can I send html and text inline without attach? - html

I would like to send a html and a text content inline, but just one appears inline, the other appears as an attached file.
My code:
MimeMultipart multipart = new MimeMultipart();
String html = "<font size=\"5\">Test HTML</font>";
String text = "Test text + html";
BodyPart bodyparty = new MimeBodyPart();
bodyparty.setContent(text, "text/plain");
multipart.addBodyPart(bodyparty);
bodyparty = new MimeBodyPart();
bodyparty.setContent(html, "text/html");
multipart.addBodyPart(bodyparty);
message.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(user_auth, user_password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Is it possible?
Thanks!

It's unlikely any mail client is going to display that the way you want. Why not embed the plain text part in the html part using <pre> or something like that?

Related

Use JSP & HTML to read file and change display options?

I want to import a text file (not user import), for example:
Coding
There are several languages...
When the file is read, the first line should be in larger font and bolded, and the other lines can be kept in the text file format. Not sure how I can use JSP and link it to HTML
You've left out a lot of important details (Q: How is the JSP getting invoked? Q: Are you passing a filename in the URL? Etc. etc.)
But here's a simple example that might get you started in the right direction:
<%
BufferedReader reader = new BufferedReader(new FileReader("myile.txt"));
boolean firstLine = true;
String line;
while((line = reader.readLine())!= null){
if (firstLine) {
line = "<b>" + line + "</b>";
firstLine = false;
}
out.println(line);
}
reader.close();
%>

Html not rendering for MailKit email

I'm trying to send transaction emails from my website using the MailKit library but I can't make the html render an anchor tag. In fact I'm not sure that any of the html is being rendered. Any ideas what's wrong? My code is below.
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Eternal Meta", "eternal.meta.transactions#gmail.com"));
message.To.Add(new MailboxAddress(user.Username, user.Email));
message.Subject = "Eternal Meta - Registration";
var builder = new BodyBuilder();
builder.HtmlBody = "<p>Use the link below to activate your account</p><br>Click Me";
message.Body = builder.ToMessageBody();
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, false);
client.Authenticate("eternal.meta.transactions#gmail.com", "password");
client.Send(message);
client.Disconnect(true);
}
Use a fully qualified URL for the "href" attribute (like https://[...]).
The mail client can parse the HTML inside your message and most probably the "href" attribute gets removed as the URL is considered relative.

iText HtmlWorker Pre tag rendering issue

I am newbie to iText. I am trying convert html file to pdf.After conversion the contents inside the "pre tag" is not proper. If anybody come across this issue before please share your thought on this with the solution that you applied.
Document document = new Document();
string filePath = HostingEnvironment.MapPath("~/Content/Pdf/");
PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+Name+".pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
FontFactory.Register(Path.Combine(_webHelper.MapPath("~/App_Data/Pdf/arial.ttf")), "Garamond"); // just give a path of arial.ttf
StyleSheet css = new StyleSheet();
css.LoadTagStyle("body", "face", "Garamond");
css.LoadTagStyle("body", "encoding", "Identity-H");
css.LoadTagStyle("body", "size", "12pt");
hw.SetStyleSheet(css);
hw.Parse(new StringReader(htmlText));
in above code see there is htmlText is a html code in string format plase pass your html viewpage code in string format and use above code, your pdf will be generate.
please note me if this code is not work.
hope this helps.

How to insert html in Microsoft Word Placeholder

I have this situation:
The user has an editor on his page and he enters text(with colors, formating, hyperlinks and he can also add pictures). When he clicks Submit the data from the editor(with the proper formating) must be sent to a specific placeholder in a Microsoft Office Word document.
I am using OpenXml SDK to write in the document and I tried HtmlToOpenXml so I can read the html.
I use HtmlToOpenXml and from the html string(from the user) I det a couple of paragraphs and now I have to insert them in the content control. Do you know how can I find the control and append them in it(if possible)
I managed to fix this and here is the code I used
//name of the file which will be saved
const string filename = "test.docx";
//html string to be inserted and rendered in the word document
string html = #"<b>Test</b>";
//the Html2OpenXML dll supports all the common html tags
//open the template document with the content controls in it(in my case I used Richtext Field Content Control)
byte[] byteArray = File.ReadAllBytes("..."); // template path
using (MemoryStream generatedDocument = new MemoryStream())
{
generatedDocument.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(generatedDocument, true))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
//just in case
if (mainPart == null)
{
mainPart = doc.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
}
HtmlConverter converter = new HtmlConverter(mainPart);
Body body = mainPart.Document.Body;
//sdtElement is the Content Control we need.
//Html is the name of the placeholder we are looking for
SdtElement sdtElement = doc.MainDocumentPart.Document.Descendants<SdtElement>()
.Where(
element =>
element.SdtProperties.GetFirstChild<SdtAlias>() != null &&
element.SdtProperties.GetFirstChild<SdtAlias>().Val == "Html").FirstOrDefault();
//the HtmlConverter returns a set of paragraphs.
//in them we have the data which we want to insert in the document with it's formating
//After that we just need to append all paragraphs to the Content Control and save the document
var paragraphs = converter.Parse(html);
for (int i = 0; i < paragraphs.Count; i++)
{
sdtElement.Append(paragraphs[i]);
}
mainPart.Document.Save();
}
File.WriteAllBytes(filename, generatedDocument.ToArray());
}

AJAX HTMLEditorExtender on postback tables don't display

I am currently using an Ajax tool; HTMLEditorExtender to turn a textbox into a WYSIWYG editor, in a C# ASP.NET project. On the initial page load I place a large amount of formated text and tables into the editor which appears fine; even the tables.
The data is loaded into an asp:panel and the items/display from the panel is what is actually loaded into the extender and displayed.
However, if I want to have a button that saves all of the data that is in the editor to a Session and after the button press still display everything in the WYSIWG editor on the page postback everything that loads in the the textbox is fine except for the tables. They come up with the tags. Is there anyway around this?
The code I am using to initially load the page is this:
ContentPlaceHolder cphMain = (ContentPlaceHolder)this.Master.FindControl("MainContent");
Panel pnlContent = (Panel)cphMain.FindControl("innerFrame");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlContent.RenderControl(hw);
txtPN.Text = sb.ToString();
pnlContent.Visible = false;
On the button click I am having this saved:
string strHTMLText = txtPN.Text;
Session["ProgressNoteHTML"] = strHTMLText;
And I am loading it on the postback like this:
txtPN.Text = (string)Session["ProgressNoteHTML"];
ContentPlaceHolder cphMain = (ContentPlaceHolder)this.Master.FindControl("MainContent");
Panel pnlContent = (Panel)cphMain.FindControl("innerFrame");
pnlContent.Visible = false;
Any ideas as to why any postbacks would make the tags appear and in the original page load they do not?
The solution offered by Erik won't work for table tags containing property values. For instance: <table align="right"> will not be decoded. I have also found that <img> tags are encoded by the HTMLEditorExtender as well.
The easier solution is to use the Server.HTMLDecode() method.
TextBox_Editor.Text = Server.HtmlDecode(TextBox_Editor.Text) 'fixes encoding bug in ajax:HTMLEditor
I have the same problem, It seems to have something to do with the default sanitizing that the extension performs on the HTML content. I haven't found a way to switch it off, but the workaround is pretty simple.
Write an Anti-Sanitizing function that replaces the cleansed tags with proper tags. Below is mine written in VB.Net. A C# version would look very similar:
Protected Function FixTableTags(ByVal input As String) As String
'find all the matching cleansed tags and replace them with correct tags.
Dim output As String = input
'replace Cleansed table tags.
output = output.Replace("<table>", "<table>")
output = output.Replace("</table>", "</table>")
output = output.Replace("<tbody>", "<tbody>")
output = output.Replace("</tbody>", "</tbody>")
output = output.Replace("<tr>", "<tr>")
output = output.Replace("<td>", "<td>")
output = output.Replace("</td>", "</td>")
output = output.Replace("</tr>", "</tr>")
Return output
End Function