I have some Java code which sends out an email with code somewhat like the following: Actually i got Mimemessage from Httprequest param and in that mimemessage i'm going to append some content to existing body.
If Mimemessage is of Multipart content-type , i'm not facing any issue while sending message.
If the message is of text/plain and text/html content-type, the content-transfer encoding which i set didn't applied to body.
Based on this docs
Q: Even though JavaMail does all the encoding and decoding for me, I need to manually control the encoding for some body parts. A: In the rare case that you need to control the encoding, there are several ways to override JavaMail's default behavior. A simple approach is as follows. After creating the entire message, call msg.saveChanges() and then use something like mbp.setHeader("Content-Transfer-Encoding", "base64") to force base64 encoding for the given body part.
Another approach is to subclass MimeBodyPart and override the updateHeaders method so that it first calls super.updateHeaders() and then sets the Content-Transfer-Encoding header as above.
Applied above also. But it doesn't works for me.
InputStream ins = request.getInputStream();
MimeMessage msg = new MimeMessage(session,ins);
msg.setContent("some non-Ascii content","text/plain; charset="UTF-8"");
//Tried setheader before saveChanges() method, also doesn't work for me
//msg.setHeader("Content-Transfer-Encoding","base64");
msg.saveChanges();
//Now tried based on above docs after saveChanges method, also doesn't work
msg.setHeader("Content-Transfer-Encoding","base64");
please help to solve this.
You duplicated most of this question in your other post, and I answered part of it there.
You would probably be better off sending the content for the mail message in the http request, then creating a new message on the server based on that content, instead of trying to send a complete MIME message to the server that you then edit.
Related
I am using 'javax mail' to send emails, and I want to send emails with a signature in the body, but 'javax mail' doesn’t seem to support setting signatures, what should I do?
"Signatures" don't exist structurally. They're just text at the end of the body. You can prepare whatever content you like to go in the body before sending the message.
In one of my Java applications I have to forward e-mails. So, I get e-mails (plain text or multipart) with any content (maybe also attachments). I edit their subject, from- and to-header and send them via SMTP.
I already implemented this using Apache James Mime4j and Apache Commons Net but now I also have to append a footer/signature to the content of each e-mail.
Can I achieve this with Mime4j too? Would be great! How? If not: is there another way?
EDIT: Details after Wolfgang Fahl's comment:
Maybe I'm using the library the wrong way, but I am parsing the message as follows:
MessageBuilder messageBuilder = new DefaultMessageBuilder();
InputStream in = ...
Message m = messageBuilder.parseMessage(in);
Now I have an instance of Message and I can set it's subject, sender, etc. But Message does not provide a method setText() or something like that. Ok, there is getBody() but then I don't know how to manipulate the Body.
I've set up my GAE/Java project to receiving emails and it works pretty fine excepting it can not preserve the incoming mail's format(e.g. bold, italic, font size, text color, bulleted list...), and the content type of incoming mails are always "text/plain", as a result from the end user's view the mail content huddled and unreadable.
For example I send a formatted mail from Gmail, when I receiving the mail in GAE all formats is tripped off and leaves a bulk of plain text.
Is there any way I can get incoming mail type as HTML so the format would be preserved?
While sending the mail through server. Set the body content type text/html.
.
.
.
htmlPart = new MimeBodyPart();
htmlPart.setContent("<b>html content</b>", "text/html");
This should work for you..
Looks like a duplicate of this question and answer
Moreover, I am copying a few excerpts from Google App Engine Documentation here which says:
The message contains a subject, a plaintext body, and an optional HTML body.
It can also contain file attachments, as well as a limited set of headers.
And I am guessing the content type should be text/html
I am talking to a file upload service that accepts post data, not form data. By default, java's HttpURLConnection sets the Content-Type header to application/x-www-form-urlencoded. this is obviously wrong if i'm posting pure data.
I (the client) don't know the content type. I don't want the Content-Type header set at all. the service has a feature where it will guess at the content type (based on the file name, reading some data from the file, etc).
How do I unset a header? There's no remove header, and setting it to null doesn't change the value and setting it to the empty string results in the header being set with no value.
I haven't tested this approach but you can try this:
Extend HttpURLConnection and try by overriding its getContentHandler() and setContentHandler(...) methods. Most probably this should work as, you will take a look at code of getContentHandler().
Use Apache HttpClient instead of URLConnection
Use fluent Request to generate your request
use removeHeader()
What do you mean "i don't want the Content-Type header to set at all"?
The browser (or other http client) sends your post request to the server, so it has to inform the server which way it encoded the parameters.
If the Content-Type header is not set, on the server side you (= your server) won't be able to understand how to parse the received data.
If you didn't set Content-Type, the default value will be used.
You browser (or other http client) MUST do two things:
Send key/value pairs.
Inform the server how the key/value pairs were encoded.
So, it is impossible to completely get rid of this header.
I just accomplished this by setting the header to null.
connection.setRequestProperty(MY_HEADER, null);
When using javax.mail.*, I'm trying to send a message with the content encoded in both text/plain and text/html. How can I add both encodings to the MimeMessage?
Does setText override the previous text set? ie: if I do setText("", "text/plain") then setText("", "text/html"), will the secord call override the message text previously set or will they both be present in the message?
Q: How do I send mail with both plain text as well as HTML text so that each mail reader can choose the format appropriate for it?
A: You'll want to send a MIME multipart/alternative message. You construct such a message essentially the same way you construct a multipart/mixed message, using a MimeMultipart object constructed using new MimeMultipart("alternative"). You then insert the text/plain body part as the first part in the multpart and insert the text/html body part as the second part in the multipart. You'll need to construct the plain and html parts yourself to have appropriate content. See RFC2046 for details of the structure of such a message.
http://www.oracle.com/technetwork/java/faq-135477.html#sendmpa