Home > Java > javaTutorial > body text

Receiving Email in Java

WBOY
Release: 2024-08-30 15:56:47
Original
876 people have browsed it

Receiving email in Java is the Store and Folder classes used in conjunction with MimeMessage, Session, and Transport classes. It is used for receiving emails and learning the JavaMail API’s email-sending stages. The emailreceive test class will make contact with the nearby email server, and account’s mails and display them to point out the ip address in the host config file for hostname and pop3 which is the preferred mail store protocol type.

Receiving Email in Java

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Key Takeaways

  • When using the JavaMail API, emails are received using the Store and Folder classes.
  • Emails can be sent and received using Java APIs. The underlying protocols have no bearing on this layer at all.
  • Obtain a session instance using the Session class’s getDefaultInstance() or getInstance() methods. To Produce the POP3 shop object and link to the pop store.
  • After executing the getFolder() method on the store object, create the folder object and open it in the mailbox.
  • Get the messages from the folder object.
  • Lock the folder and put the items away.

What is Receiving Email in Java?

To learn about the email-sending stages of the JavaMail API, the emailreceive test class will make contact with the nearby email server, connect to the account’s emails, and display them to highlight the IP address in the host’s config file for hostname and pop3 is the preferred mail store protocol type.

Receiving email is accomplished using the Store and Folder classes in conjunction with MimeMessage, Session, and Transport classes. The program receives email messages as HTTP requests. We must associate email addresses with servlets in the application setup in order to process incoming email messages, and we must then include the servlet code in the web app. The proper servlets receive HTTP requests from the incoming email to handle the requests and send the response.

Create Receiving Email in Java Class

We have examined using the Java Mail API to send emails. With only one protocol (SMTP) to learn, sending emails was rather straightforward. POP3 and IMAP are the two protocols used for reception, nevertheless. The more traditional POP3 protocol gives a single inbox with a single queue of mail messages. The more recent standard, IMAP, displays mail as entries in a hierarchy of folders, one of which will be an inbox.

Procedures for receiving mail

  • Define the mail properties that use the properties class to define the protocol and mail server.
  • Create the session with the attributes that we already defined to read the letter.
  • Establish a connection and create a store to read messages.
  • Define and open the folder that we need to read and the read-only folder should be opened.
  • Look through the unread contents of the chosen folder and save the results in an array of messages.
  • Then display the messages.

Two Jar Files in JavaMail

POP3 and IMAP Provider implementations, as well as their secure counterparts, POP3S and IMAPS, are included with Java Mail.

We must load the following two jar files in order to send emails using the JavaMail API:

Receiving Email in Java

  • mail.jar
  • activation.jar

We can incorporate the JavaMail API implementation, which is totally free and open source, into the product. This edition is also to be featured with SMTP, POP3, and IMAP providers. A Jakarta EE API called Jakarta Mail that is mainly used to send and receive email through SMTP, POP3, and IMAP protocols. The Java EE platform includes a built-in version of Jakarta Mail then however Java SE users can alternatively be utilized an extra package. In the most recent version which was released last few months and also another open source Java Mail implementation which is GNU JavaMail, supports only version 1.3 of the JavaMail specification but offers only the free NNTP backend, enabling the use of this technology for reading and sending newsgroup articles.

  • Mail.jar: We can incorporate the JavaMail API implementation, which is totally free and open source, into the product. Additionally, IMAP, POP3, and SMTP providers are included in this jar edition.
  • Activation.jar: The JavaBeans Activation Framework classes are contained in this JAR (Java Archive) file. Demos: A collection of straightforward unsupported demos that utilized several JAF features which may be found in this directory. A directory containing the Javadoc API descriptions for the JAF’s public classes serves as documentation.

Example of Receiving Email in Java

Given below is the example mentioned:

Code:

package TestNG;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import com.sun.mail.pop3.POP3Store;
public class NewTest{
public static void receiveEmail(String hst, String stype,
String user, String password) {
try {
Properties props = new Properties();
props.put("mail.store.protocol", "pop3");
props.put("mail.pop3s.host", hst);
props.put("mail.pop3s.port", "995");
props.put("mail.pop3.starttls.enable", "true");
Session sess = Session.getDefaultInstance(props);
Store st = sess.getStore("pop3s");
st.connect(hst, user, password);
Folder emailFolder = st.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println("Welcome To Email");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContent().toString());
}
emailFolder.close(false);
st.close();
} catch (NoSuchProviderException e) {e.printStackTrace();}
catch (MessagingException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
}
public static void main(String[] args) {
String host = "pop.gmail.com";
String stypes = "pop3";
String username= "[email protected]";
String password= "xodbizaoiqijifre";
receiveEmail(host, stypes, username, password);
}
}
Copy after login

Output:

Receiving Email in Java

Explanation:

  • In the above example, we used to receive email from the java mail services.
  • Here using the store, pop3, and other smtp services to connect the specific mail users.
  • Using Folder and Messages[] arrays is to create the instance and store the datas at a specific time.
  • For the loop to iterate the datas and to validate the required condition, finally it prints all the datas.
  • The main method will call the method and once again they create the connection instance to perform this operation.

FAQs

Given below are the FAQs mentioned:

Q1. Define Java Receiving email.

Answer:

Using the JavaMail API to retrieve or receive a basic email with underlying protocols have no bearing on this layer at all.

Q2. What are the protocols used to receive the email in java?

Answer:

  • IMAP
  • POP3

Among that IMAP is an advanced protocol for receiving messages.

Q3. How can Java email be received by me?

Answer:

  • Acquire the session object.
  • Connect to the pop server and construct the POP3 or store object.
  • Open the folder object after creating it.
  • Print an array of the messages you’ve retrieved from the folder.
  • Shut off the folder and store objects.

Conclusion

The program receives email messages as HTTP requests. We must associate email addresses with servlets in the application setup in order to process incoming email messages, we should include the servlet code in the app. Then the proper servlets receive HTTP requests from the incoming email and handle them.

The above is the detailed content of Receiving Email in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!