Detect emails and response with a mail - java

If i got any email I want to response with a email from my code.
Am new to java, Is it possible with any api to detect mails?
I saw javamail api in this we can read and send mail. In this we need to pass server host. To read hotmail emails what host name we need to pass?
I find the following code.
Properties props =System.getProperties();
props.setProperty("mail.store.protocol","imaps");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("server hostname","username","password");
can u prefer any tutorials for that and property key values and server hosts.

http://www.javatpoint.com/example-of-receiving-email-using-java-mail-api
here the solution to read mails

For reading mail
http://www.compiletimeerror.com/2013/06/reading-email-using-javamail-api-example.html
and get the To Address and send mail using
http://www.tutorialspoint.com/java/java_sending_email.htm

Related

Mail headers missing via SMTP in office365

Here is peculiar case only found on office365 and not on outlook.com.
When I send email with a custom header set (X-ArchiveNum) via O365 SMTP. I am able to send email. But the receiver does not receive the headers when fetched via IMAP. This issue exists only with office365.
Transport transport = getTransport();
MimeMessage forward = new MimeMessage(getSession());
forward.addHeader("X-ArchiveNum", num);
forward.setSubject(sMessage.getSubject());
forward.setFrom(InternetAddress.parse(from)[0]);
forward.setText(body);
forward.saveChanges();
transport.sendMessage(forward, InternetAddress.parse(to));
I see the header being set, when I goto Sent Items folder in my outlook.office365.com account:
I also see the header on the browser when viewed from recipient account. But when I fetch the email via IMAP, I do not see it. I can see the other O365 related headers, but not mine.
Things work as expected from outlook.com (not O365)

Using Mailjet API in Grails application for sending mails

I want to integrate Mailjet API in Grails application for sending mails, I have already used ASYNC mail API for sending mail in grails, in this api , I need to give configuration parameters in config.groovy. Now as I am using Mailjet for sending mails, so I need to give Mailjet SMTP settings in config.groovy, so I give as follows :
grails {
mail {
host = "in.mailjet.com"
port = 465
username = "xxx#gmail.com"
password = "xxx"
props = ["mail.smtp.host": "in.mailjet.com",
"mail.smtp.socketFactory.port": "465",
"mail.smtp.socketFactory.class": "javax.net.ssl.SSLSocketFactory",
"mail.smtp.auth":"true",
"mail.smtp.port": "465",
"mail.smtp.user": "API key of mailjet",
"mail.smtp.password": "secret key of mailjet"]
}
}
Here is the example of sending mail using java code
What am I doing wrong?
username and password have to contain your api key and the api secret, not the credentials
that you use to log into the web site.
Using mail.smtp.host as a property is redundant and should not be necessary.

Not able to search the email server with searchTerm using Mail API

An email is sent from lets say gg#gg.com with an attachment to 2 email id's .One, my email id and another xx#xx.com
I got that email. When i did mail search using java Mail API with email id as search criteria , it is not able to find it though i received that email.But when i forward it to the same email id's search criteria is working fine. Please let me know when email is received for the first time at that point why it is not able to search
Properties properties = System.getProperties();
properties.put("mail.smtp.host", ExchangeProperties.getSmtpHost());
properties.put("mail.pop3.connectiontimeout", String.valueOf(ExchangeProperties.getPop3ConnectionTimeout() * 1000));
properties.put("mail.pop3.timeout", String.valueOf(ExchangeProperties.getPop3Timeout() * 1000));
session = Session.getInstance(properties, null);
session.setDebug(logger.isDebugEnabled());
// Get the store
store = session.getStore("pop3");
store.connect(ExchangeProperties.getSmtpHost(), user, password);
Folder folder = store.getFolder(folderName)
Message[] foundMessages = folder.search(andTerm); //andTerm contains email id
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(foundMessages, fp);
Since you're using pop3, all the searching is done by downloading all the messages to the client and searching them there. If you want the server to do the searching, use imap.
If the new message arrives while you have the pop3 folder open, you won't be able to see it. You need to close the folder and reopen it. That's a limitation of the pop3 protocol.
If none of that helps with your problem, I would need to know exactly which search term you're using, exactly what value you're searching for, and exactly what value appears in the email header.

