parse "from" address in email in javamail - parsing

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);

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 !

Mirth HL7 ACK ERROR: Message control Ids do not match

I'm starting out with Mirth and HL7 and I'm trying to send a message to a remote server. My MSH looks as follows:
MSH|^~\&|EPIC|EPIC|IMG_SCHEDULE_APPT|REMOTE|20170328193318|PERSONNAME|ORM^O01|12345678|T|2.4||||||||||
The response looks as follows:
MSH|^~\&|IMG_SCHEDULE_APPT|REMOTE|EPIC|EPIC|20170328193318||ACK|12345678|T|2.4|
MSA|AA|||
and I get an error saying ERROR: Message control Ids do not match.
As far as I understand this error means that the Message Control Id which is returned in the ACK message is not the same.
From what I can see, the number 12345678 is the Message Control Id, and I see that number both in the message I send as well as the in the ACK which is returned. So what is wrong here? And who is at fault? Me or the remote party?
Does anybody know how I can solve or debug this?
If you don't want to validate the message control ID, you can open the Response Validation properties and uncheck "Validate Message Control Id":
If the remote system cannot change their logic and you still want to validate the control ID, you can do that in a response transformer:
if (responseStatus == ERROR) {
// msg here is the ACK, origMsg is the encoded data that was sent outbound
var origMsg = new XML(SerializerFactory.getSerializer('HL7V2').toXML(connectorMessage.getEncodedData()));
if (origMsg.MSH['MSH.10']['MSH.10.1'].toString() == msg.MSH['MSH.10']['MSH.10.1'].toString()) {
responseStatus = SENT;
}
}
MSA.2 (Message Control ID) is required and should be the same as the ControlId in the former message that the ACK message acknowleges..

How to send a email automatically from a .net application without using smtp server?

In my lab smtp server is blocked.I had gone through the EAsendmail package but i couldn't succeed in sending email from the application directly without using going through smtp server.
here is the code
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
//Set sender email address, please change it to yours
oMail.From = "yaswanthmdh#gmail.com";
//Set recipient email address, please change it to yours
oMail.To = "manojsr6#gmail.com";
//Set email subject
oMail.Subject = "direct email sent from c# project";
//Set email body
oMail.TextBody = "this is a test email sent from c# project directly";
//Set SMTP server address to "".
SmtpServer oServer = new SmtpServer("");//173.194.40.246,"74.125.237.181");
//Set 465 port
oServer.Port = 465;
//detect SSL/TLS automatically
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
//Gmail user authentication
//For example: your email is "gmailid#gmail.com", then the user should be the same
oServer.User = "yaswanthmdh#gmail.com";
oServer.Password = "";
try
{
Console.WriteLine("start to send email directly ...");
oSmtp.SendMail(oServer, oMail);
Console.WriteLine("email was sent successfully!");
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
after running this code i am getting timeout error.
is there any other way to send an email other than through smtp?
is there any way to send an email from application through http?
No. That's how email works. Just like you need a web server to serve web pages, you need an SMTP server to send email. You'll have to talk to your IT Infrastructure department, and get them to either set something up for you or allow requests to whatever SMTP server you're trying to use.
If you just need to test sending email, you can use something like Papercut, for the time being, and then depending on where and how you deploy your application in production, it may no longer be a problem.

How to know the current state of application invoked in blackberry?

Is it possible to know the state of application invoked in blackberry? For example, if we invoke blackberry email application after sending an email, can we know if the application has closed or still running and also where the email has been sent, the subject, the content, etc.? The code may be something like this:
try {
Message message = new Message();
Address address = new Address("email#yahoo.com", "Email");
Address[] addresses = {address};
message.addRecipients(RecipientType.TO, addresses);
message.setContent("Testing email from MyTabViewDemo application");
message.setSubject("Testing Email");
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(message));
log.debug(MyApp.GUID_LOG, "Send email action done!");
} catch (Exception e) {
Dialog.inform(e.toString());
}
and how about retrieving the state of other applications like phone, sms, camera?
Thank you.
You can view the visible applications by calling
ApplicationManager.getApplicationManager().getVisibleApplications();
That returns an array of application descriptors. From a descriptor, you can know the names and ids.
It is possible, however, that the messaging app is always on background and cannot be closed (I'm not 100% sure here)
But you can't know if a message has ben sent or not sending the mail like that.

Message-Id is being replaced when sending mail via JavaMail

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.

Resources