Dealing with failed email sending with SMTP-JAVA - java

I am working on a service that sends out emails using SMTP. I set my SMTP properties this way:
property.setProperty("mail.transport.protocol", "smtp");
property.setProperty("mail.smtp.host", mailHost);
property.setProperty("mail.from", "xyz#gmail.com");
It also works for successful cases. However, when the email sending process fails, I want a notification sent to another one of our customer care email address(say, CC#gmail.com)..
How do I accomplish this. Is there any property among SMTP properties that lets me give like a "failure email address" so that it send a notification to that address, if a failure occurs

Bounced emails usually go back to the From: address, so you might change this to, say, "CC#gmail.com". This is also nice because it allows your customers back to you by just hitting "reply" in their mail client.

Related

send email to generic ID in lotus notes using notesFactory in java

Their is a requirement in a project to send emails from java application through lotus notes.
Note: the domino server is installed on client server.
Currently i am able to send email using notesFactory on my local machine.using notes.jar file
Which accesses the user by .nsf by its password.
I.e creating secure connection by password.
And gtting database object by calling
Session.getdatabase(null,"user.nsf")
Its perfectly working.
But for some types of emails the client have shared a generic id...(link) over an email... By clicking on that link the generic mail box opens under active user. In separate tab... Through which we can send emails.
But have not shared their .nsf path or id or password.
It directly opens by clicking on that link.
Now i want to access that generic id in notesfactory session
I tried to keep open that id and then running my code...but still it sends email through active user itself.
And client is not ready to share the id and password details of that user. Not the id file is getting generated in our local machine.
Is their any way to send emails through that id?
If anyone want code i am using..ill share.
But for some types of emails the client have shared a generic
id...(link) over an email... By clicking on that link the generic mail
box opens under active user. In separate tab... Through which we can
send emails.
That does not sound like a "shared id", it sounds more like a mail database with the ACL set to give a group of users access.
When you send an email from within Notes (no matter if it is through the UI or through code), the actual logged in user is used as the sender. It is intentionally by design, to prevent users from spoofing the sender.
There is an unsupported way to fake the sender address, by dropping the email directly into mail.box, but that should only be done by someone know what they are doing.
I wrote a script library several years ago, intended to help sending emails. It includes the ability to set the sender address. You can find it on my blog, it's free to use. But I would not recommend you using it without first understanding what the code is doing.
Here is the relevant part of the code:
Set mailbox = New NotesDatabase(mailservername,"mail.box")
If mailbox.Isopen = False Then
Print "mail.box on " & mailservername & " could not be opened"
Exit Sub
End If
Set me.maildoc = New NotesDocument(mailbox)
Call me.maildoc.ReplaceItemValue("Form","Memo")
Set me.body = New NotesRichTextItem(maildoc,"Body")
Call maildoc.ReplaceItemValue("Principal", me.p_principal)
' If principal is set, we want to fix so mail looks like
' it is coming from that address, need to set these fields
Call maildoc.ReplaceItemValue("From", me.p_principal)
Call maildoc.ReplaceItemValue("Sender", me.p_principal)
Call maildoc.ReplaceItemValue("ReplyTo", me.p_principal)
Call maildoc.ReplaceItemValue("SMTPOriginator", me.p_principal)
Call maildoc.ReplaceItemValue("PostedDate",Now())
If me.p_principal<>"" Then
Call maildoc.Save(True,False) ' Save in mail.box
Else
Call maildoc.Send(True) ' Send mail normally
End If
You use the Principal field to set the sender address.

Gmail watch user inbox, history.getMessagesAdded is not returning the new message

Requirement is to sync mails from Gmail for an user into our CRM. The system in place is based on Google Pub/Sub which watches inbox of the user for any change and fires the notification to our HTTPs endpoint. More on this at Gmail cloud pub/sub.
Based on the above procedure we git history of changes. And then i am interested in only new messages, so history.getMessagesAdded is preferred as per this guide. Issue we are facing now is the first mail of a thread is not captured under messagesAdded all the subsequent messages are passing through our system.
Note: For the first mail, we do get push from Google. But when we try to get Messages added it turns out empty. Is there anything special needs to be done for the first mail of the thread or am i missing out something.
I was experiencing a very similar problem, and my mistake was that I was using the historyId from the push notification, the solution was to store the last known historyId on my database, so, every time I get a notification, I get the history from the id I have stored, not the one from the notification.
In my case, the historyId from the notification doesn't even make part of the history, maybe because of my watch restrictions: labelIds=['INBOX']
This is the google pub/sub notification:
{
message:
{
data: {"emailAddress": "user#example.com", "historyId": "9876543210"},
message_id: "1234567890",
}
subscription: "projects/myproject/subscriptions/mysubscription"
}
I was using the message.data.historyId, wich was causing the confusion!
The message.data, comes as a base64 encoded string, in this example I just decoded it!
Step by step for watching new e-mails on the inbox:
Do all the configuration in the google pub/sub.
Start watching the user with the filters you want (docs.: https://developers.google.com/gmail/api/v1/reference/users/watch)
Store the historyId obtained in the step 2
When receive the notification, get all the events (history) using the stored id as the startHistoryId parameter (docs: https://developers.google.com/gmail/api/v1/reference/users/history/list)
In the history list obtained on the step 4, look for the new messages: history.getMessagesAdded().
Update the last known history id in your database, so you don't need to deal with the whole history every time!
I hope it helps.

Setting address manually on JGroups

I'd like to create a simple chat application where the user can send message to a specific user. Based on this answer, I can send a message to a specific target using the target's address. The problem is, I can't get a specific user address without tracking his id on the channel's view. Is there anyway to set a user address so I can generate a unique address for each registered user and then use it as their address each time they try to login? With that, I can get their address based their username from data storage.

Spring IAMP Mail receiver not reading all emails everytime [duplicate]

I have a piece of code which uses spring integration's IMAP adapter to poll an inbox to read all incoming emails which are unread and that works perfectly. But if I open any email message and and then mark it as "unread" in my outlook inbox the poller doesn't fetch the marked email.
I can use the pop3 adapter which fetches all the email, but deletes them afterwords, but I want to keep the emails in my inbox and I want the poller to fetch all the email which are unseen.
Any suggestions to handle this problem? I been searching and reading articles on email adapters but didn't find anything useful.
Thanks in advance.
Looks like you need custom 'search-term-strategy'. From Spring Integration (SI) documentation:
By default, the ImapMailReceiver will search for Messages based on the default SearchTerm which is All mails that are RECENT (if supported), that are NOT ANSWERED, that are NOT DELETED, that are NOT SEEN and have not been processed by this mail receiver (enabled by the use of the custom USER flag or simply NOT FLAGGED if not supported). Since version 2.2, the SearchTerm used by the ImapMailReceiver is fully configurable via the SearchTermStrategy which you can inject via the search-term-strategy attribute. SearchTermStrategy is a simple strategy interface with a single method that allows you to create an instance of the SearchTerm that will be used by the ImapMailReceiver.
And here is a post from SI forum with funtastic Oleg's explanation: Server does not support RECENT or USER flags
And here you can find SI DefaultSearchTermStrategy: it's a place to determine how you should implement your own strategy. I guess, you case is:
This email server does not support RECENT flag, but it does support USER flags which will be used to prevent duplicates during email fetch.
Switch SI-mail logging level to DEBUG and take a look, which flag supports your email server.

Using Java mail Pop3 cannot seem to delete emails from Gmail

I have a java program that acts as a POP3 client using javax.mail. I am able to list and retrieve the contents of a Gmail inbox no problem. However, I cannot seem to delete emails. Here is the (important parts of the) code:
POP3Store sto=... another method creates and connects the POP3Store
Folder ibx=sto.getFolder("INBOX");
ibx.open(Folder.READ_WRITE);
Message[] msgarr=ibx.getMessages();
for(int mi=0; mi<msgarr.length; mi++) {
...do stuff with the message
msgarr[mi].setFlag(Flags.Flag.DELETED, true);
}
ibx.close(true); //folder.close(true) indicates to expunge the folder
sto.close();
After running this and seeing it handle each message, I go into Gmail and the emails are still there, and even showing as unread. If I re-run the java client, it will see and handle the same emails.
This same code happily deletes emails from an exchange server.
How can I get Gmail to delete emails?
Gmail handles POP deletion specially.
You can configure what Gmail should do when a message is deleted through POP in Gmail Settings, on the Forwarding and POP / IMAP tab.
As SLaks says, Gmail is a special case where it has its own settings for controlling deletion that override whatever the client wants to do.
It can be edited in the Forwarding and POP / IMAP
However, I want to add that for a message to be considered "downloaded" by Gmail, you need to have retrieved the content of each message, and in the case of multipart message types, the content of each part within that message.
Here's some example code that I use to force the deletion of unwanted messages from Gmail:
// Grab the content to get the email off the server
// folder is of type javax.mail.Folder and is already in the correct state to get messages from the mail store (Gmail)
Message msg = folder.getMessage(1);
Multipart multipart = (Multipart) msg.getContent();
int partcount = multipart.getCount();
for (int count = 0; count < partcount; count++) {
multipart.getBodyPart(count);
}
If you prepend "recent:" in your pop3 username you will solve.
Example: recent:yourusername#gmail.com
This connects to gmail using Recent Mode

Categories

Resources