I have three classes in the program. This is a part of my assignment. I am doing it for the first time; so you might find something stupid in there.. as i have just tried to get the output my way or based on the assignment requirements. I have one main class and two other class. It is a pretend mail client.
1. Main class - does work of getting the input and storing in the message class.
2. message class - stores and returns the data
3. sendmail class - it is meant to get the stored data in message class and write it in the file.
Now the problem:
I can get stored data in the main class but not in sendmail class.. It writes the value of the data as NULL.
import java.io.*;
import java.io.FileWriter;
import java.util.Scanner;
public class MyPMC_B {
public static void main(String[] args)
{
// TODO Auto-generated method stub
String from = "";
String to = "";
String subject = "";
String emailText = "";
Scanner scanner = new Scanner(System.in);
Message m = new Message();
//System.out.println("HELO mail.swin.edu.au");
if (args.length == 0)
{
System.out.print("From: ");
from = scanner.nextLine();
m.setFrom(from);
System.out.print("To: ");
to = scanner.nextLine();
m.setTo(to);
System.out.print("Subject: ");
subject = scanner.nextLine();
m.setSubject(subject);
System.out.print("Body: ");
emailText = scanner.nextLine();
m.setEmailtext(emailText);
}
else
{
for (int i = 0; i < args.length; i++)
{
if (args[i].toLowerCase().startsWith("from:"))
{
from = args[i];
from = from.substring(5);
m.setFrom(from);
}
}
if ( from.isEmpty())
{
System.out.print("From: ");
from = scanner.nextLine();
m.setFrom(from);
}
for (int i = 0; i < args.length; i++)
{
if(args[i].toLowerCase().startsWith("to:"))
{
to = args[i];
to = to.substring(3);
m.setTo(to);
}
else if(args[i].toLowerCase().startsWith("subject"))
{
subject = args[i];
subject = subject.substring(8);
m.setSubject(subject);
}
else if(args[i].toLowerCase().startsWith("body"))
{
for (int j = i; j < args.length; j++)
{
emailText += args[j]+ " ";
}
m.setEmailtext(emailText);
break;
}
}
if ( to.isEmpty())
{
System.out.print("To: ");
to = scanner.nextLine();
m.setTo(to);
}
if ( subject.isEmpty())
{
System.out.print("Subject: ");
subject = scanner.nextLine();
m.setSubject(subject);
}
if ( emailText.isEmpty())
{
System.out.print("Body: ");
emailText = scanner.nextLine();
m.setEmailtext(emailText);
}
}
//getting the SMTP server from the FROM address
String [] fromadd = from.split("@");
String ServerName = fromadd[1];
m.setServer(ServerName);
SendMail sm = new SendMail();
sm.mail();
String test = m.getTo();
System.out.println(test);
if (sm.mail()==0)
{
System.out.println("The mail was unsuccessful, check the logs");
}
else
{
System.out.println("The mail was sent successfully");
}
System.out.print("Do you want to send another email? (yes/no): ");
String ans = scanner.nextLine();
int n;
if (ans.toLowerCase().startsWith("y"))
{
n = 1;
}
for (n = 0; n <= 1; n++)
{
System.out.print("From: ");
from = scanner.nextLine();
m.setFrom(from);
System.out.print("To: ");
to = scanner.nextLine();
m.setTo(to);
System.out.print("Subject: ");
subject = scanner.nextLine();
m.setSubject(subject);
System.out.print("Body: ");
emailText = scanner.nextLine();
m.setEmailtext(emailText);
String [] fromadd1 = from.split("@");
ServerName = fromadd[1];
m.setServer(ServerName);
SendMail sm1 = new SendMail();
sm1.mail();
if (sm.mail()==0)
{
System.out.println("The mail was unsuccessful, check the logs");
}
else
{
System.out.println("The mail was sent successfully");
}
System.out.print("Do you want to send another email? (yes/no): ");
ans = scanner.nextLine();
if (ans.toLowerCase().startsWith("y"))
{
n = 1;
}
else
{
n = 3;
}
}
}
public static class Message
{
public String from1;
public String to1;
public String subject1;
public String emailText1;
public String server;
public void setFrom(String f)
{
from1 = f;
}
public String getFrom()
{
return from1;
}
public void setTo(String t)
{
to1 = t;
}
public String getTo()
{
return to1;
}
public void setSubject(String s)
{
subject1 = s;
}
public String getSubject()
{
return subject1;
}
public void setEmailtext(String e)
{
emailText1 = e;
}
public String getEmailtext()
{
return emailText1;
}
public void setServer(String n)
{
server = n;
}
public String getServer()
{
return server;
}
}
public static class SendMail
{
Message n = new Message();
String from = n.getFrom();
String to = n.getTo();
String subject = n.getSubject();
String emailText = n.getEmailtext();
String Servername = n.getServer();
int result;
public int mail()
{
try
{
FileWriter fw = new FileWriter("c:\\smtp.txt",true);
// the true parameter in the constructor is for append mode
// \r\n are for end of line
fw.write("HELO "+Servername+"\r\n");
fw.write("MAIL FROM: " +from+"\r\n");
fw.write("MAIL TO: " +to+"\r\n");
fw.write("DATA\r\n");
fw.write("Subject: " +subject+"\r\n");
fw.write(emailText+"\r\n\n");
fw.close();
result = 1;
return result;
}
catch (IOException e)
{
e.printStackTrace();
result = 0;
return result;
}
}
}
}
I know it is long one to go through.. but any help would be highly appreciated.