Author |
Message
|
xiaojy822 |
Posted: Thu Dec 08, 2011 7:43 am Post subject: Garbled problem during sending Email by Email output |
|
|
Newbie
Joined: 07 Dec 2011 Posts: 7
|
Hi,All
I try to send a dynamic email by Email output node.but I met a problem.
the problem is when the content of email which I try to send is normal when there is no attachment.but when the email has attachment the content of email is garbled.Accurate to say is when add any of nodes which is related to attachment such as "attachmentContent attachmentContentType attachmentContentName attachmentContentEncoding".the content of the mail is become garbled.by the way the character in the content is not English.here is some of my codes:
Code: |
MbElement destination=newEnv.getRootElement().getFirstElementByPath("./Destination");
MbElement attachmentContent=attachment.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"Content ",null);
MbElement attachmentContentType=attachment.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"ContentType",null);
MbElement attachmentContentName=attachment.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"ContentName",null);
MbElement attachmentContentEncoding=attachment.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"ContentEncoding",null);
MbElement outParser = outRoot.createElementAsLastChild(MbBLOB.PARSER_NAME);
outParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB", content.getBytes("UTF-8")); |
please tell me how can solve this problem?THX |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Dec 08, 2011 7:48 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
First step is to understand what the MIME fields you are setting to NULL are supposed to accomplish, and then stop setting them to null and set them to the correct values. In particular ContentEncoding and ContentType have a strong influence on how email servers treat the data of the associated MIME part (for example, deciding not to treat it as 7-bit ASCII text).
Otherwise you haven't included enough code to point out any further errors. Like you didn't show where "attachment" exists, nor do you show how you populate the actual data of the attachment part. |
|
Back to top |
|
 |
xiaojy822 |
Posted: Fri Dec 09, 2011 2:33 am Post subject: |
|
|
Newbie
Joined: 07 Dec 2011 Posts: 7
|
The whole flow is this:I fill some information which contains receiver,the subject,content and attachement in a webpage form.then the webpage form submit those information in a HttpInput node.HttpInput transmission the informmation to a JavaComput node.In the javaComput I rebuild the Message.then transmission the message in a EmailOutput node.the code which I wrote in javacompute as follow:
Code: |
MbElement inRoot = inMessage.getRootElement();
MbElement inMime = inRoot.getFirstElementByPath("MIME");
MbElement inParts = inMime.getFirstElementByPath("Parts");
MbElement outRoot = outMessage.getRootElement();
MbElement emailOutputHeader = outRoot.createElementAsLastChild(MbElement.TYPE_NAME, "EmailOutputHeader", null);
MbElement receiver = emailOutputHeader.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"To","");
MbElement cc = emailOutputHeader.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"Cc","");
MbElement bcc=emailOutputHeader.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"Bcc","");
MbElement from = emailOutputHeader.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"From","xjy822@126.com");
MbElement replyTo = emailOutputHeader.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"Reply-To","");
MbElement subject= emailOutputHeader.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"Subject","");
MbMessage env = contact admin.getLocalEnvironment();
MbMessage newEnv = new MbMessage(env);
MbElement destination=newEnv.getRootElement().getFirstElementByPath("./Destination");
MbElement email = destination.createElementAsLastChild(MbElement.TYPE_NAME, "Email", null);
MbElement attachment = email.createElementAsLastChild(MbElement.TYPE_NAME, "Attachment", null);
MbElement attachmentContentName=attachment.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"ContentName",null);
MbElement attachmentContent=attachment.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"Content ",null);
MbElement attachmentContentType=attachment.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"ContentType",null);
String xheader = null;
String dataString = null;
MbElement part = inParts.getFirstChild();
while (part != null) {
String content = (String)part.getFirstChild().getValue();
int start = content.indexOf("\"") + 1;
int end = content.indexOf("\"", start);
String property = content.substring(start, end);
MbElement data = part.getLastChild().getFirstElementByPath("BLOB");
byte[] blobData = (byte[])data.getFirstElementByPath("BLOB").getValue();
if (!property.equals("attachement"))dataString = new String(blobData);
if (property.equals("receiver")) receiver.setValue(dataString);
if (property.equals("subject")) subject.setValue(dataString);
if (property.equals("content")){
MbElement outParser = outRoot.createElementAsLastChild(MbBLOB.PARSER_NAME);
try {
outParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB",dataString.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
if (property.equals("attachement")) {
attachmentContent.setValue(blobData);
attachmentContentType.setValue("text/plain");
attachmentContentName.setValue("1.txt");
attachmentContentEncoding.setValue("7bit");
} part = part.getNextSibling();
}
MbMessageAssembly outAssembly = new MbMessageAssembly(
contact admin,
newEnv,
contact admin.getExceptionList(),
outMessage); |
|
|
Back to top |
|
 |
mqjeff |
Posted: Fri Dec 09, 2011 2:53 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
It would be nice if you edited your message to wrap the java in [ c o d e ] tags.
it's still readable, but it's nicer in code tags.
You are setting the content encoding of your attachment to 7-bit. Is that correct?
The email message that has garbled stuff - is it the body of the email that is garbled or the attachment that is garbled? |
|
Back to top |
|
 |
xiaojy822 |
Posted: Fri Dec 09, 2011 3:01 am Post subject: |
|
|
Newbie
Joined: 07 Dec 2011 Posts: 7
|
I try to let the attachmentContentEncoding become 7bit,base64 and quoted-printable but is still not worked.the result is the subject is normal,but the content of the email is garbled.and now I must cover this case first. |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Dec 09, 2011 3:13 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Again, please be specific.
Is it the content of the attachment that is garbled, or the contents of the body?
These are separate mime-parts with separate content-types and separate content-encodings. |
|
Back to top |
|
 |
xiaojy822 |
Posted: Fri Dec 09, 2011 3:16 am Post subject: |
|
|
Newbie
Joined: 07 Dec 2011 Posts: 7
|
the contents of the body is garbled. |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Dec 09, 2011 3:45 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Okay.
So the body content-encoding and/or content-type is not correct, not the one associated with the attachment.
So make sure you set Destination.Email.BodyContentType to the correct thing, probably even including a charset string. |
|
Back to top |
|
 |
xiaojy822 |
Posted: Fri Dec 09, 2011 5:55 am Post subject: |
|
|
Newbie
Joined: 07 Dec 2011 Posts: 7
|
can tell me more detail like what value is setted may cover this problem.
the character which become Garbled is Chinese character.and the character of the webpage which is used for filling information is UTF-8 |
|
Back to top |
|
 |
fjb_saper |
Posted: Fri Dec 09, 2011 8:02 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
xiaojy822 wrote: |
can tell me more detail like what value is setted may cover this problem.
the character which become Garbled is Chinese character.and the character of the webpage which is used for filling information is UTF-8 |
So you need to follow mqjeff's advice and make sure the message body text is encoded with UTF-8.
Have fun  _________________ MQ & Broker admin |
|
Back to top |
|
 |
xiaojy822 |
Posted: Fri Dec 09, 2011 9:49 pm Post subject: |
|
|
Newbie
Joined: 07 Dec 2011 Posts: 7
|
I already make the encoding of message body text is UTF-8 by using the code as fellow:
MbElement outParser = outRoot.createElementAsLastChild(MbBLOB.PARSER_NAME);
outParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB",dataString.getBytes("UTF-8"));
but it's not worked and what I want to know is whether it is correct to make the encoding of message body text is UTF-8 in this way.If this way is incorrect I hope someone can tell me the right way.THX |
|
Back to top |
|
 |
xiaojy822 |
Posted: Sat Dec 10, 2011 7:21 am Post subject: |
|
|
Newbie
Joined: 07 Dec 2011 Posts: 7
|
Fianlly I cover this case the solution code as fellow:
String content= new String(dataString.getBytes("GB2312"),"8859_1");
outParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB", content.getBytes("UTF-8")); |
|
Back to top |
|
 |
rekarm01 |
Posted: Sat Dec 10, 2011 1:33 pm Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
Without knowing what the input bytes look like, it's difficult to guess how they're encoded.
xiaojy822 wrote: |
Fianlly I cover this case the solution code as fellow:
Code: |
String content= new String(dataString.getBytes("GB2312"),"8859_1"); |
|
The getBytes() method will write the bytes, encoded as "GB2312", and the String() constructor will read the bytes back, as if they were encoded using "8859_1". If that works at all, it's likely because it's discarding all the non-ASCII characters.
Either that, or this bug is undoing the effects of a previous bug -- most likely here:
xiaojy822 wrote: |
Code: |
if (!property.equals("attachement"))dataString = new String(blobData); |
|
Confirm the correct spelling for "attachement", and add a charsetName to the String() constructor that matches the blobData encoding. |
|
Back to top |
|
 |
|