JavaMail Message getHeader() bug - android

I am writing an Android GMail client application.
When it creates a message, I add a header to it like this:
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(username));
msg.setSubject(subject);
msg.setText(message);
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
long time = someTime;
msg.addHeader("My_Header", Long.toString(time));
//IMAPFolder f declaration & initialization
f.open(Folder.READ_WRITE);
f.addMessages(new Message[]{msg});
f.close(true);
It all works fine and I am able to see the header correctly added to the message when viewing my GMail account on my PC. However, when I later try to retrieve the header information, it gets very weird.
When I call
String[] str = msg.getHeader("My_Header");
getHeader() returns null if I am running the app. However, when I debug the app and set a breakpoint before the getHeader() call, it returns the header value corrently.
My source code:
MimeMessage msg = getNextMessage();
String subject = msg.getSubject();
InternetAddresses to[] = msg.getAllRecipients()
String when[] = msg.getHeader(GMailClient.TIME_TO_SEND);
if (when == null) {
Log.i(TAG, "Null Header");
} else {
long time = Long.parseLong(when[0]);
Log.i(TAG, "Value retrieved: " + when[0]);
}

Possibly Gmail isn't returning the header information correctly via IMAP? You might want to look at the protocol trace to see exactly what the server is returning for your request.
Also, try writing out the entire message using (e.g.)
msg.writeTo(new FileOutputStream("msg.txt"));
and see if the header is there. If it is, and the protocol trace shows that it's not being returned for the getHeader call, it's a bug in the server. In that case, see the JavaMail FAQ entry for how to work around such server bugs.

Related

How to get "SIP header of incoming call" in PJSUA2?

