Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java MAil & HTML attachment

Status
Not open for further replies.

jshanoo

Programmer
Apr 2, 2002
287
IN
Hi All
I have java mail application which send mail in an infinite loop.
Now my company has decided to add a footer to the existing mail.
so that compnay logo should refelect in all teh mails.
I made a HTML file and then made as second attachment mail.

But ;( it gave me the following error message

Exception in thread "main" java.lang.ClassFormatError: sendmail (Bad magic number)
above all i am novice in this a java
please help me

Regards
John

*** Even the Best, did the Bad and Made the Best ***

John Philip
 
The API doc says that this is "Thrown when the Java Virtual Machine attempts to read a class file and determines that the file is malformed or otherwise cannot be interpreted as a class file."

Can you post the code which is throwing this error. Also, are you using an old JVM or non-Sun JVM ?
 
The first four bytes of a class file are the "magic number". It should be "CA FE BA BE".

[ On very old java compilers it could be different ].
On a windows pc you can check this in a Dos-box via the command "debug". [ first copy your "sendmail.class" file to "sendmail.exe" , then type "debug sendmail.exe" and aftet the dash - type a "d" (for dump). The first four bytes should be CA FE BA BE ]
 
hologram :

Well I never did ! Any idea why, and incidentally how did you come by that nugget of info ?!

Prize for "most obscure knowledge about java" goes to you !
 
Haven't you read the "Java Virtual Machine Specification" ? Chapter 4 describes the class file format :


...

4.1 The ClassFile Structure
A class file consists of a single ClassFile structure:

ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}

The items in the ClassFile structure are as follows:

magic
The magic item supplies the magic number identifying the class file format; it has the value 0xCAFEBABE.

minor_version, major_version

....

4.9.1 The Verification Process
The class file verifier operates in four passes:
Pass 1:
When a prospective class file is loaded (§2.17.2) by the Java virtual machine, the Java virtual machine first ensures that the file has the basic format of a class file. The first four bytes must contain the right magic number. All recognized attributes must be of the proper length. The class file must not be truncated or have extra bytes at the end. The constant pool must not contain any superficially unrecognizable information

....

PS : I haven't read the "Java Virtual Machine Specification" either. ;-)
 
HI,
Below the Java sendmail source code , and i am not aware of any JVM new or old.
Below is the complete source code.

Regards
John
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;

