adspace


how to write a program for sending mails between client and
server

Answer Posted / Deep Kumar

To send emails between a client and a server using Java, you can use the JavaMail API as mentioned in the previous answer. In this case, however, the server will need to have a separate process running to handle incoming emails.nnOn the client side, the code for sending an email remains the same. On the server side, you'll need to set up a Java program that listens for incoming emails and handles them accordingly. Here's an example of how to receive an email using a separate Java thread:n
1. First, create a class for handling incoming emails:n```java
import javax.mail.*;
import java.io.IOException;
import java.util.Properties;
npublic class EmailReceiver {
public static void main(String[] args) throws MessagingException {
Properties props = System.getProperties();
props.put("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props);
Store store = session.getStore("imaps");
store.connect("your_imap_server");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);

Message[] messages = inbox.search(new FlagTerm(new Flags.Flag.InstantiatedFlag(Flags.Flag.SEEN), false));
for (Message message : messages) {
// Process the email here
}
}n}n```
2. To process incoming emails, you can extract relevant information such as the sender, subject, and body of the email and take appropriate actions based on your requirements.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Differentiate between static and non-static methods in java.

1125


Explain public static void main(string args[]) in java.

1073


What is an object in java and how is it created?

1138


Write a program to find the whether a number is an Armstrong number or not?

1096


What are the differences between heap and stack memory in java?

1135


What is a classloader in java?

1086


Realized?

2259


What is parsing in java?

1038


How to sort array in descending order in java?

993


How to create a base64 decoder in java8?

1135


What is a constructor overloading in java?

1124


What is the difference between equals() and == in java?

1037


explain different ways of using thread? : Java thread

1079


Write a program to print count of empty strings in java 8?

1084


What is java string pool?

1079