Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

email help

  Asked By: Koila    Date: Nov 20    Category: Java    Views: 1581
  

i need help writing a methodthat will print out the content of all
the emails in the vector from email handler. I have yet to start
this method because i don't even know where to start. But i know i
want it in the same format as the date search with the method in the
Emailhandler class and then the printing in the EmailHandlerTest
Class. ANy help would be great.



import java.util.*;
public class EmailHandler {

private Vector emails;

public EmailHandler() {
emails = new Vector();
}
public EmailHandler(int size) {
emails = new Vector(size);
}

public void store(SimpleEmail e) {
emails.addElement(e);
}


public SimpleEmail RetrieveSubject(String subject) {
SimpleEmail thisEmail;
// look through the email vector if there are elements in it
if (emails.size() > 0) {
for (int i = 0; i< emails.size(); i++) {
// take each email in turn, using a cast to set the class of the
object
thisEmail = (SimpleEmail) emails.elementAt(i);
// check the subject of this email against the incoming subject
if (thisEmail.getSubject().equalsIgnoreCase(subject)) {
return thisEmail;
}
}
}
// if we are here, then email has not been found
return null;
}



public SimpleEmail RetrieveSender(String sender) {
SimpleEmail thisEmail;
// look through the email vector if there are elements in it
if (emails.size() > 0) {
for (int i = 0; i< emails.size(); i++) {
// take each email in turn, using a cast to set the class of the
object
thisEmail = (SimpleEmail) emails.elementAt(i);
// check the subject of this email against the incoming subject
if (thisEmail.getSender().equalsIgnoreCase(sender)) {
return thisEmail;
}
}
}
// if we are here, then email has not been found
return null;


}


public SimpleEmail RetrieveRecipient(String recipient) {
SimpleEmail thisEmail;
// look through the email vector if there are elements in it
if (emails.size() > 0) {
for (int i = 0; i< emails.size(); i++) {
// take each email in turn, using a cast to set the class of the
object
thisEmail = (SimpleEmail) emails.elementAt(i);
// check the subject of this email against the incoming subject
if (thisEmail.getRecipient().equalsIgnoreCase(recipient)) {
return thisEmail;
}
}
}
// if we are here, then email has not been found
return null;


}


public SimpleEmail RetrieveDate(String dte)
{

Date d = new Date(dte);
SimpleEmail thisEmail; // look through the email vector if there are
elements in it
if (emails.size() > 0) {
for (int i = 0; i< emails.size(); i++) { // take each email in turn,
using a cast to set the class of the object
thisEmail = (SimpleEmail) emails.elementAt(i); // check the subject
of this email against the incoming subject
if (thisEmail.getDate().equals(d)) {
return thisEmail;
}
}
} // if we are here, then email has not been found
return null;
}


}