How to send email from a java web app

I have a java web application sitting on Tomcat/Apache.
I have a form which has to send email. What is the best way to get this working.
I suppose these threads did appear when you posted your question:
Sending mail from java
How do I send an e-mail in Java?
How can I send an email by Java application using GMail, Yahoo, or Hotmail?
You should look at JavaMail API
Additionally, you may want to look at Fancymail, a small library to simplify usage of JavaMail API.
Short and dirty copy-and-paste for sending a simple plain text mail message using javamail here
Tiny example of sending a plain text msg, using custom smtp host:
Properties props = new Properties();
props.put("mail.smtp.host", "your.mailhost.com");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("mail#from.com"));
msg.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("mail#to.com")});
msg.setSubject("Subject Line");
msg.setText("Text Body");
Transport.send(msg);

How to set the Return-Path to an email address other than Sender address using JavaMail?

How to set the Return-Path to an email address other than Sender address using JavaMail?
The code below does what you want, and does it in the correct way. Reread what you yourself posted in the comment
From: RFC2821: 4.4 Trace Information
When the delivery SMTP server makes
the "final delivery" of a message, it
inserts a return-path line at the
beginning of the mail data. This use
of return-path is required; mail
systems MUST support it. The
return-path line preserves the
information in the
from the MAIL command. Here, final
delivery means the message has left
the SMTP environment. Normally, this
would mean it had been delivered to
the destination user or an associated
mail drop, but in some cases it may be
further processed and transmitted by
another mail system.
and a few lines later.
A message-originating SMTP system
SHOULD NOT send a message that already
contains a Return-path header.
If you carefully read this you will understand that only the final smtp-server/delivery agent is supposed to add the Return-Path header. It is not something you as client (trying to send a mail) should do. The final smtp-server will base the Return-Path header on the sender address of the envelope (SMTP MAIL FROM part).
So setting mail.smtp.from is the correct way to tell java that the envelope sender address should be different from the from part.
If you have troubles understanding what the different from's are just take a look at a telnet smtp-session. Where replyto#example.com should correspond to mail.smtp.from and from#example.com to m.addFrom(...);
telnet smtp.example.com 25
220 smtp.example.com ESMTP .....
helo computername
250 smtp.example.com Hello computername [123.123.123.123]
mail from:<replyto#example.com>
250 <replyto#example.com> is syntactically correct
rcpt to:<rcpt#foo.com>
250 <rcpt#foo.com> verified
data
354 Enter message, ending with "." on a line by itself
To: Joey <to#joey.com>
From: Joey <from#example.com>
Subject: Joey
Hey Joey!
.
250 OK id=....
Quit
props.put("mail.smtp.from", "replyto#example.com");
Session session = Session.getDefaultInstance(props, null);
MimeMessage m = new MimeMessage(session);
m.addFrom(InternetAddress.parse("from#example.com"));
I've experienced the same issue and found the only solution discussed putting property "mail.smtp.from" props.put("mail.smtp.from", "replyto#example.com");
Still this solution was not suitable for me because I'm sending lot's of e-mails from different users, so recreating session for each e-mail would be horrible for prodictivity.
So I found another solution after reading JavaMail sources:
1) Use SMTPMessage(extends MimeMessage) instead of MimeMessage.
2) Use setEnvelopeFrom(String) method.
3) Use SMTPTransport to send e-mail (I didn't try with others).
Here is a code example:
SMTPMessage message = new SMTPMessage(session);
message.setEnvelopeFrom("returnpath#hotmail.com");
...
transport.sendMessage(message, message.getAllRecipients());
I found that if the 'mail.protocol' property is set to something other than 'smtp' (like 'smtps'), then only the following would work:
props.put("mail.smtps.from", "replyto#example.com");
This allowed me to avoid using the SMTPMessage class as described in GiorgosDev's answer (classes in the 'com.sun' package aren't intended to be public API).

Categories