ForwardEmail - Yash-777/Java_Mail GitHub Wiki
package javax.mail; // mail-1.4.5.jar
class EventQueue implements Runnable
package com.sun.mail.imap;
ublic class IMAPStore extends Store
implements QuotaAwareStore, ResponseHandler {
## Properties
#defines the various attributes like login credentials and mail ids
username=4033871943370
password=
toAddress=
mail.store.protocol=pop3
mail.pop3.host=193.177.238.180
mail.pop3.port=110
mail.smtp.host=smtpmail.onmicrosoft.com
mail.smtp.port=25
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class ForwardEmail {
private Properties properties = null;
private Session session = null;
String toId;
private void loadServerProperties() {
try {
this.properties = new Properties();
InputStream inputStream = getClass().getResourceAsStream("config.properties");
this.properties.load(inputStream);
this.toId = this.properties.getProperty("toAddress");
System.out.println("Senders Address---" + this.toId);
this.session = Session.getInstance(this.properties,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(ForwardEmail.this.properties
.getProperty("username"), ForwardEmail.this.properties
.getProperty("password"));
}
});
System.out.println("POP Authenticated successful------------------------------");
} catch (IOException e1) {
e1.printStackTrace();
}
}
private void readSendEmail() throws AddressException, MessagingException {
this.session.setDebug(true);
Store store = this.session.getStore("pop3");
store.connect();
Folder folder = store.getFolder("inbox");
folder.open(2);
Message[] messages = folder.getMessages();
System.out.println("Messages sent...... ." + messages.length);
if (messages.length != 0) {
for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
MimeMessage mimeMessage = new MimeMessage(this.session);
mimeMessage.setRecipients(Message.RecipientType.TO,
(Address[])InternetAddress.parse(this.toId));
mimeMessage.setSubject(message.getSubject());
mimeMessage.setFrom((Address)new InternetAddress(InternetAddress.toString(message
.getRecipients(Message.RecipientType.TO))));
mimeMessage.setSentDate(message.getSentDate());
MimeBodyPart messageBodyPart = new MimeBodyPart();
MimeMultipart mimeMultipart = new MimeMultipart();
messageBodyPart.setContent(message, "message/rfc822");
mimeMultipart.addBodyPart((BodyPart)messageBodyPart);
mimeMessage.setContent((Multipart)mimeMultipart);
mimeMessage.saveChanges();
message.setFlag(Flags.Flag.DELETED, true);
System.out.println("Read and Delete Sucessful.");
Transport t = this.session.getTransport("smtp");
t.connect();
t.sendMessage((Message)mimeMessage, mimeMessage.getAllRecipients());
t.close();
System.out.println("Email sent successfully.");
}
folder.close(true);
store.close();
}
}
public static void main(String[] args) throws AddressException, MessagingException {
ForwardEmail mail = new ForwardEmail();
mail.loadServerProperties();
mail.readSendEmail();
}
}