public class sendmail
{
static Connection cn;
static Statement stat;
static Statement stat1;
static Statement stat2;
static Statement stat3;
static String proxyname = "" ;
static String servername= "";
static String mailid="";



private static String mailHost = "";

public String sendMail(String strTo, String strSubject, String strMailText, String strFrom, String strattachfile, String strTitle, String strCity,String strContract,String strMailTime) throws Exception //MessagingException
{

Properties properties = System.getProperties();
Session session = Session.getInstance(properties,null);
ConnectionHandler conHandler = new ConnectionHandler();
TransportHandler transHandler = new TransportHandler();
Message message = new MimeMessage(session);
InternetAddress address_to = new InternetAddress(strTo);
String formatter;
String SimpleDateFormat;
message.setRecipient(Message.RecipientType.TO,address_to);

//if (strContract=="AKAI") {
message.setFrom(new InternetAddress(strFrom,"JustDial Feedback Alert"));
//message.setFrom(new InternetAddress(strFrom,"AKAI - Call Centre"));
//}
//else {
//message.setFrom(new InternetAddress(strFrom,"JustDial Feedback Alert"));
//}
//message.setFrom(new InternetAddress(strFrom,"1947 Feedback Alert"));
//Address address = new InternetAddress("president@whitehouse.gov", "George Bush");
//Date formatter starts here
// The hour (1-12)
// formatter = new SimpleDateFormat("h"); // 8
// formatter = new SimpleDateFormat("hh"); // 08

// The hour (0-23)
// formatter = new SimpleDateFormat("H"); // 8
// formatter = new SimpleDateFormat("HH"); // 08

// The minutes
// formatter = new SimpleDateFormat("m"); // 7
// formatter = new SimpleDateFormat("mm"); // 07

// The seconds
// formatter = new SimpleDateFormat("s"); // 3
// formatter = new SimpleDateFormat("ss"); // 03

// The am/pm marker
// formatter = new SimpleDateFormat("a"); // AM

// The time zone
// formatter = new SimpleDateFormat("z"); // PST
// formatter = new SimpleDateFormat("zzzz"); // Pacific Standard Time
// formatter = new SimpleDateFormat("Z"); // -0800

// Get today's date
// Date date = new Date();

// Some examples
// formatter = new SimpleDateFormat("hh:mm:ss a");
// String s = formatter.format(date);
// 01:12:53 AM

// formatter = new SimpleDateFormat("HH.mm");
// s = formatter.format(date);
// 14.36.33
// End of date format
java.util.Date d=new java.util.Date();
String tm=d.getHours()+":"+d.getMinutes();
//if (strContract == "AKAI") {
// message.setSubject(" CC C # " + strTitle);
// strMailText=" AKAI Complaint ";
//}
//else {
message.setSubject( strCity+ " # " + strTitle + strSubject + " " + strMailTime);
//}
//message.setSubject("Mumbai : Enquires for the Day");
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(strMailText);

// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
MimeBodyPart mbp3 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds=new FileDataSource("c:\\program files\\unijust\\data_out\\" + strattachfile);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName("JDS.txt");
FileDataSource fds1=new FileDataSource("c:\\program files\\unijust\\data_out\\test1.htm");
mbp3.setDataHandler(new DataHandler(fds1));
mbp3.setFileName("JDS.htm");
// create the Multipart
//and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
mp.addBodyPart(mbp3);
// add the Multipart to the message
message.setContent(mp);
message.saveChanges();

Transport transport = session.getTransport("smtp");

transport.addConnectionListener(conHandler);
transport.addTransportListener(transHandler);
transport.connect(mailHost,"","");
transport.sendMessage(message,message.getAllRecipients());

String transportStatus = "notSet";
boolean transportFlag = false;
while(!transportFlag)
{
if(transportStatus.equals("successful"))
transportFlag = true;
else if(transportStatus.equals("part"))
transportFlag = true;
else if(transportStatus.equals("not"))
transportFlag = true;
transportStatus = transHandler.getStatus();
}
transport.close();

return transportStatus;
}

class TransportHandler extends TransportAdapter
{
String finalStatus= "notSet";

public void messageDelivered(TransportEvent e)
{
setStatus("successful");
System.out.println(">>>> Message delivered successfully");
}

public void messageNotDelivered(TransportEvent e)
{
setStatus("not");
System.out.println(">>>> Message NOT delivered");
}

public void messagePartiallyDelivered(TransportEvent e)
{
setStatus("part");
System.out.println(">>>> Message Partially delivered");
}

public void setStatus(String indata){
finalStatus = indata;

}

public String getStatus(){
return finalStatus;
}
}

class ConnectionHandler extends ConnectionAdapter
{
public void opened(ConnectionEvent e)
{
System.out.println(">>>> Connection opened");
}

public void disconnect(ConnectionEvent e)
{
System.out.println(">>>> Connection disconnected");
}

public void closed(ConnectionEvent e)
{
System.out.println(">>>> Connection closed");
}
}

public static void main(String args[])
{
sendmail s=new sendmail();

ResultSet out_mail=null;
while(true)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:eek:dbc:javadsn");

stat1=cn.createStatement();
stat2=cn.createStatement();
stat3=cn.createStatement();
}
catch(Exception e){ System.out.println("DSN Error " + e);}
System.out.println("DSN Connected");
try{
out_mail=stat1.executeQuery("select * from out_mail where outgoingstatus like 'NO%'") ;
}
catch(Exception e){ System.out.println("File Not Found " + e);}
System.out.println("outfile read Connected");

try{
ResultSet smtp=stat2.executeQuery("select * from smtpserver") ;
smtp.next();
proxyname=smtp.getString("proxyname");
servername=smtp.getString("servername");
mailHost = servername ;
mailid=smtp.getString("from");
smtp.close();

}
catch(Exception e){ System.out.println("File Not Found " + e);}
System.out.println("SMTP read");

String mailSubject = ", enquiry for you at ";
String mailText = "Dear Sir/Madam, \n \n Please Find JustDial Services - Feedback Report attached as text file.\n\n Regards \n Customer Service Department \n JustDial Services";
//String mailText = "Dear Sir/Madam, \n \n Please Find 1947 - Feedback Report attached as text file.\n\n Regards \n Customer Service Department \n 1 9 4 7";
try{
stat3.executeUpdate("update parameter set parameter_value=1 where parameter_type like 'EMAILSTATUS'");
stat3.close();
}
catch(Exception e){ System.out.println("File Not Found " + e);}
try
{
System.out.println("Before outmail");
while(out_mail.next())
{
System.out.println("test");
java.util.Date d=new java.util.Date();
String dt=d.getMonth()+1+"/"+d.getDate()+"/"+(d.getYear()+1900);
String tm=d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
Connection cnn=null;
cnn=DriverManager.getConnection("jdbc:eek:dbc:javadsn");
try
{

String toutmsgid = out_mail.getString("outmsgid");
s.sendMail(out_mail.getString("outemailnumber"),mailSubject,mailText,mailid,out_mail.getString("outemailfile"),out_mail.getString("inmsg"),out_mail.getString("mcity"),out_mail.getString("contract"),out_mail.getString("outemailtime"));
stat=cnn.createStatement();
stat.executeUpdate("update out_mail set outgoingstatus='YES',outemaildate='"+ dt +"', outemailtime ='"+ tm +"' where outmsgid like '"+toutmsgid+"'");
System.out.println("update out_mail set outgoingstatus='YES' where outmsgid like '"+toutmsgid+"'");
stat.close();

}

catch(Exception e)
{
System.out.println("Error from main : " + e);
try{
Statement st = cnn.createStatement();
//st.executeUpdate("insert into error_msg values('"+ dt +"', '"+ tm +"','Java Mail"+ e +"')");
st.close();
}catch(Exception ee){System.out.println("Other Error :"+ee);}
}
////////////// updating parameter /////
try
{
stat3=cn.createStatement();
//stat3.executeUpdate("update parameter set parameter_value=0 where parameter_type like 'EMAILSTATUS'");
stat3.close();
}catch(Exception e){ System.out.println("paramter value update error " + e);}

cnn.close();
}// end of while loop
out_mail.close();
}
catch(Exception e){System.out.println("Error :"+e);}
} // closing contineous loop
} // end of main function

}// end of CLass



