File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes basic mail doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply Bookmark "basic mail doubt" Watch "basic mail doubt" New topic
Author

basic mail doubt

Prabhu Chandrasekaran
Ranch Hand

Joined: Oct 14, 2000
Posts: 36
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
 
 
subject: basic mail doubt
 
Threads others viewed
Attachment in email not working in server
how to send mail using gmail server
JavaMail installation
Having Problem in sending mail
Sending mail thru JAVA
developer file tools