Related differences

Ques 26. Sample code for deleting messages using JavaMail.

If you want to delete any message then set the message flag delete. There are different types of flags, some system-defined and some user-defined.

* Flags.Flag.ANSWERED* Flags.Flag.DELETED* Flags.Flag.DRAFT* Flags.Flag.FLAGGED* Flags.Flag.RECENT* Flags.Flag.SEEN* Flags.Flag.USERTo delete messages, you set the message's DELETED flag:message.setFlag(Flags.Flag.DELETED, true);Open up the folder in READ_WRITE mode first though:folder.open(Folder.READ_WRITE);Then, when you are done processing all messages, close the folder, passing in a true value to expunge the deleted messages.

folder.close(true);


package com.withoutbook.common;

import com.sun.mail.imap.protocol.FLAGS;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;

public class DeleteMail {

public static void main(String args[]) throws Exception {

Properties properties = System.getProperties();

Session session = Session.getDefaultInstance(properties);
Store store = session.getStore("pop3");

store.connect("192.168.10.110", "arindam", "arindam");

Folder folder = store.getFolder("inbox");

if (!folder.exists()) {
System.out.println("inbox not found");
System.exit(0);
}

folder.open(Folder.READ_WRITE);

Message[] msg = folder.getMessages();
//System.out.println((messages.length+1)+" message found");
for (int i = 0; i < msg.length; i++) {
System.out.println("--------- " + (i + 1) + "------------");
String from = InternetAddress.toString(msg[i].getFrom());
if (from != null) {
System.out.println("From: " + from);
}

String replyTo = InternetAddress.toString(
msg[i].getReplyTo());
if (replyTo != null) {
System.out.println("Reply-to: " + replyTo);
}
String to = InternetAddress.toString(
msg[i].getRecipients(Message.RecipientType.TO));
if (to != null) {
System.out.println("To: " + to);
}

String subject = msg[i].getSubject();
if (subject != null) {
System.out.println("Subject: " + subject);
}
Date sent = msg[i].getSentDate();
if (sent != null) {
System.out.println("Sent: " + sent);
}
System.out.println("Message : ");
System.out.println(msg[i].getContent());

}
System.out.println("Enter message no to delete :");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String no = br.readLine();
msg[Integer.parseInt(no) - 1].setFlag(FLAGS.Flag.DELETED, true);
System.out.println("Msg Delete .....");

folder.close(true);
store.close();
}
}

Is it helpful? Add Comment View Comments
 

Ques 27. Sample code to send HTML mail with images using JavaMail.

A client create new message by using Message subclass. It sets attributes like recipient address and the subject, and inserts the content into the Message object, and inserts the content into the Message object. Finally, it sends the Message by invoking the Transport.send() method.

The Transport class models the transport agent that routes a message to its destination addresses. This class provides methods that send a message to a list of recipients. Invoking the Transport.send() method with a Message object identifies the appropriate transport based on its destination addresses.


package com.withoutbook.common;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class HTMLMail {

public static void main(String args[]) throws Exception {

String host = "192.168.10.110";
String from = "arindam@localhost";
String to = "arindam@localhost";

// Get system properties
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

// Get the default Session object.
Session session = Session.getDefaultInstance(properties);

// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

// Set the RFC 822 "From" header field using the
// value of the InternetAddress.getLocalAddress method.
message.setFrom(new InternetAddress(from));

// Add the given addresses to the specified recipient type.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));


// Set the "Subject" header field.
message.setSubject("hi..!");


String htmlText = "

Hello

" +
"";

// Sets the given String as this part's content,
// with a MIME type of "text/plain".
message.setContent(htmlText, "text/html");

// Send message
Transport.send(message);

System.out.println("Message Send.....");
}
}

Is it helpful? Add Comment View Comments
 

Most helpful rated by users: