java mail --how to convert String to message in java mail api? - jakarta-mail

i have to check for the attachments in the mail thrugh java. For that firstly getting the multipart object of the message through getcontent() as follows.
Multipart mp=(Multipart)msg.getContent()
but even if mail have attachment and the message part is simple text then it throws an exception as -- java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart
So how to get attachments for simple message mails..?
thanks..in advance.

You can use the MimeMessage constructor which accepts an InputStream.
Message msg = new MimeMessage(mySession,
new ByteArrayInputStream(myString.getBytes()));
See JavaMail documentation

Related

get JMSMessageID when use producerTemplate

In Camel,
ProducerTemplate producerTemplate = exchange.getContext().createProducerTemplate();
producerTemplate.sendBody("endpointqueue?includeSentJMSMessageID=true", ExchangePattern.InOnly, body);
I would need to get JMSMessageID that is returned from IBM MQ/ActiveMQ. I am looking at exchange values on debug mode but cannot find it. I can only find sessionID. Where is it stored and how to get it?
The Camel documentation says:
includeSentJMSMessageID - only applicable when sending to jms destination using InOnly. enabling this option will enrich the Camel Exchange with the actual JMSMessageID that was used by the JMS client when the message was sent to the JMS destination.
Is includeSentJMSMessageID different than my needs or am I missing something?
The doc says the JMS MessageID is available as header.
So you should be able to return it like this:
from("direct:queueMessage")
.to("jms://myqueue?includeSentJMSMessageID=true")
.setBody().simple("${header.JMSMessageID}");
And then send your message using:
String msgId = producerTemplate.requestBody("direct:queueMessage", body, String.class);

Override Message-ID by configuration

When I send email from my application using the default WildFly mail session, the auto-generated message ID gives away that my server is WildFly:
Message-ID: <524672585.11.1429091886393.JavaMail.wildfly#myserver.example.com>
For security reasons, I'd like to suppress or override the wildfly substring in the message ID.
Is there a configuration element or a system property to do that?
Answering my own question: The wildfly part of the message ID corresponds to the value of the user.name system property. My server happens to be running under a Linux user account named wildfly.
So one option would be to use a different user account. Alternatively, simply passing -Duser.name=foo to the WildFly start script is enough to change the message ID.
Upgrade to JavaMail 1.5.3. That official release has Bug 6496 -Message-Id leaks current user/hostname of the Java process marked as resolved.
Otherwise, the Message-ID computation uses the InternetAddress.getLocalAddress method which is including the username. You can set the mail.from session property to override including the O/S user name.
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.from", "------#bar.baz");
Session s = Session.getInstance(props);
MimeMessage m = new MimeMessage(s);
m.addFrom(InternetAddress.parse("foo#bar.baz"));
m.setText("");
m.saveChanges();
m.writeTo(System.out);
}
Which will output something like:
From: foo#bar.baz
Message-ID: <1688376486.0.1429814480627.JavaMail.------#bar.baz>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
If you are using the default session you can just add 'mail.from' to the system properties.

How to Print SOAP Message Request Body generated using GSOAP when calling service

I have a java Service and i am calling it from my C program . I generated stubs using GSOAP and when i call the service i get the error message as :
Error -21 in function "SOAPDataAdapter::getFieldValueFromXSDElement (XSD -> Dataset conversion)": Feature "12xsd__anyType" is not supported
N5vasco20InvalidDataExceptionE: Error -17 in function "SOAPDataAdapter::getVASCOAttributeFromGSOAP (Attempting to insert WSDL attribute 'User ID' (attribute ID 0) as a dataset value)": The supplied data contains invalid characters
My question is how can i print the Soap Message object that is going through GSOAP call . I want to compare it with my SOAPUI request which is working fine.
Any help will be highly appreciated.
Thanks
I was able to view the SOAP message using WIreShark

Extracting Salesforce data using Camel throws org.apache.camel.NoTypeConversionAvailableException

I am new to Apache Camel and working on a simple java program that can extract Salesforce custom object data using Apache Camel. While running the program it throws org.apache.camel.NoTypeConversionAvailableException. Objects QueryRecordsLine_Item__c and Line_Item__c implements Serializable interface. Kindly help me to fix this.
Code snippet:
from("direct:query")
.to("salesforce:query?sObjectQuery=SELECT name, Unit_Price__c, Units_sold__c, Merchandise__c, Invoice_Statement__c from Line_Item__c&sObjectClass=" + QueryRecordsLine_Item__c.class.getName())
.to("file:E:\\ajagan\\datafiles\\out");
Exception:
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.apache.camel.component.salesforce.dto.generated.QueryRecordsLine_Item__c to the required type: java.io.InputStream with value {"done":true,"totalSize":280,"records":[.....]}
at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:181)
at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99)
... 31 more
You should add camel-jaxb to the classpath, so Camel can covert the salesforce DTO objects (they are JAXB annottated) to XML representation in a stream, that the file endpoint can use.
How about you try converting to a string first:
from("direct:query")
.to(...)
.convertBodyTo(String.class)
.to(...)

ASIHTTPClasses and iOS6

My app has been working correctly on previous iOS versions. I just upgraded to iOS6 and now the app is returning an error while processing an XML request/response.
The app sends an XML request to a server and receives an XML response, but now the response is empty "<>" causing the NSXMLParser to fail.
Here is how the app is sending the request (this used to work, prior ios6):
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
//[request setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedData = [[NSMutableData data] retain];
}
But the response is now empty "<>".
parser error is Error Domain=NSXMLParserErrorDomain Code=9 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 9.)", path to file is :
/Users/ronluna/Library/Application Support/iPhone Simulator/6.0/Applications/5BA74D89-1967-4B9E-A1C0-B076F7BB9064/Documents/Menus.xml
Any ideas what could be the problem?
Hi ive also encountered the same problem. Try changing the format of your xml file to UTF-8 without BOM or if you are populating your xml on a server side script try changing the character set to UTF-8. Hope this helps.
make sure you create your NSData object with encoding specified in xml format.
i.e, if xml have utf-8 encoding, the you should have :
NSData* d = [xmlResponseStrings dataUsingEncoding:NSUTF8StringEncoding];
in your code,
other NSUnicodexxxxx encoding will not work.
Hope this help.

Resources