This discussion is archived
8 Replies Latest reply: Jan 22, 2013 12:37 AM by PhHein RSS

How to avoid attachment with mail code

Puneet Lobana Newbie
Currently Being Moderated
Hi ,

I have written a code for sending e-mail notification .Code is working fine as e-mail is triggered as expected but when mail is recieved an attachment comes along with it named attzurlm.dat. Plz provide pointers how to remove this attachment .

PFB the code
public String sendEmail( List<String> recipientList, String subject, String message , String from,String cc,String logoPath,Logger LOGGER) {

try{
functionName="sendEmail()";
//Set the host smtp address
Properties props = new Properties();
Utils util=new Utils();
//Fetching values from lookup
tcLookupOperationsIntf lookupIntf = Platform.getService(tcLookupOperationsIntf.class);
String smtpHost=util.getLookUpValueByKey("LookUp.EmailInfo","CG_EMAIL_SERVER",lookupIntf,LOGGER);
LOGGER.debug(functionName+ " SMTP Host Name :"+smtpHost);
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "false");

// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
//session.setDebug(debug);

// create a message
Message msg = new MimeMessage(session);

//Check if cc is required
if (cc.length() != 0) {
InternetAddress ccAddress = new InternetAddress(cc);
msg.setRecipient(RecipientType.CC, ccAddress);
}

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);

msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipientList.size()];

