Hi,
I am able to succesfully able to send the attachment but the body message is not going with it. If i dont send the attachment then the email body goes properly. i can't understand what the problem is . please help.
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import com.sun.mail.smtp.SMTPMessage;
public class Emailer
{
String emailSubject = null;
String emailBody = null;
String smtphost = null;
String ccAddr = null;
String file = "C:\\trainee\\eclipse_lg.gif";
Vector vecToAddr = null;
Vector vecCCAddr = null;
Address fromAddr = null;
public Emailer(Hashtable hashConfigParam) throws Exception
{
smtphost = (String)hashConfigParam.get("host");
ccAddr = (String)hashConfigParam.get("ccAddr");
vecToAddr = new Vector();
vecCCAddr = new Vector();
}
public Boolean setFromAddr(String fromEmailAddr)
{
try
{
fromAddr = new InternetAddress (fromEmailAddr);
//if(true)
//throw new IOException();
}
catch (Exception e)
{
return null;
}
return new Boolean(true);
}
public Boolean setToAddr(String toEmailAddr)
{
try
{
for(int j=0; j< vecToAddr.size();j++)
{
if(((String)vecToAddr.get(j)).equals(toEmailAddr))
return new Boolean(false);
}
vecToAddr.add(toEmailAddr);
//if(true)
//throw new IOException();
}
catch (Exception e)
{
return null;
}
return new Boolean(true);
}
public Boolean setCCAddr(String ccEmailAddr)
{
try
{
for(int j=0; j< vecCCAddr.size();j++)
{
if(((String)vecCCAddr.get(j)).equals(ccEmailAddr) && ((String)vecCCAddr.get(j)).equals(ccAddr))
return new Boolean(false);
}
vecCCAddr.add(ccEmailAddr);
vecCCAddr.add(ccAddr);
//if(true)
//throw new IOException();
}
catch (Exception e)
{
//System.out.println(e.getClass().getName());
return null;
}
return new Boolean(true);
}
public Boolean setEmailSubject(String subject)
{
try
{
emailSubject = subject;
if(emailSubject.equals(null))
return new Boolean(false);
else
return new Boolean(true);
}
catch (Exception e)
{
return null;
}
//return new Boolean(false);
}
public Boolean setEmailBody(String body)
{
try
{
emailBody = body;
if(emailBody.equals(""))
return new Boolean(false);
else
return new Boolean(true);
}
catch (Exception e)
{
return null;
}
//return new Boolean(false);
}
public void sendEmail()
{
try
{
Properties eMailConfigProps = null;
eMailConfigProps = System.getProperties();
eMailConfigProps.put("mail.smtp.host", smtphost);
Session session = Session.getInstance(eMailConfigProps, null);
MimeMessage message = new MimeMessage(session);
try
{
message.setFrom(fromAddr);
for(int i=0; i< vecToAddr.size(); i++)
{
message.addRecipient(Message.RecipientType.TO,new InternetAddress((String)vecToAddr.get(i)));
}
for(int i=0; i< vecCCAddr.size(); i++)
{
message.addRecipient(Message.RecipientType.CC,new InternetAddress((String)vecCCAddr.get(i)));
}
message.setSubject(emailSubject);
//message.setText(emailBody);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(emailBody);
Multipart multipart = new MimeMultipart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
}
catch (SendFailedException ex)
{
ex.printStackTrace();
}
catch (MessagingException ex)
{
System.err.println("Exception. " + ex);
}
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
public static void main(String args[])
{
Hashtable hash = new Hashtable();
hash.put("host","213.312.230.211");
hash.put("ccAddr","admin@dfgdfg.dfgdfg.in");
try
{
Emailer objEmailer = new Emailer(hash);
Boolean fromFlag = objEmailer.setFromAddr("nb99@yahoo.com");
String toAddresses = "shamim@ghghg.net";
String ccAddresses = "shamim@ghghg.net";
Boolean toFlag = objEmailer.setToAddr(toAddresses);
Boolean ccFlag = objEmailer.setCCAddr(ccAddresses);
Boolean subjectFlag = objEmailer.setEmailSubject("Emailer.java");
Boolean bodyFlag = objEmailer.setEmailBody("blah blah blahblha ");
if((fromFlag.toString()).equals("true") && (toFlag.toString()).equals("true") && (bodyFlag.toString()).equals("true"))
objEmailer.sendEmail();
}
catch(AddressException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}