JavaMail API dropping attachments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John P

    JavaMail API dropping attachments

    Hello,

    I'm building email messages and attachments via the JavaMail API,
    which are then sent via sendmail. The attachments are mime attachments
    of type "applicatio n/octet-stream". They are received via fine via
    lotus notes and outlook, but in outlook express, yahoo, and hotmail
    the attachment does not appear. Yahoo will even report the correct
    message size. Any ideas on what the issue could be?

    Any help would be greatly appreciated

    John P.

    below is the code:



    package Frs.Tags;

    import org.w3c.dom.*;
    import Frs.*;
    import java.util.Prope rties;
    import javax.mail.*;
    import javax.mail.inte rnet.*;
    import javax.activatio n.*;
    import java.io.*;
    import java.util.*;
    import java.sql.*;
    import oracle.sql.*;

    public class tagSENDMAIL_ATT ACHMENT implements TagHandler {
    public String handleTag(Eleme nt el, FrsState state) throws
    Exception {

    // Get system properties
    Properties props = new Properties();
    Blob blob = null;
    byte[] bytes = null;
    String attachment_name = "";
    // Setup mail server
    props.put("mail .smtp.host", "blah.blah.com" );

    // Get session
    Session sess = Session.getInst ance(props, null);

    if(sess == null){
    props.clear();
    props.put("mail .smtp.host", "blah2.blah.com ");
    sess = Session.getInst ance(props, null);
    }

    // Define message
    MimeMessage message = new MimeMessage(ses s);

    // ID
    String id = FrsUtilAPI.getR equiredField(el , "ATTACHMENT_ID" ,
    state);

    if(!id.equals(" ") && id != null){
    String query = "select name, img_blob from lds_image where id = "
    + id;
    DbTool dbTool = null;
    try
    {
    dbTool = DbTool.getDbCon nection(query, DbTool.EXECUTE_ QUERY);
    ResultSet result = dbTool.getResul tSet();
    if (!result.next() )
    throw new Exception("Debu g: cannot not fetch image from
    db");
    else{
    attachment_name = result.getStrin g(1);
    blob = result.getBlob( 2);
    bytes = blob.getBytes(1 , (int) blob.length());
    }
    }

    catch (SQLException sqle)
    {
    if (dbTool != null)
    {
    dbTool.close();
    dbTool = null;
    }
    }
    catch (Exception e)
    {
    System.out.prin tln("Debug: cannot get image id = " + id);
    }

    if (dbTool != null)
    {
    dbTool.close();
    dbTool = null;
    }
    }
    // Set the from address
    String from = FrsUtilAPI.getR equiredField(el , "FROM", state);
    System.out.prin tln("FROM " + from);
    if(!from.equals ("") && from != null){
    Address address = new InternetAddress (from);
    message.setFrom (address);

    // Set the to address
    String to = FrsUtilAPI.getR equiredField(el , "TO", state);
    System.out.prin tln("TO " + to);
    Address address2 = new InternetAddress (to);
    message.setReci pient(Message.R ecipientType.TO ,address2);

    // Set the subject
    String subject = FrsUtilAPI.getR equiredField(el , "SUBJECT",
    state);
    System.out.prin tln("SUBJECT " + subject);
    message.setSubj ect(subject);

    // Get the switch value
    String switch_value = FrsUtilAPI.getR equiredField(el ,
    "SWITCH", state);
    System.out.prin tln("SWITCH " + switch_value);

    if(switch_value == null)
    switch_value = "";

    // Set the content
    String body = FrsUtilAPI.getR equiredField(el , "BODY", state);

    String body_alt = FrsUtilAPI.getR equiredField(el , "BODY_ALT",
    state);

    if(body_alt == null)
    body_alt = "";

    Multipart MP = new MimeMultipart(" alternative");
    BodyPart MBP = new MimeBodyPart();
    BodyPart MBP_ALT = new MimeBodyPart();

    if(switch_value .equals("on")){

    MBP.setContent( body, "text/html");
    MBP_ALT.setCont ent(body_alt, "text/plain");
    MP.addBodyPart( MBP);
    MP.addBodyPart( MBP_ALT);
    }
    else{
    MBP.setContent( body, "text/plain");
    MP.addBodyPart( MBP);
    }

    if(blob != null){
    BodyPart MBA = new MimeBodyPart();
    DataSource ds = new ByteArrayDataSo urce(bytes,
    "applicatio n/octet-stream");
    MBA.setDataHand ler(new DataHandler(ds) );
    MBA.setFileName (attachment_nam e);
    MP.addBodyPart( MBA);
    }

    message.setCont ent(MP);


    // Send message
    try{
    Transport.send( message);
    }

    catch (SendFailedExce ption SFE)
    {
    System.out.prin tln("My generated exception: " + SFE);
    }
    }

    return "";

    }
    }
Working...