Java SMTP accept badly formatted emails - java

I am using SubethaSMTP to proxy smtp emails. I have an application sending emails to this proxy.
It happens that sometimes the application send malformed emails (for historical reasons).
This I cannot change. This is a buggy external solutions.
I am handling the received emails and route them based on the email value.
My issue is when I call the following method on the received emails:
Address[] tos = message.getRecipients(RecipientType.TO);
When there are malformed emails in the recipients list, I cannot get the list because I receive an exception.
My objective is to get all the emails and correct the malformed ones.
But because of the exception, I cannot get them.
Is there a way to get all the recipient emails even if there are some bad ones? Just read them without any issue? Is it possible to bypass the controls?
The stack trace is: The email address is #TEST(value) . this is generated by the external application. I'd like to be able to get it, and remove it from the list, and correct it to resend it
javax.mail.internet.AddressException: Missing local name in string ``#TEST''
at javax.mail.internet.InternetAddress.checkAddress(InternetAddress.java:1216)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:1096)
at javax.mail.internet.InternetAddress.parseHeader(InternetAddress.java:663)
at javax.mail.internet.MimeMessage.getAddressHeader(MimeMessage.java:733)
at javax.mail.internet.MimeMessage.getRecipients(MimeMessage.java:565)
at MessageHandler.done(TestMessageHandler.java:128)
at org.subethamail.smtp.server.Session.endMessageHandler(Session.java:513)
at org.subethamail.smtp.server.Session.resetMessageState(Session.java:490)
at org.subethamail.smtp.command.DataCommand.execute(DataCommand.java:84)
at org.subethamail.smtp.server.RequireTLSCommandWrapper.execute(RequireTLSCommandWrapper.java:30)
at org.subethamail.smtp.server.CommandHandler.handleCommand(CommandHandler.java:99)
at org.subethamail.smtp.server.Session.runCommandLoop(Session.java:244)
at org.subethamail.smtp.server.Session.run(Session.java:145)

Related

How to differentiate between the automatic reply and the normal mail received

I am working on receiving mails in my springboot application. In order to fetch and store the receive mails. I am using imap mail listener. There are two types of mails which I am storing. One is multipart payload type and the other is string payload type.
After receiving mail I am trying to send an auto-generated mails using java mail.
The issue which I am facing is worst case scenario of generating auto-reply from my application i.e infinite loop.
Can someone help ow can I differentiate between a normal mail received and auto-reply received in my mail box and generate auto-replies from my system only for those mails which are not auto-reply type.
It would be nice if explained via code for headers check. I came across through few headers like x-Autosubmitted. But they are returning null if I am trying to print the values in console.
The auto-submmitted markers are described below that you may find helpful:
auto-generated - Indicates that a message was generated by an automatic process, and is not a direct response to another message.
auto-replied - Indicates that a message was automatically generated as a direct response to another message.
auto-notified - Indicates that a message was generated by a Sieve notification system.
no - Indicates that a message was NOT automatically generated, but was created by a human. It is the equivalent to the absence of an Auto-Submitted header altogether.
The RFC 2822 states the following:
Though optional, every message SHOULD have a "Message-ID:" field.
Furthermore, reply messages SHOULD have "In-Reply-To:"
So, you may check for the "In-Reply-To:" value in the header.
Also you may add your own value to the outgoing email, so you may distinguish between an automatically generated reply from your system and manually created.

gmail is modifying header(Message-ID) of incoming mails

I sent a mail from my smtp server to gmail.
The message-id that I got is:
Message-ID: SMTPIN_ADDED_BROKEN#mx.google.com>
X-Google-Original-Message-ID:
Gmail appended extra parts in Message-id.
All I got on google that it can be an authentication issue.But source of mail shows:
SPF:PASS
But DKIM is not present in the source.
Do we require DKIM to be necessarily present in the mail's source??
What can I do to prevent header from being modified?
the msesage ID will be modified only if it does not obey RFC standard. so send mail with correct message id format
After many tries, I finally figured out that you need to add brackets enclosing the Message-Id
<yourid#domain.com>

java mail - getting message by message ID

I'm developing Android mail client. I need to build a "conversation" structure for every email message. I use the
IMAPMessage.getInReplyTo()
method that returns the Message ID of message which the message is a reply to. Unfortunatelly there seems to be no easy way to obtain message from
IMAPFolder
using its message ID. It is only possible to get the message by its UID. Is there an easy way to get the IMAP message by its Message ID?
You can use the IMAPFolder's search method like this:
SearchTerm searchTerm = new MessageIDTerm(messageId);
Message[] messages = imapFolder.search(searchTerm);
See the docs for the IMAPFolder's search method here:
https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/IMAPFolder.html#search-javax.mail.search.SearchTerm-
and for the MessageIDTerm class here:
https://javaee.github.io/javamail/docs/api/javax/mail/search/MessageIDTerm.html
unfortunately there is no straight forward solution... May be what you can try is to maintain an internal structure with body structures of all the mail ids and then perform a one on one Message-ID check and obtain the UID of mail. Anyways you would be doing it, in order to show the Maillist. Add a new logic to map the message-ids as well.

Java Mail API: Find if a mail is forwarded mail or a reply to another mail

I have a very special requirement
I am using Java Mail API to access a user's Inbox. You can say its like a service inbox for complaints. User registers a complaint by sending an email to this address. I fetch every email from Inbox and create a new complaint. My problem is that I don't want to create unnecessary complaints for reply or forwarded emails. Is there a way to find out that.
Most email clients add Re: or Fwd: to the subject line, but I wouldn't rely on that to determine whether an email is a reply or forwarded text; for example, a German mailclient might put Betr.: in front of the original subject.
Instead, you might want to look at the In-Reply-To: header and/or the References: header. See RFC 4021 for detailed information on those headers.
You can retrieve the headers from a Message by calling the getHeader(java.lang.String) method.
Reading the email header might help, see http://javamail.kenai.com/nonav/javadocs/javax/mail/Part.html#getAllHeaders(). In case of replies, the message header would include:
In-Reply-To: Message-ID of the message that this is a reply to
In case of a forwarded message, selected headers might be preserved. Not as straight-forward as the previous case, but it might still be possible to determine if it was a forwarded message. Refer to http://en.wikipedia.org/wiki/Email_forwarding#Manual_client-based_forwarding for details.

Sending email, via javamail smtp, results in messagePartiallyDelivered on a completely successful send

Sending email, via javamail smtp, with 2 addresses listed in MIME headers but only 1 address used in send function seems to result in a messagePartiallyDelivered, regardless of the fact that the 'one' address provided to the send function successfully received the message.
Is this the expected behaviour ?
The javadocs for javamail are not very well filled out, so it is hard to tell if this is the behaviour I can expect with any implementation of javamail (be it sun/oracle, or gnu).
Do you have mail.smtp.reportsuccess set? If so, that's the expected behavior.
Otherwise, it would be helpful to see the protocol trace and debugging out.

Categories

Resources