import java.util.*;
public class EmailHandlerTest {

public static void main (String[] args){

String s = "16/01/1983";

Date today = new Date();
Date birthday = new Date(s);

SimpleEmail e1, e2, e3, e4, e5, e6, e7, e8;
String searchSubject;
String searchSender;
String searchRecipient;
String searchDate;



EmailHandler email = new EmailHandler(20);


e1 = new SimpleEmail("Suresh", " sureshdutt@...", "Sheena
Receiver","
SheenaReciever@...","12212.4224.3443.3","admin@...","
Dutts Independant Traders(DITS)","Verseion 1","yahoo","text","How
are you today?","I just wrote an email class!", today);
e2 = new SimpleEmail("Edie", " edie@...", "john Receiver","
johnReciever@...","12212.4224.3443.3","admin@...","Du
tts Independant Traders(DITS)","Verseion 1","hotmail","text","i'm
not bad","I just wrote an email class!", today);
e3 = new SimpleEmail("Chris", " chris@...", "alan
Receiver","
alanReciever@...","12212.4224.3443.3","admin@...","Du
tts Independant Traders(DITS)","Verseion 2","yahoo","text","thats
good to hear","I just wrote an email class!", birthday);
e4 = new SimpleEmail("Stef", " stef@...", "paul Receiver","
paulReciever@...","12212.4224.3443.3","admin@...","Du
tts Independant Traders(DITS)","Verseion 1","hotmail","text","thanks
for telling me","I just wrote an email class!",today);


email.store(e1);
email.store(e2);
email.store(e3);
email.store(e4);


//look for a email by it's subject
searchSubject = "I'm not Bad";
e5 = email.RetrieveSubject(searchSubject);
if (e5 != null) {
System.out.println( "SimpleEmail: subject check");
System.out.println("From: " + e5.getSender() + e5.getSenderMail());
System.out.println("To: " + e5.getRecipient() + e5.getRecipientMail
());
System.out.println("Subject: " + e5.getSubject());
System.out.println("Message ID: " + e5.getMessageId());
System.out.println("Return Path: " + e5.getReturnPath());
System.out.println("Organisation: " + e5.getOrg());
System.out.println("MIME: " + e5.getMime());
System.out.println("Mailer: " + e5.getMailer());
System.out.println("Content Type: " + e5.getcontentType());
System.out.println("Date: " + e5.getDate().toString());
System.out.println("Message: " + e5.getContent());
}
else {
System.out.println("No such email subject"+ searchSubject +" in the
store.");
}

//look for a email by it's sender
searchSender = "chris";
e6 = email.RetrieveSender(searchSender);
if (e6 != null) {
System.out.println( " \n SimpleEmail: sender check");
System.out.println("From: " + e6.getSender() + e6.getSenderMail());
System.out.println("To: " + e6.getRecipient() + e6.getRecipientMail
());
System.out.println("Subject: " + e6.getSubject());
System.out.println("Message ID: " + e6.getMessageId());
System.out.println("Return Path: " + e6.getReturnPath());
System.out.println("Organisation: " + e6.getOrg());
System.out.println("MIME: " + e6.getMime());
System.out.println("Mailer: " + e6.getMailer());
System.out.println("Content Type: " + e6.getcontentType());
System.out.println("Date: " + e6.getDate().toString());
System.out.println("Message: " + e6.getContent());
}
else {
System.out.println("No such email sender"+ searchSender +" in the
store.");
}

//look for a email by it's recipient
searchRecipient = "paul Receiver";
e7 = email.RetrieveRecipient(searchRecipient);
if (e7 != null) {
System.out.println( " \n \n SimpleEmail: reciever check");
System.out.println("From: " + e7.getSender() + e7.getSenderMail());
System.out.println("To: " + e7.getRecipient() + e7.getRecipientMail
());
System.out.println("Subject: " + e7.getSubject());
System.out.println("Message ID: " + e7.getMessageId());
System.out.println("Return Path: " + e7.getReturnPath());
System.out.println("Organisation: " + e7.getOrg());
System.out.println("MIME: " + e7.getMime());
System.out.println("Mailer: " + e7.getMailer());
System.out.println("Content Type: " + e7.getcontentType());
System.out.println("Date: " + e7.getDate().toString());
System.out.println("Message: " + e7.getContent());
}
else {
System.out.println("No such Recipient"+ searchRecipient +" in the
store.");
}

//look for a email by it's date
searchDate = "16/01/83";
e8 = email.RetrieveDate(searchDate);
if (e8 != null) {
System.out.println( " \n \n SimpleEmail: date check");
System.out.println("From: " + e8.getSender() + e8.getSenderMail());
System.out.println("To: " + e8.getRecipient() + e8.getRecipientMail
());
System.out.println("Subject: " + e8.getSubject());
System.out.println("Message ID: " + e8.getMessageId());
System.out.println("Return Path: " + e8.getReturnPath());
System.out.println("Organisation: " + e8.getOrg());
System.out.println("MIME: " + e8.getMime());
System.out.println("Mailer: " + e8.getMailer());
System.out.println("Content Type: " + e8.getcontentType());
System.out.println("Date: " + e8.getDate().toString());
System.out.println("Message: " + e8.getContent());
}
else {
System.out.println("No such Date"+ searchDate +" in the store.");
}


}


}

import java.util.*;
class SimpleEmail {

private String sender;
private String sendermail;
private String recipient;
private String recipientmail;

private String subject;
private String content;

private String messageID; // unique id for email
private String returnPath; // a return Email if fails
private String org; //company name
private String mime; //version if encoded
private String mailer; //name of mail client used
private String contentType; //text/plain charset= US-ascii

private Date dateOfEmail;

public SimpleEmail(String s,String sm, String r,String rm,String
mi,String rp,String o, String mm, String ml, String ct,String t,
String c, Date d) {

sender = s;
sendermail = sm;
recipient = r;
recipientmail = rm;

messageID = mi;
returnPath = rp;
org = o;
mime = mm;
mailer = ml;
contentType = ct;


subject = t;
content= c;

dateOfEmail = d;
}

public void setSender(String s) {
sender = s;
if(isBlank(sender))
sender = "unknown";
}
public String getSender() {
return sender;
}



public void setSenderMail(String sm) {
sendermail = sm;

if(isBlank(sendermail))
sendermail = "unknown";
}

public String getSenderMail() {
if( !isAddressValid ( sendermail ) ) {
sendermail = " Alert ----> Invalid email address";
}

return sendermail;
}



public void setRecipient(String r) {
recipient = r;
if ( recipient == " ")
recipient = "unknown";
}
public String getRecipient() {


return recipient;
}



public void setRecipientMail(String rm) {
recipientmail = rm;
if ( recipient == " ")
recipientmail = "unknown";
}
public String getRecipientMail() {
if( !isAddressValid ( recipientmail ) ) {
recipientmail = " Alert ----> Invalid email address";
}


return recipientmail;
}



public void setMessageId(String mi) {
messageID = mi;
if ( messageID == " ")
messageID = "unknown";
}
public String getMessageId() {
return messageID;
}




public void setReturnPath(String rp) {
returnPath = rp;
if ( returnPath == " ")
returnPath = "unknown";
}
public String getReturnPath() {
return returnPath;
}



public void setOrg(String o) {
org = o;
if ( org == " ")
org = "unknown";
}
public String getOrg() {
return org;
}



public void setMime(String mm) {
mime = mm;
if ( mime == " ")
mime = "unknown";
}
public String getMime() {
return mime;
}



public void setMailer(String ml) {
mailer = ml;
if ( mailer == " ")
mailer = "unknown";
}
public String getMailer() {
return mailer;
}



public void setcontentType(String ct) {
contentType = ct;
if ( contentType == " ")
contentType = "unknown";
}
public String getcontentType() {
return contentType;
}



public void setSubject(String t) {
subject = t;
if ( subject == " ")
subject = "unknown";

}
public String getSubject() {
return subject;
}

public void setContent(String c) {
content = c;
if ( content == " ")
content = "unknown";
}
public String getContent() {
return content;
}

public void setDate(Date d) {
dateOfEmail = d;
}
public Date getDate() {
return dateOfEmail;
}



boolean isAddressValid(String address){
int opPos=address.indexOf("@");
if(opPos==-1){//no @
return(true);
}
if(address.indexOf("@",opPos)!=-1){//more then one @
return(true);
}

if(address.indexOf(".")<opPos){//checks that there are no
dots //before the @
return(true);
}
if(address.indexOf(".",opPos)== -1){//checks that there is at least
one dot after the @
return(true);

}


return(false);//if none of the flags rose up it means that the email
is valid

}

private boolean isBlank(String str)
{
if(str==null) return true;
if(str.trim().equals("")) return true;
return false;

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on email help Or get search suggestion and latest updates.




Tagged: