Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

program to send/receive emails  RSS feed

 
ganti hari kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to develop a mailing system to send or receive mails using the coding as below.but im getting many compilation errors.please help. OR give me a new program thank u.
import java.io.*;
import java.net.*;
public class SmtpDemo
{
public static void main(String [] args)
{
String msgfile;
String from,to,mailhost;
CheckEmailAddress(from);
CheckEmailAddress(to);
SMTP mail=new SMTP(mailhost);
if(mail!=null)
{
if(mail.send(new FileReader(msgfile),from,to))
{
System.out.println("Mail sent");
}
else
{
System.out.println("Connection to SMTP failed");
}
}
System.out.println("Done");
}
static void CheckEmailAddress(String addresses)
{
if(address.indexOf('@')==-1)
{
System.out.println("Invalid Email Address'"+ address+"'");
System.exit(10);
}
}
}

class SMTP
{
public final static int SMTP_PORT =25;
InetAddress mailhost;
InetAddress ourselves;
BufferedReader in;
PrintWriter out;

public SMTP(String host) throws UnknownHostException
{
mailhost=InetAddress.getByName(host);
ourselves=InetAddress.getLocalHost();
System.out.println("mailHost =" +mailhost);
System.out.println("localHost =" +ourselves);
System.out.println("Smtp constructor done");
}
public boolean send(FileReader msgg,String from,String to)
throws IOException
{
Socket smtpPipe;
InputStream inn;
OutputStream outt;
BufferedReader msg;
msg=new BufferedReader(msgg);
smtpPipe= new Socket(mailhost,SMTP_PORT);
if(smtpPipe==null)
{
return false
}
inn= smtpPipe.getInputStream();
outt=smtpPipe.getOutputStream();
in = new BufferedReader(new InputStreamReader(inn));
out= new PrintWriter(new OutputStreamWriter(outt),true);
if(inn==null | | outt==null)
{
System.out.println("Failed to open Stream to Socket");
return false;
}

String InitialID=in.readLine();
System.out.println(InitialID);
System.out.println("Hello "+ourselves.getHostName());
System.out.println("Hello "+ourselves.getHostName());
String Welcome =in.readLine();
System.out.println(Welcome);
System.out.println("Mail from :<"+from +">");
System.out.println("Mail to :<" + to +">");
String senderOk = in.readLine();
System.out.println(senderOk);
System.out.println("RCPT to:< " + to+">");
System.out.println("RCPT to:<"+ to+">");
String recipientOk = in.readLine();
System.out.println(recipientOk);
System.out.println("Data");
System.out.println("Data");
String line;
while((line=msg.readLine())!=null)
{
out.println(line);
}
System.out.println(".");
System.out.println(",");
String acceptedOk = in.readLine();
System.out.println("Quit");
System.out.println("Quit");
return true;
}
}
}

------------------
Thank u!
harikumar
 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A colleague of mine has the O'Reilly book 'Java Examples in a Nutshell' which has some examples of a mail program in java. You'll find a link to it in the Bunkhouse.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!