I'm developing an android app which supports SIP using pjsip2.7 and pjsua2. I needed to set custom header to my sip call, which I had done, and I set Asterisk 13 Server to forward my header to other party. I could see in logs that my custom header is received but I cant use this header in receiver. I need a way to get this parameters I sent in header.
Is there any way to do that ?
I am using pjsua2 with golang instead of C++ and I had the same problem. Although, I didn't find the best way to do it, I found one that works.
First at all, you need to implement one account which extends to the pjsua2 account. Then, overwritte the OnIncomingCall callback in order to use the OnIncomingCallParam, I have used this code to get the header (it's in golang)
func (acc *MyAccount) OnIncomingCall(prm pjsua2.OnIncomingCallParam) {
msg := prm.GetRdata().GetWholeMsg()
msgindex := strings.Index(msg, "NEWHEADER")
if msgindex != -1 {
cabecera = true
msg = msg[msgindex:]
msg = msg[strings.Index(msg, ":")+1:]
msgindex = strings.Index(msg, "Content-Type")
msg = msg[:msgindex] //this is to avoid spaces
msg = strings.Trim(msg, " ")
msg = strings.Trim(msg, "\n")
msg = strings.Trim(msg, "\r")
log.Info("NEWHEADER info:" + msg + "")
} else {
log.Error("THERE IS NO NEWHEADER")
}
I'm sorry for being so late

WeChat Immediately Closing Using SDK

I'm trying to send text to WeChat for sharing and it looks like WeChat loads but then immediately closes. The error code I'm getting in WXEntryActivity is -6, which doesn't seem to correspond to anything in the ErrCode enum.
My package name is the same that the app was registered under and I'm building with the signature also used to register the app. I'm stumped on where to go from here.
Here's what I see in the logcat (including some data from the BaseResp object):
D/MicroMsg.SDK.WXApiImplV10: check signature:...
D/MicroMsg.SDK.WXApiImplV10: pass
D/MicroMsg.SDK.MMessageAct: send mm message, intent=Intent { cmp=com.tencent.mm/.plugin.base.stub.WXEntryActivity (has extras) }
D/WXEntryActivity: onResp: errStr: null
D/WXEntryActivity: onResp: transaction: text1461027271082
D/WXEntryActivity: onResp: getType(): 2
D/WXEntryActivity: onResp: errCode: -6
And this is the code that gets called to send the request (I literally copied it from someone's test app that works):
WXTextObject textObj = new WXTextObject();
textObj.text = "TEST TEXT";
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
// msg.title = "Will be ignored";
msg.description = "TEST DESCRIPTION";
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("text");
req.message = msg;
req.scene =/* isTimelineCb.isChecked() ? endMessageToWX.Req.WXSceneTimeline :*/ SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);

XMPP server : Account created but attributes not getting saved

I am creating new user on ejabberd server.The user gets created.
In the code below user 918 got created but no attribute were saved.
ejabberd server has access rule is : [register, [{allow,all}]]
However, the attributes that i pass along with createAccount(), do not get saved on the ejabberd server.
libraries used - asmack-android-8-4.0.7, dnsjava-2.1.7.
XMPP server - ejabberd 2.1.11
Code is
ConnectionConfiguration config = new ConnectionConfiguration("myserver.co");
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
conn1 = new XMPPTCPConnection(config);
conn1.connect();
AccountManager accMan = AccountManager.getInstance(conn1);
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("phone", "237");
attributes.put("misc", "123");
accMan.createAccount("918", "hellopass", attributes);
conn1.login("918", "hellopass");
accMan = AccountManager.getInstance(conn1);
Log.d(TAG, "acc IMEI " + accMan.getAccountAttribute("misc")); // null returned here
Log.d(TAG, "acc phone " + accMan.getAccountAttribute("phone")); // null returned here
The last 2 lines of code posted above, return null value.
I do not know what am i missing to correctly create account with attributes too getting saved.
thanks
Vilpesh.
Attributes are not stored on the account but on the Vcard. When you register a user, you can only pass username and password.
For the user information, you should look into setting your Vcard.

javamail fetch mail pop3 android

I want to fetch emails in android by using javamail.
but I encountered some problems.
in POP3, if I want to fetch the content of a certain mail, the javamail will download all the content of the mail including the attachments. if the attachments are large enough, the android program will throw oom exception. But I tried in IMAP protocal, it only fetch the skeleton of the content first and even if there are some large attachments, it works well.
so, how could I fetch the content of a certain email using pop3 protocal when there are large attachment in the email?
following codes work well with imap, but not with pop3.
public void getContent(Part part) throws Exception {
String contentType = part.getContentType();
int nameindex = contentType.indexOf("name");
boolean conname = false;
if (nameindex != -1)
conname = true;
if (part.isMimeType("text/plain") && !conname) {
bodytext.append((String) part.getContent());
} else if (part.isMimeType("text/html") && !conname) {
String html = (String) part.getContent();
Spanned plainText = Html.fromHtml(html);
bodytext.append(plainText);
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int counts = multipart.getCount();
for (int i = 0; i < counts; i++) {
BodyPart mpart = multipart.getBodyPart(i);
Log.d("type,"," "+ i+mpart.getContentType());
String disposition = mpart.getDisposition();
if ((disposition != null)
&& ((disposition.equals(Part.ATTACHMENT)) || (disposition
.equals(Part.INLINE))))
continue;
String contype = mpart.getContentType();
if(contype.toLowerCase().indexOf("application")!=-1||contype.toLowerCase().indexOf("name")!=-1)
continue;
getContent(multipart.getBodyPart(i));
}
} else if (part.isMimeType("message/rfc822")) {
getContent((Part) part.getContent());
}
}
I do not know the specifics of JavaMail, however:
IMAP provides methods in it's protocol for downloading MIME Parts of a message, allowing the message to be picked apart on the server and downloaded in parts.
POP3s basic protocol only has support for downloading the entire message. With the optional TOP extension, it will allow you to download the first n lines of a message.
If JavaMail has specific extensions to stream the download of a message to disk (rather than all in memory), that should allow you to get around your Out Of Memory error.
JavaMail 1.4.4 and later can cache POP3 messages in a tmp file on disk, thus using less memory. See the javadocs for the com.sun.mail.pop3 package for the properties to set. You'll still need to be careful how you process the contents of the mail, e.g., using getInputStream instead of getContent to process large attachments.

Smack getPrivacyList method throws classCastException

I am using Smack in an Android app for XMPP based communication. I am trying to create a privacy list using PrivacyManager. I have created a PrivacyList successfully, but when I try to access that list it returns ClassCastException. Here is the code:
PrivacyListManager privacyManager;
privacyManager = PrivacyListManager.getInstanceFor(connection);
PrivacyList privacyList = privacyManager.getPrivacyList("msg_block_list");
Investigating a bit more, I have found that the exception occurs in getRequest method in PrivacyListManager.java class on following line:
Privacy privacyAnswer =
(Privacy) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
Here is the getRequest method:
private Privacy getRequest(Privacy requestPrivacy) throws XMPPException {
// The request is a get iq type
requestPrivacy.setType(Privacy.Type.GET);
requestPrivacy.setFrom(this.getUser());
// Filter packets looking for an answer from the server.
PacketFilter responseFilter = new PacketIDFilter(requestPrivacy.getPacketID());
PacketCollector response = connection.createPacketCollector(responseFilter);
// Send create & join packet.
connection.sendPacket(requestPrivacy);
// Wait up to a certain number of seconds for a reply.
Privacy privacyAnswer =
(Privacy) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
// Interprete the result and answer the privacy only if it is valid
if (privacyAnswer == null) {
throw new XMPPException("No response from server.");
}
else if (privacyAnswer.getError() != null) {
throw new XMPPException(privacyAnswer.getError());
}
return privacyAnswer;
}
I have followed this tutorial for implementing PrivacyList. Anyone can help?
PacketCollector's method getResult() returns Packet object, but it could be any inherited from Packet class object, either Message or Presence or IQ (in IQ case you can use (privacy) cast, because Privacy is IQ's child class). In your case it seems like you get object which is present in another hierarchy, than Privacy class.
I have found the problem. The XML file smack.providers was missing. I created the file with following contents and it worked.
<?xml version="1.0"?>
<!-- Providers file for default Smack extensions -->
<smackProviders>
<!-- Privacy -->
<iqProvider>
<elementName>query</elementName>
<namespace>jabber:iq:privacy</namespace>
<className>org.jivesoftware.smack.provider.PrivacyProvider</className>
</iqProvider>
</smackProviders>

Categories

Resources