JavaMail API dropping attachments

From: John P (john_pallozzi_at_itworld.com)
Date: 04/28/04


Date: 28 Apr 2004 10:58:27 -0700

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 "application/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.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import oracle.sql.*;

public class tagSENDMAIL_ATTACHMENT implements TagHandler {
    public String handleTag(Element 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.getInstance(props, null);
    
    if(sess == null){
        props.clear();
        props.put("mail.smtp.host", "blah2.blah.com");
        sess = Session.getInstance(props, null);
    }
        
    // Define message
    MimeMessage message = new MimeMessage(sess);
   
    // ID
    String id = FrsUtilAPI.getRequiredField(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.getDbConnection(query, DbTool.EXECUTE_QUERY);
                ResultSet result = dbTool.getResultSet();
                if (!result.next())
                        throw new Exception("Debug: cannot not fetch image from
db");
                else{
                    attachment_name = result.getString(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.println("Debug: cannot get image id = " + id);
            }
                      
        if (dbTool != null)
        {
            dbTool.close();
            dbTool = null;
        }
    }
    // Set the from address
    String from = FrsUtilAPI.getRequiredField(el, "FROM", state);
    System.out.println("FROM " + from);
    if(!from.equals("") && from != null){
        Address address = new InternetAddress(from);
        message.setFrom(address);
    
        // Set the to address
        String to = FrsUtilAPI.getRequiredField(el, "TO", state);
        System.out.println("TO " + to);
        Address address2 = new InternetAddress(to);
        message.setRecipient(Message.RecipientType.TO,address2);
    
        // Set the subject
        String subject = FrsUtilAPI.getRequiredField(el, "SUBJECT",
state);
        System.out.println("SUBJECT " + subject);
        message.setSubject(subject);

        // Get the switch value
        String switch_value = FrsUtilAPI.getRequiredField(el,
"SWITCH", state);
        System.out.println("SWITCH " + switch_value);
    
        if(switch_value == null)
            switch_value = "";
        
        // Set the content
        String body = FrsUtilAPI.getRequiredField(el, "BODY", state);
           
        String body_alt = FrsUtilAPI.getRequiredField(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.setContent(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 ByteArrayDataSource(bytes,
"application/octet-stream");
            MBA.setDataHandler(new DataHandler(ds));
            MBA.setFileName(attachment_name);
            MP.addBodyPart(MBA);
        }
    
        message.setContent(MP);
   
   
        // Send message
        try{
            Transport.send(message);
        }
    
        catch (SendFailedException SFE)
        {
            System.out.println("My generated exception: " + SFE);
        }
    }
    
    return "";
    
  }
}



Relevant Pages

  • Re: OT/ Virus SPAM email
    ... report the virus. ... I immediately advised Yahoo; I'll copy their ... attachments but I did open the message. ... become vulnerable to computer viruses. ...
    (alt.support.diabetes)
  • RE: Cant receive attachments from Yahoo
    ... My bellsouth and other email account attachments are coming through just ... I then logged onto my Yahoo account and sent the ... same e-mail with attachment from my Yahoo account to BellSouth. ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)
  • FW: 543.rar attachment
    ... I take it your not the email admin for your location? ... "Systems Admin" to see the logs of the bogus attachments. ... >> Do You Yahoo!? ... Mail has the best spam protection around ...
    (Security-Basics)
  • Re: Attachments
    ... The PGP-Basics yahoogroup has got attachments forbidden (by its ... The content is strictly to help people with PGP or GnuPG, ... stripped as their mail messages go through Yahoo mail servers. ... Please sign and encrypt for internet privacy ...
    (Ubuntu)
  • Re: Emailing Pictures Not as Attachments
    ... The Yahoo "Color and Graphics" is their term for HTML. ... > I'm trying to send an email with pictures, but I want each> picture to be preceded by some text, so I can't send it> them attachments. ...
    (microsoft.public.internet.mail)