*** Even the Best, did the Bad and Made the Best ***

John Philip
 
>>>>>Below the Java sendmail source code , and i am not aware of any JVM new or old.


You don't know what version of the SDK and JVM you are using ?

Type "java -version".


As we have hinted at before, this problem looks like it is due not to the source code, but because the JVM cannot interpret your .class file as valid (see my first post). So, please follow the previous advice above and check that the class file is valid (see holograms first post). Also please report your JVM vendor and version.
 
Hi Sedj
D:\>java -version
this is the machine where i compiled the java

java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

//this machine where i deploy the sendmail.class
C:\Program Files\unijust>java -version
java version "1.2.2"
Classic VM (build JDK-1.2.2_012, native threads, symcjit)


please comment, is this problem of attaching html as second attachemnt to teh mail.

Regards
john

*** Even the Best, did the Bad and Made the Best ***

John Philip
 
Well, straight off, you're problem is that you are compiling the code on a 1.4 machine, and running it on a 1.2 machine.

While sometimes, if using standard packages, it is possible to compile on, say 1.4 and run on 1.3, it is not good practice. There have been some major changes between 1.4 and 1.2 - so you should compile and execute using the same major JVM and SDK version.

>>>>> please comment, is this problem of attaching html as second attachemnt to teh mail.

As I said before, it has nothing to do with the code - it is the JVM versions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top