• Post Reply Bookmark Topic Watch Topic
  • New Topic

basic mail doubt

 
Prabhu Chandrasekaran
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Iam trying out a mailing code which will mail via a smtp server. It is compiling without a problem.
but on execution of the same.. the following msgs are shown :
DEBUG: SMTPTransport connected to host "193.1.1.22", PORT: 25
DEBUG SMTP SENT: NOOP
DEBUG SMTP RCVD: 500 unknown ??? command ""
java.lang.IllegalStateException: Not Connected
The code is as follows :
import java.io.*;
import java.util.*;
import java.net.InetAddress;
//mail imports for activation.jar and mail.jar
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.Multipart;
import javax.mail.BodyPart;
import javax.activation.DataSource;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
class test
{
public static void main(String[] args)
{
try
{
HashMap x=new HashMap();
x.put("mailServer","193.1.1.22");
x.put("toAddress","prabhu_vc@yahoo.com");
x.put("subject","sent by vcp");
x.put("message","congratulations on ur first step");
boolean sent=sendMail(x);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public static boolean sendMail(HashMap mailDetails) throws Exception
{
try
{
boolean debugFlag=true;
String mailServer=(String)mailDetails.get("mailServer");
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties,null);
session.setDebug(debugFlag);
Message mess = new MimeMessage(session);
if(mailDetails.get("toAddress")!=null)
{
//String to[] =(String[])mailDetails.get("toAddress");
String to[] = StringSplit.splitString((String)mailDetails.get("toAddress"),",");
InternetAddress toInternetAddress[] = new InternetAddress[to.length];
for(int i=0;i<to.length;i++)
{
toInternetAddress[i]=new InternetAddress(to[i].trim());
}
mess.setRecipients(Message.RecipientType.TO,toInternetAddress);
}
String message=(String)mailDetails.get("message");
String attachment[] = (String[])mailDetails.get("attachment");
properties.put("mail.smtp.host",mailServer); // get snmphost property
String from = (String)mailDetails.get("fromAddress");
if(from!=null && !from.equals(""))
mess.setFrom(new InternetAddress(from));
mess.setSentDate(new Date());
if(mailDetails.get("ccAddress")!=null)
{
String cc[] =(String[])mailDetails.get("ccAddress");
InternetAddress ccInternetAddress[] = new InternetAddress[cc.length];
for(int i=0;i<cc.length;i++)
{
ccInternetAddress[i]=new InternetAddress(cc[i].trim());
}
mess.setRecipients(Message.RecipientType.CC,ccInternetAddress);
}
mess.setSubject((String)(mailDetails.get("subject")));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
if(attachment!=null)
{
for(int i=0;i<attachment.length;i++)
{
File f = new File(attachment[i].trim());
if(f.exists())
{
messageBodyPart = new MimeBodyPart();
DataSource dataSource = new FileDataSource(attachment[i]);
messageBodyPart.setDataHandler(new DataHandler(dataSource));
messageBodyPart.setFileName(f.getName());
multipart.addBodyPart(messageBodyPart);
}
}
}
mess.setContent(multipart);
mess.saveChanges();
try
{
Transport.send(mess);
}
catch(Exception ex)
{
System.out.println("While Sending Only .... "+ex);
}
}
catch(Exception ex)
{
System.out.println("inside fn :"+ex);
}
return true;
}
}

I would be very much thankful if any of u can reply with some clarifications.
Advance Thanks,
Prabhu.V.C
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!