How to get an SMTP response using Javamail - java

I'm using Javamail to connect to an AWS email service; I've tested that I can receive emails using this code, but I would also like to get a response with data about the sent email, such as the Message ID, which AWS uses to uniquely identify a message.
I'm using MimeMessage to create an email and I am sending it with this code within a try catch block:
transport.sendMessage(message, message.getAllRecipients)
This code just fires off an email to the AWS server and I can't get any metadata back. Is there a way to listen for a response to see if the message was successful so I can retrieve the Message ID?

If the sendMessage method returns, the server accepted the message. Note that that doesn't necessarily mean it will be delivered to the recipient, however. But at that point you can use the getMessageID method to retrieve the message ID.
If the send method throws an exception, the server refused to accept the message for some reason and it won't be sent.

add properties mail.smtp.reportsuccess and mail.smtp.sendpartial to you java Mail Properties, you will get SMTPAddressSucceededException if send email success or SMTPAddressFailedException in fail.
javamail doc

Related

AWS SNS Response Link to JavaMail

I have sent my mail through java mail and registered my email on Amazon to get the response notifications (Bounce, Delivered) etc.
How can i succesfully match these two to say when i have sent the mail, the mail i sent has the following incomming response.
Note: Everything is setup (Webhooks to get the response from AWS SNS)
I tried matching it by the messageId, but it seems aws adds a different messsageId than the java mail one. Example
MimeMessage msg = new MimeMessage(session);
msg.getMessageID(); // returns "<1619401941.3.1560500581268#Tinus-NB>"
And the from the AWS SNS response
"MessageId" : "7cd42bc4-e5c2-576b-a567-7eb9baa51cad" // directly
"Message" : "{\"notificationType\":\"Delivery\",\"mail\":{\"timestamp\":\"2019-06-14T08:38:55.113Z\",\"messageId\":\"0102016b5523c189-55acd572-ba3f-4750-aaae-b7019080f1ae-000000\",\"delivery\":{\"timestamp\":\"2019-06-14T08:39:03.043Z\",\"processingTimeMillis\":7930,\"smtpResponse\":\"250 2.0.0 OK 1560501543 f15si1572805ede.113 - gsmtp\"}}" // this is in the Message object in the JSON
Just to be clear, I want to match these two, to know what i sent and what the response was.
I am using Java Mail and not the AWS SDK lib
SES overwrites MessageID whatever you set because it also needs to know which email is it. In your case, when sending emails from Javamail,in the last section of your code, you can print the 250 response code+ message ID from SES and match it with the message ID you received from SNS.
When SES accepts an email in SMTP conversation, it gives 250 ok+ message ID and the same message ID can be seen in SNS notification.
Found it, i just had to include the original headers

How to get the message id of the email message before sending using Exchange Web Service(JAVA)?

Need to track the emails so before sending emails I need to get the message id of the email message.
All you need is to save the EmailMessage first before you send it.
EmailMessage emailMessage = new EmailMessage(service);
emailMessage.save();
EWS saves the email message and assigns an Internet message identifier to it. But on your local emailMessage this property still isn't set. Trying to access it by
emailMessage.getInternetMessageId();
will result in a ServiceObjectPropertyException. You need to get the remote and updated version of the email message:
emailMessage = EmailMessage.bind(service, emailMessage.getId());
Now you can read the Internet message identifier property. Set other properties as needed and then send the message.

Java SMTP accept badly formatted emails

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)

How to get message and sender sent via Skype with Skype4Java

Once I have received a message in Skype, I would like to be able to get the username of the sender as well as the content of the message using Skype4Java. How would I go about doing this?
message.getContent().getSender().getUsername();

How to make sure a SMS went through after sending it with Twilio

I am using the Twilio java wrapper provided on the website and started making some tests. I am able to send text messages that are successfully received. However, I would like to make sure that the messages have been sent successfully and that no problem has been encountered by Twilio (bad phone number or whatever reason).
I understand that when you make a REST request to Twilio to send a SMS, Twilio responds with the Status. How could I get this response?
Here is the explanation page I found: http://www.twilio.com/docs/howto/sms-notifications-and-alerts
If you specify a StatusCallback URL when you make the request to send an SMS, we will make a request to the callback URL you provided after the message has finished processing, with the parameters either SmsStatus=sent or SmsStatus=failed. You can use this information to do more processing on the SMS message. There's more information here: http://www.twilio.com/docs/api/rest/sending-sms#post-parameters-optional
Alternately, if you hang on to the SMS Message Sid, you should be able to query the API for the message and get the status in the response. So if the sid is SM123, making a GET request to https://api.twilio.com/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json should return a object with the status of the SMS Message.
I recall that the response comes to your url and can be matched up by an ID. In the REST post to SMSMessages you can specify a statuscallback url where Twilio will post a status message to your url.
When you receive that post to your site, you can record it or take any other action you need, such as retrying or using another mode of communication.
In 2020, with the Java SDK, you can now create a MessageFetcher with that SID and then call fetch to ask for the Message instance until its getStatus returns "delivered", "undelivered", or "failed".

Categories

Resources