电子邮件正文中的邮件未来

时间:2016-09-15 17:50:52

标签: java javamail

我写了下面的代码来发送带附件的电子邮件。我能够获得附件,但电子邮件正文中的消息不会出现。我可以知道我做了什么错。提前致谢 。

代码如下所示

        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("example@gmail.com" ) );

        //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");

1 个答案:

答案 0 :(得分:1)

JavaMail FAQ中所述,当发送带附件的邮件时,主体部分必须是附件的第一个正文部分。您将消息的内容设置为两次,第二次调用将覆盖第一次呼叫所执行的操作。