for (int iCountMail = 0; iCountMail < recipientList.size(); iCountMail++)
{
addressTo[iCountMail] = new InternetAddress(recipientList.get(iCountMail));
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setText(message);
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<br>"+ message + "<BR><BR><P align=\"left\">" + "<br><img src=\"cid:image\"><br><br>" + "</P>";
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource fds=null;
try{
fds= new FileDataSource(logoPath);
}catch(Exception exe){
LOGGER.debug("Function:+ sendEmial :logofile path is missing"+logoPath);
}
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
multipart.addBodyPart(messageBodyPart);

// put everything together
msg.setContent(multipart);

Transport.send(msg);
LOGGER.info("Email sent to recipient :"+recipientList);
}catch(MessagingException msgExc){
LOGGER.error(functionName+" Error occurred while sending email:"+msgExc);
msgExc.printStackTrace();
return "ERROR";

}catch(Exception exception){
LOGGER.error(functionName+" Error occurred while sending email:"+exception);
exception.printStackTrace();
return "ERROR";
}
return "SUCCESS";
}
  • 1. Re: How to avoid attachment with mail code
    PhHein Guru Moderator
    Currently Being Moderated
    Please repost and add code tags. That is unreadable.
  • 2. Re: How to avoid attachment with mail code
    Puneet Lobana Newbie
    Currently Being Moderated
    Hi PhHein ,
    I am not able to understand what exactly you are looking for in the post .Can you plz elaborate.Sorry for inconvenience .
  • 3. Re: How to avoid attachment with mail code
    PhHein Guru Moderator
    Currently Being Moderated
    Read this announcement:
    https://forums.oracle.com/forums/ann.jspa?annID=1429
  • 4. Re: How to avoid attachment with mail code
    Puneet Lobana Newbie
    Currently Being Moderated
    Code Goes as below in curley braces
    :
    {
    public String sendEmail( List<String> recipientList, String subject, String message , String from,String cc,String logoPath,Logger LOGGER)     {
             
            try{
                 functionName="sendEmail()";
                 //Set the host smtp address
                 Properties props = new Properties();
                 Utils util=new Utils();
                 tcLookupOperationsIntf lookupIntf = Platform.getService(tcLookupOperationsIntf.class);
                 String smtpHost=util.getLookUpValueByKey("CG.LookUp.EmailInfo","EMAIL_SERVER",lookupIntf,LOGGER);
                 LOGGER.debug(functionName+ " SMTP Host Name :"+smtpHost);
                 props.put("mail.smtp.host", smtpHost);
                 props.put("mail.smtp.auth", "false");
                 
                // create some properties and get the default Session
                Session session = Session.getDefaultInstance(props, null);
                //session.setDebug(debug);
        
                // create a message
                Message msg = new MimeMessage(session);
                
                //Check if cc is required
                if (cc.length() != 0) {
                    InternetAddress ccAddress = new InternetAddress(cc);
                    msg.setRecipient(RecipientType.CC, ccAddress);
                }
                
                // set the from and to address
                InternetAddress addressFrom = new InternetAddress(from);
                
                msg.setFrom(addressFrom);
                
                InternetAddress[] addressTo = new InternetAddress[recipientList.size()]; 
                
                for (int iCountMail = 0; iCountMail < recipientList.size(); iCountMail++)
                {
                    addressTo[iCountMail] = new InternetAddress(recipientList.get(iCountMail));
                }
                msg.setRecipients(Message.RecipientType.TO, addressTo);
              
                // Setting the Subject and Content Type
                msg.setSubject(subject);
                msg.setText(message);
                MimeMultipart multipart = new MimeMultipart("related");
             // first part (the html)
                BodyPart messageBodyPart = new MimeBodyPart();
                String htmlText = "<br>"+ message + "<BR><BR><P align=\"left\">" + "<br><img src=\"cid:image\"><br><br>" + "</P>";
                messageBodyPart.setContent(htmlText, "text/html");
                multipart.addBodyPart(messageBodyPart);
                messageBodyPart = new MimeBodyPart();
                DataSource fds=null;
                try{
                    fds= new FileDataSource(logoPath);
                }catch(Exception exe){
                    LOGGER.debug("Function:+ sendEmial :logofile path is missing"+logoPath);
                }
                messageBodyPart.setDataHandler(new DataHandler(fds));
                messageBodyPart.setHeader("Content-ID", "<image>");
                multipart.addBodyPart(messageBodyPart);
        
                // put everything together
                msg.setContent(multipart);
        
                Transport.send(msg);
                LOGGER.info("Email sent to recipient :"+recipientList);
            }catch(MessagingException msgExc){
                 LOGGER.error(functionName+" Error occurred while sending email:"+msgExc);
                 msgExc.printStackTrace();
                 return "ERROR";
                
            }catch(Exception exception){
                LOGGER.error(functionName+" Error occurred while sending email:"+exception);
                 exception.printStackTrace();
                 return "ERROR";
            }
            return "SUCCESS";
        }
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 5. Re: How to avoid attachment with mail code
    gimbal2 Guru
    Currently Being Moderated
    Fail. Let me fix that for you.
    public String sendEmail( List<String> recipientList, String subject, String message , String from,String cc,String logoPath,Logger LOGGER)     {
    
    try{
    functionName="sendEmail()";
    //Set the host smtp address
    Properties props = new Properties();
    Utils util=new Utils();
    tcLookupOperationsIntf lookupIntf = Platform.getService(tcLookupOperationsIntf.class);
    String smtpHost=util.getLookUpValueByKey("CG.LookUp.EmailInfo","EMAIL_SERVER",lookupIntf,LOGGER);
    LOGGER.debug(functionName+ " SMTP Host Name :"+smtpHost);
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.auth", "false");
    
    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    //session.setDebug(debug);
    
    // create a message
    Message msg = new MimeMessage(session);
    
    //Check if cc is required
    if (cc.length() != 0) {
    InternetAddress ccAddress = new InternetAddress(cc);
    msg.setRecipient(RecipientType.CC, ccAddress);
    }
    
    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    
    msg.setFrom(addressFrom);
    
    InternetAddress[] addressTo = new InternetAddress[recipientList.size()]; 
    
    for (int iCountMail = 0; iCountMail < recipientList.size(); iCountMail++)
    {
    addressTo[iCountMail] = new InternetAddress(recipientList.get(iCountMail));
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);
    
    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setText(message);
    MimeMultipart multipart = new MimeMultipart("related");
    // first part (the html)
    BodyPart messageBodyPart = new MimeBodyPart();
    String htmlText = "<br>"+ message + "<BR><BR><P align=\"left\">" + "<br><img src=\"cid:image\"><br><br>" + "</P>";
    messageBodyPart.setContent(htmlText, "text/html");
    multipart.addBodyPart(messageBodyPart);
    messageBodyPart = new MimeBodyPart();
    DataSource fds=null;
    try{
    fds= new FileDataSource(logoPath);
    }catch(Exception exe){
    LOGGER.debug("Function:+ sendEmial :logofile path is missing"+logoPath);
    }
    messageBodyPart.setDataHandler(new DataHandler(fds));
    messageBodyPart.setHeader("Content-ID", "<image>");
    multipart.addBodyPart(messageBodyPart);
    
    // put everything together
    msg.setContent(multipart);
    
    Transport.send(msg);
    LOGGER.info("Email sent to recipient :"+recipientList);
    }catch(MessagingException msgExc){
    LOGGER.error(functionName+" Error occurred while sending email:"+msgExc);
    msgExc.printStackTrace();
    return "ERROR";
    
    }catch(Exception exception){
    LOGGER.error(functionName+" Error occurred while sending email:"+exception);
    exception.printStackTrace();
    return "ERROR";
    }
    return "SUCCESS";
    }
    }
  • 6. Re: How to avoid attachment with mail code
    PhHein Guru Moderator
    Currently Being Moderated
    Check whether the logoPath leads to attzurlm.dat, if so, don't attach the file.
  • 7. Re: How to avoid attachment with mail code
    Puneet Lobana Newbie
    Currently Being Moderated
    Yes PhHein , When i changed the logo from png image to gif image the issue got resolved .
  • 8. Re: How to avoid attachment with mail code
    PhHein Guru Moderator
    Currently Being Moderated
    Puneet Lobana wrote:
    Yes PhHein , When i changed the logo from png image to gif image the issue got resolved .
    Uhm, ok, good.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points