krish mandava
krish mandava

Reputation: 83

message in the email body is not coming

I wrote the below code to send an email with attachment. I am able to get the attachment , but the message in the email body is not coming. May I know what mistake I did. Thanks in advance .

The code is as shown below

        Session session = Session.getDefaultInstance(properties);
        from = email;
        Message msg = new MimeMessage(session);
     // Instantiatee a message
        //Set message attributes
        msg.setFrom(new InternetAddress(from));

        msg.addRecipients(Message.RecipientType.BCC, 
        InternetAddress.parse("[email protected]" ) );

        //InternetAddress[] address = {new InternetAddress(to)};
        InternetAddress[] address = {new InternetAddress(groupemail)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject("EIDMS Contact Us information ");
        msg.setSentDate(new Date());

        String prefix = "<html><body>";
        StringBuilder sb= new StringBuilder(prefix);

          Connection conn = connections();
         Statement statement = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY );



        System.out.println("inside if looppp ..................");
        sb.append("[Title: "+helparea+"] <BR>"
                + "[SR Type:Incident]<BR>[Description:  "+jsutify+"<BR> Firstname:"+firstname+" Lastname:"+lastname+"<BR> Org:"+org+" Email :"+email+" Index no:"+index+"]<BR>[Region:"+region+"]<BR>"
                + "[Area:"+area+"]<BR>[Source:Web]<BR>[Requested Item:"+requesteditem+"]<BR>[Sub-Area:"+subarea+"]<BR>[Sub-Area1:"+subarea1+"]<BR>"
                + "[Severity:3-Medium] <BR>[On Behalf Of Badge:"+index+"]<BR>[On Behalf Of Index:"+index+"]<BR>"
                + "[UNIN On Behalf Email Addr: "+email+"]<BR>[UNIN Requestor Email Addr: "+email+"]<BR>[Reported By Index:"+index+"]<BR>"
                + "[Reported By Badge:"+index+"]<BR>[Alternate Contact Name: "+firstname+" "+lastname+"]<BR>"
                + "[Alternate Email Address:"+email+"]<BR>[Point of Contact:Reported By]<BR><BR>[Impact:Low]<BR><BR>[Urgency:Medium]<BR><BR>[Severity:3-Medium]<BR>"
                + "[SR Assigned To Group: "+srgroup+"]<BR></BODY></HTML>");
        msg.setContent(sb.toString(), "text/html");
        System.out.println("second email");
        System.out.println(sb.toString());


        BodyPart messageBodyPart = new MimeBodyPart();
        Multipart multipart = new MimeMultipart();


        // Now set the actual message
         messageBodyPart.setText(sb.toString());

  // Set text message part
         multipart.addBodyPart(messageBodyPart);

        //code to attach the file
        //String filename = filePath;
        String filename = "C:/Users/S.Mandava/Documents/ContactusAction.java";
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);
        msg.setContent(multipart);
        System.out.println("Attaching the file");

Upvotes: 1

Views: 69

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

As described in the JavaMail FAQ, when sending a message with attachments the main body part must be the first body part of the attachment. You're setting the content of the message twice and the second call overwrites what was done by the first call.

Upvotes: 1

Related Questions