Message-Id is being replaced when sending mail via JavaMail - message

I have tried to find a solution to this with any luck, so i decided to post it here.
The problem is, when i send a message with javaMail, it automatically generates a Message-Id (The one i store into my database to then identify replies to this message) but it is being changed for some reason by the smpt server when the message is sent so i wont be able to trace any related to this message.
For example
I first send a message via gmail to one of the accounts sycronized with my mail client, then i check the message with my message client and everything is ok the Message-Id is
<CAPDSfCN1qPAhBCRmFK-zwP=MM=KjgpYuvhVRFAPwz1PjOqtnFA#mail.gmail.com>
Then i send a reply for this message via my message client, the id generated by javaMail is
<1907960987.0.1322086080735.JavaMail.root#smtp.live.com>
Finally, when i go to check the reply in my email account it has the following values in its headers
Message-ID: <BLU0-SMTP33091BE2B32A7F46E370665C2C90#phx.gbl> FAIL
In-Reply-To: <CAPDSfCN1qPAhBCRmFK-zwP=MM=KjgpYuvhVRFAPwz1PjOqtnFA#mail.gmail.com> OK
As you see, the Message-Id is changed, i was expecting it to be
<1907960987.0.1322086080735.JavaMail.root#smtp.live.com>
Why is this happening?
I appreciate any help
Thank you all
--Edit
According to sugestions i made a test using smtpsend demo from javaMail (I implemented a subclass of MimeMessage to generate my own Message-Id).
java -jar -Dmail.smtp.starttls.enable=true -Dmail.smtp.port=587 SMTPSend.jar -d -M smtp.live.com -U myaccount#hotmail.com -P mypass -o myaccount#hotmail.com -A anotheraccount#gmail.com
Between smtpsend output when message was sent, there was the Message-Id generated
<60eea6ae-2657-41bd-b475-3a57eff885ac#mydomain.com>
But then, when i went to check this message on anotheraccount#gmail.com, the Message-Id was different
<BLU0-SMTP109215E6BB99B93FC106B1E88B00#phx.gbl>
Why is it changing my Message-Id on the fly... i dont get it
--Edit 2
I noticed that the problem is now happening just when i send mails from a hotmail account
message-id is not changing anymore when i send mails from a gmail account (i think that implementing my own Message-Id generation method helped to solve that)
Thanks for replying

I know this is an old thread, but this answer could still maybe help people.
You need to overrule updateMessageID() in MimeMessage as it gets called everytime before sending an e-mail.
class MyMessage extends MimeMessage {
public MyMessage(Session session) {
super(session);
}
protected void updateMessageID() throws MessagingException {
setHeader("Message-ID", "<your-message-id#domain.nl>");
}
}
And if you would like to pass the unique id for each MyMessage...
class MyMessage extends MimeMessage {
String uniqueMessageId;
public MyMessage(Session session, String uniqueMessageId) {
super(session);
this.uniqueMessageId = uniqueMessageId;
}
protected void updateMessageID() throws MessagingException {
setHeader("Message-ID", "<" + uniqueMessageId + ">");
}
}
And then call it, eg:
MyMessage message = new MyMessage(session, "201610131428_newsletter1#domain.nl");

We've encountered the same issue with Simple Java Mail and as far as I know the only way to catch the actual ID assigned by the SMTP server is to parse the 250 OK response which includes it (in case of MS Exchange).
As Bill (the prime author of the original JavaMail) mentions below, the SMTP server should actually never change the Message ID, but some servers like MS Exchange explicitly allow configuration otherwise. For example see: https://social.technet.microsoft.com/Forums/en-US/132df54c-871b-4e9d-98c9-5b5caa993322/custom-message-id-replaced-by-outlook-smtp-server?forum=exchangesvrclients.
If you are seeing this behavior with all servers you connect to, chances are you are not setting MessageID the right way (Java/Jakarta Mail is a little obtuse about this). In that case refer to the other answer.

Your mail server is broken. It should not be changing the Message-ID header.
Report the problem to the owner of your mail server.

Related

Proper way to NOT send a reply back from webhook

I have setup a webhook and everything is working properly, however iam used to using a long number where STOP is handled automatically, but now I have a short code where we have to handle STOP ourselves. This isnt an issue on sending a message as I can check with my 'blacklist' numbers before sending the message. My question is in the Reply webhook, what is the best way or standard way to NOT send the reply message.
Below is twilios sample code (i added the comment where i would check if they are black listed)
public class SmsController : TwilioController
{
public TwiMLResult Index(SmsRequest incomingMessage)
{
var messagingResponse = new MessagingResponse();
// check for phone number in blacklist to NOT SEND
messagingResponse.Message("The copy cat says: " +
incomingMessage.Body);
return TwiML(messagingResponse);
}
}
If the number is blacklisted and i dont want to send the reply how do I "gracefully" not reply to them as this example takes a TwiMLResult and message response. Do i just set the message to an empty string ? do I return null? Any thoughts ? Thank you !

StatusCallback does not work as expected

