I'm using Java Mail to create emails, it's almost working but i'm facing a problem that i don'thave any idea how to resolve it.
The Content-Transfer-Encoding quoted-printable breaks my body in a lot of line with 77 characters each line and the problem happens when the next line starts and the first character is a . (dot).
An example of this:
<table border=3D"0" cellpadding=3D"0" cellspacing=3D"0" align=3D"center">
<tbody>
<tr>
<td><br /><font color=3D"#666666" face=3D"Arial, Helvetica, sans-serif=
" size=3D"1">Lala não leleler lala lalalaa, <a href=3D"http://t.laiu=
com.ar/TestsTrackings/op.aspx?Osa8Br5zxNpqrv0AtVqBIiGIGG0CPNrUoxbqY7WYcGhP7=
LrlPvlBijtUAlN+b07u4cgghR7erUuf
P9PWGu7YtTkb51txcLYb9+6jzjBtWhf/L8Ai/gdZjrXfmIamviwsffMsjXa8mtnQm8n/XXkWuDw=
8
gW6EpcofAgSMsqpqmqxv85MRVG2vIFuD9v6lFD1H+dMk0RtR/cMhg/zgtjdIym6pig8sSTDT">c=
lalal lala</a>.</font><br /></td>
</tr>
</tbody>
</table>
On line i have a link that starts with http://t.laiu.... and on next line it just removes my dot. When the user receive emails, he gots a link like t.laiucom.ar... instead t.laui.com.ar.
Anyone have an idea how can i avoid it?
Thank in advance.
In the comments you confirmed that you use Message.writeTo to create a file, and that the periods are there in that file.
So the problem is not javamail or the quoted printable encoding here.
The pickup service which picks up the file seems to already expect it to be fit for SMTP transport, as per rfc5321 (or rfc2821/rfc821), which means that periods at the beginning of a line must be doubled. Message.writeTo won't do that directly, because it does not care about the used transport, it just writes the message to a stream.
Usually, when sent to SMTP through javax.mail.Transport javamail handles this by wrapping the output stream in a SMTPOutputStream, so everything works fine. But by using Message.writeTo directly, you're operating on a lower level and need to deal with correctly formatting the output so it is accepted by the pickup service yourself.
That means you need to replace dots at the beginning of a line with two dots yourself. To do so you could use the SMTPOutputStream wrapper class mentioned above (but it's not public/documented API), or write your own stream wrapper which does the same. Or any other way to modify the generated data you like...
Related
I'm having difficulties getting an html page to pick up a rupee symbol (₹), store it into an SQL Server 2016 database and then retrieve it for display.
Important to note here is that I need to enter the actual symbol not the html version.
The basic flow of the page is that an administrator can add a new currency to the application via a web interface. There is a text box where
they would enter the actual rupee symbol ₹ and hit submit. This then passes the parameters via an HttpServletRequest to a java back end.
The java backend just inserts/updates this value to a SQL Server 2016 table in a field nchar(10).
When the page is refreshed it runs a select against this table and displays all the valid currencies.
The problem is that when the java application retrieves from the HttpServletRequest request object the symbol ₹ becomes â?¹. I can see this in the
debugger, I appreciate that this might be due to my debugger not being able to display this so I go forward.
The java (jdbc) updates the field. I view the field using Sql Server Management Studio and it displays â?¹ in both text and grid view.
I know that SSMS can dispay this symbol as I can insert it directly and it works. So it looks like the information is lost at the html>java request.
The web page itself is legacy and built using xslt. I have added some more details below of where I'm up to.
The website runs on tomcat 8 and the pages are built using xslt, the back end is java.
In the front end I have a text field in an EditCurrency page. I enter ₹ in the symbol field and hit submit.
The relevent fragments of the xslt page that is used to build the the front end are:
<!--header indicates page is utf8-->
<xsl:param name="csrfToken"/>
<xsl:param name="currencyFormatError"/>
...
<!-- on submission the EditCurrency java class is called. method=POST indicates it should allow UTF8 request URL's as is my understanding-->
<form id="cmanager" name="cmanager" onsubmit="return(vNewCurrency())" action="../servlet/webpay.website.admin.EditCurrency" method="POST">
<input name="csrfToken" type="hidden" value="{$csrfToken}"/>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
The tomcat 8 server's server.xml set to encoding UTF-8. I understand this allows the request/response to handle UTF-8:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
Java class EditCurrency:
//Retrieves symbol from the HttpServletRequest req
//symbol returns â?¹
String symbol = (String) getParameter(PARAM_SYMBOL);
I've also tried to set the HttpServletRequest req using the following but it does nothing:
try {
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException ex) {
java.util.logging.Logger.getLogger(EditCurrency.class.getName()).log(Level.SEVERE, null, ex);
}
Sql Server:
Value â?¹ appears in the nchar(10) field.
Display html:
â?¹ is displayed when the screen is refereshed with this updated value.
So question is how do I fix this up!!??
I had considered some sort of reference table of all currencies and their display values etc but it doesn't seem correct way of doing it.
Unless every tool in the chain, including whatever you use to look at the intermediate results, is UTF-8 capable you will see garbage at some point.
The Unicode code point of the Rupee symbol is 0x20B9, which is UTF-8 encoded as three bytes 0xE2 0x82 0xB9. If you attempt to display that in a tool that uses ISO-8859-1 you see
0xE2 = â
0x82 = ? (there is no character in ISO-8859-1 for code 0x82, so you see a question mark)
0xB9 = ¹
So it appears the symbol is correct in the database, but you are displaying it incorrectly.
To "fix" this problem you must ensure that your tools are all set to UTF-8, and that the web server is configured to include the UTF-8 declaration in the HTML it sends.
My java server works as follows:
http://locahost:5555/?search="java"
The above link would work fine. However, if I ever want to use "#" as part of search string, it all goes wrong. For example:
http://locahost:5555/?search="c#"
For some reason everything after "#" gets ignored. If I use the decoded version of "#" it works fine again. For example:
http://locahost:5555/?search="c%23"
The system should be used by people that don't understand url encoding so they would never put %23 instead of #. Is there anyway around it?
Other than encoding it there is no way around it. More over the string after # treats as the location of the URL.
String after # will not be passed to the server through GET parameters. Use POST method instead.
https://developer.mozilla.org/en-US/docs/Web/API/Window.location
the user supposedly should not access the url directly so if they put "c#" in the url there would be no process on the other hand you could use
<form action="yourcontroller" method="post">
<input type="text" name="txtSearch" />
<input type="submit" value="search"/>
</form>
with this, it will take care of the special characters like "#" you mentioned.
don't forget to catch the parameter in your controller
request.getParamter("txtSearch");
It is in the browser. The server never gets a request with the hashtag (#) symbol, just up to the symbol.
A javascript workaround is probably a bad idea.
I am sending emails using amazon java sdk. I have to send html template as mail. I have written a program for this and it is working fine. But now I am storing the whole html code in a single String. But whenever i need to edit the template, I have to edit the program (I mean the String variable). And also I have to take care the special characters like " \ ...etc in that html code. Please suggest me an elegant way to solve this issue.
Use a template engine for that and store your template externally either in class path or on a file system. Here is a question that may help you selecting one: https://stackoverflow.com/questions/2381619/best-template-engine-in-java
Use Apache Common Lang api's StringEscapeUtils#escapeHtml, It escapes the characters in a String using HTML entities and return a new escaped String, null if null string input.
For example:
"US" & "UK"
becomes:
"US" & "UK".
Check the Apache Velocity Project. You can create template for several things. From it's user-guide page
Velocity can be used to generate web pages, SQL, PostScript and other output from templates. It
can be used either as a standalone utility for generating source code and reports, or as an
integrated component of other systems.
You can use a VTL(Velocity Template Language) . A example from above link
<HTML>
<BODY>
Hello $customer.Name!
<table>
#foreach( $mud in $mudsOnSpecial )
#if ( $customer.hasPurchased($mud) )
<tr>
<td>
$flogger.getPromo( $mud )
</td>
</tr>
#end
#end
</table>
Better and easiest way is reading the html file line by line using simple file reading operation and append this each line to a single String. And also I found this solution (also a better one, if you are ready to add one more library file to your project) from SO.
I did a hello world web application in Java on Tomcat container. I have a query string
code=askdfjlskdfslsjdflksfjl#_=_
with underscores on both sides of = in the URL. When I tried to retrieve the query string in the servlet by request.getParameter("code"), I get only askdfjlskdfslsjdflksfjl. The part after # is missing.
How is this caused and how can I solve it?
That's because the part of the url after # is not a part of the query.
Section 3.4 of approprate RFC says:
The query component is indicated by the first question
mark ("?") character and terminated by a number sign ("#") character
or by the end of the URI.
The # is only interpreted by the browser, not the server. If you want to pass the # character to the server, you must URLEncode it.
Example:
URLEncoder.encode("code=askdfjlskdfslsjdflksfjl#=", "UTF-8");
Please read the percent encoding on Wikipedia. The # and = are reserved characters in URLs. Only unreserved characters can be used plain in URLs, all other characters are supposed to be URL-encoded. The URL-encoded value of a # is %23 and = is %3D. So this should do:
code=askdfjlskdfslsjdflksfjl%23_%3D_
If this actually originates from a HTML <a> link in some JSP like so:
some link
then you should actually have changed it to use JSTL's <c:url>:
<c:url var="servletUrlWithParam" value="servletUrl">
<c:param name="code" value="askdfjlskdfslsjdflksfjl#_=_" />
</c:url>
some link
so that it get generated as
some link
Note that this is not related to Java/Servlets per-se, this applies to every web application.
In my web application (my first with Java, Spring, OR Roo), I'm building a form that has nothing to do with any JPA objects, it's just a form. I really don't want to use JSTL to build my forms here, because there's no data backing for them at this point. I'm using tiles to assemble the pages, so the guts of this form comes from a view, but apart from that there's nothing JSPish about it; it's just a form.
Inside that form, I have a text area that I've written:
<textarea id="whatever" name="whatever"></textarea>
When that comes to the screen, the </textarea> tag is gone. Different browsers deal with that differently, up to and including swallowing up the whole rest of the body HTML inside the text area field.
So I tried putting some content inside that textarea. Spaces and line breaks don't change its behavior, but it appears that any non-space character does. If I go
<textarea>.</textarea>
... it respects my close textarea tag. But then of course my text area renders on the screen with a dot in it, which isn't what I want.
Is this a known issue? Am I doing something wrong?
EDIT:
#bozho: Here's a pertinent chunk of my jsp:
<div id="notes" class="detailPanel">
<div class="panelLabel">Notes</div>
<table >
<thead><tr><th>Date</th><th>By</th><th>Note</th></tr></thead>
<tbody id="notesBody"></tbody>
</table>
<textarea id="newNote" rows="5" cols="80" >.</textarea>
<button id="addNewNote" onClick="saveNote();">Add New Note</button>
</div>
Absolutely nothing fancy going on here (I populate the tbody with rows on the client, is why that's empty). Without the dot in the third-to-last line, the closing textarea tag does not come out in the resulting HTML.
EDIT2 (Solution):
This URL became googlable after hearing some key words from people responding here:
http://www.jroller.com/komu/entry/textareas_with_jspx
Turns out that when jspx pages are parsed, empty tags are collapsed into a single self-closing tag, which breaks text areas. The solution is to put an empty jsp:text in the middle:
<textarea><jsp:text /></textarea>
(Which is STAGGERINGLY stupid, but there it is.)
You are using jspx files right?
In general jspx remove something (or in your case it shorten it: check this: I expect that it addes a slash to the former opening tag, so it becomes: <textarea id="whatever" name="whatever"/> ) where it belives that is not needed. What exactly depends ona bit on the implementation.
So put a <jsp:text> tag in the text area tag to prevent it from "closing"
<jsp:text>
<textarea id="whatever" name="whatever"></textarea>
</jsp:text>
<textarea id="whatever" name="whatever"><jsp:text /></textarea>
for an more complex example have a look at this answer: websphere 7 (and Spring Roo) incompatible with javax.el.ELException