Overview:
I am working on a VB.NET application that places a call into Twilio, the number is not a test number. I am using the C# library with the code written in VB.Net.
When I use this line of code:
Console.WriteLine(account.SendSmsMessage(Caller, to1, strBody))
I receive a text message on my phone however the post back is never posted to my website. I have included the URL of the site on the account under Messaging > Request URL.
When I reply to the message, Twilio does make a post to my site. From what I understand, a POST should have been made when Twilio was first sent a message from my application, however this is not the case.
When using this code, I do not get any text message and no POST is made.
Console.WriteLine(account.SendMessage(Caller, to1, strBody, PostBackURL))
I have tried SendSMSMessage, I have tried it with the URL on my account and without it,
nothing seems to effect the behavior.
I need the SmsMessageSid at the time the message is sent. From everything I have seen, Twilio does not provide a response other then what is sent to the PostBackURL, I could parse a response for the
SmsMessageSid however since there is no response that is not an option. If I am mistaken on that point that would be great, however it looks like the only way to get a reply is with the post back URL. Thanks for your help with this! Below you will find an excerpt of the code I am working with:
PostBackURL = "http%3A%2F%2F173.111.111.110%3A8001/XMLResponse.aspx"
' Create Twilio REST account object using Twilio account ID and token
account = New Twilio.TwilioRestClient(SID, Token)
message = New Twilio.Message
Try
'WORKS
'Console.WriteLine(account.SendSmsMessage(Caller, to1, strBody))
'DOES NOT WORK
Console.WriteLine(account.SendMessage(Caller, to1, strBody, PostBackURL))
Catch e As Exception
Console.WriteLine("An error occurred: {0}", e.Message)
End Try
Console.WriteLine("Press any key to continue")
Console.ReadKey()
I'm doing something similar with C# and like you, I'm not getting the POST to the callback URL. I don't know why that's not working, but if all you need is the sid when you send the message, you should be able to do this:
message = SendSmsMessage(Caller, to1, strBody))
and message.Sid will give you what you need, assuming no exceptions.

parse "from" address in email in javamail

I am trying to read emails from my inbox using javamail and I am specifically checking for emails that have the subject "Delivery Status Notification". Here's the code:
for(Message message:messages){
for(Address a:message.getFrom()){
if(message.getSubject().equalsIgnoreCase("Delivery Status Notification")){
Multipart mp=(Multipart)message.getContent();
for(int i=0;i<mp.getCount();i++) {
BodyPart bodyPart = mp.getBodyPart(i);
if (bodyPart.isMimeType("text/*")) {
String cont = (String) bodyPart.getContent();
System.out.println("Content: "+cont);
}
}
System.out.println("-----\n");
}
}
The problem is that the sender of these notifications is the "Mail Delivery System" and the line
Address a:message.getFrom()
throws the error -- " javax.mail.internet.AddressException: Local address contains control or whitespace in string ``Mail Delivery System'' ".
How do I parse this address without having to catch the exception and then work with it?
Thanks for any help.
Some mail servers send these delivery notification messages using a bogus and illegal From address. You should, of course, report this bug to the mail server vendor.
While you're waiting for the vendor to fix their problem, you can work around it in JavaMail by setting the following Session property:
session.setProperty("mail.mime.address.strict", "false");
session.setProperty() was unrecognized in my case. I used below implementation and it worked!
Properties props = new Properties();
props.put("mail.mime.address.strict", "false");
Session session = Session.getDefaultInstance(props);

How SignalR manages connection between Postbacks

1> Just want to understand how SignalR 1.x functions in a particular scenario
Lets say we have a 10 clients connected to Hub and one of the connected clients say client-1 performs a postback so OnDisconnected is called than OnConnected is called right ?
What happens if during this phase if client-2 try's to send message to client-1 exactly between the said scenario ie (msg is sent after client-1 is disconnected and before connected again )will client-1 miss the message or there's internal mechanism which makes sure client-1 does not miss the message sent by client-2
2> Second query I have is that I'm trying to pass a querystring using following code
var chat = $.connection.myHub;
$.connection.myHub.qs = { "token": "hello" };
but not able to retrieve it on the server side from the Context object
using
Context.QueryString.AllKeys
I even tried
var chat = $.connection.myHub;
$.connection.myHub.qs = "token=hello" ;
But it does not work ie when I check the keys, token is not present in AllKeys
Will appreciate if someone just help me out.
1: If a postback occurs a client will disconnect and then connect. However, when the client performs a connect again it will have a different Connection Id than it had prior to the postback. Therefore, any message sent to the old connection id will be missed because when the users browser connects again it will be known as a different client.
2: You're trying to set the query string on the hub proxy, not the connection. What you should be doing is:
$.connection.hub.qs = { foo: "bar" };

how to share a link via sms in blackberry

I am building an application where I need the option to share via email and SMS.
I have done the share via Email, where when the user selects the image, the url is passed as the content of the email. But while sharing via SMS, I can't do something like setContent as I did for email and fetch the url in the SMS directly, instead of user typing the address manually.
I am using Message class in email and MessageConnection class for SMS, as shown in the blackberry community example.
The Message object you receive when calling MessageConnection.newMessage(TEXT_MESSAGE) is actually a TextMessage object (or a BinaryMessage object with BINARY_MESSAGE).
If you cast the received object to the proper class (TextMessage or BinaryMessage), you should be able to use its setPayloadText(String data) (or setPayloadData(byte[] data) for a BinaryMessage) to enter a value in the message before sending it.
Your code should look like this:
Message msg = myMessageConnection.newMessage(TEXT_MESSAGE, /* address */);
TextMessage txtMsg = (TextMessage)msg;
txtMsg.setPayloadText(/* Text to send */);
myMessageConnection.send(msg);
When you send an email, you can set the body of it and send it to the user from the Email native application. You cant do taht for SMSs. I worked on that issue and for BB Torch I was able to set the text of the SMS message but for other devices that was impossible. I always obtain an empty text message!!
So y suggestion to you is using the following code wich will send the SMS to a number without the interference of the user
MessageConnection conn = (MessageConnection) Connector.open("sms://" + userNumber);
TextMessage txtmessage = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
txtmessage.setPayloadText(text);
conn.send(txtmessage);

Resources