2

I have created the below program as it is use to send the mail through java mail api and it is working perfectly now i have to modify it as i also want to attach a pdf file which is stored in my c: , please advise how can i modify this program below so that when it send the mail it should also send the abc.pdf file stored in my c: drive as an attachment also

c:\abc.pdf

below is the program which i have developed rite now it did not send any attachment

  public class BrokMailTest {

        public static void main(String[] args) {

            String mailSmtpHost = "16.66.66.66";
            String mailSmtpPort = "2345" ;

             String mailTo = "avdg@abc.com";
            //String mailCc = "avdg@abc.com ";
            String mailFrom = "avdg@abc.com";
            String mailSubject = "fghfdwwrtuijjjjuj";
            String mailText = "wertyuuiiojgfdss";
            sendEmail(mailTo,  mailFrom, mailSubject, mailText, mailSmtpHost ,mailSmtpPort );
        }

        public static void sendEmail(String to,  String from, String subject, String text, String smtpHost , String mailSmtpPort) {
            try {
                Properties properties = new Properties();
                properties.put("mail.smtp.host", smtpHost);
                properties.put("mailSmtpPort", mailSmtpPort);
                //obtaining the session
                Session emailSession = Session.getDefaultInstance(properties);
                Message emailMessage = new MimeMessage(emailSession);
                emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                //emailMessage.addRecipients(Message.RecipientType.CC, InternetAddress.parse("avdg@abc.com,aswq@gmail.com"));
                Address[] cc = new Address[] {
                 new InternetAddress("avdg@abc.com"),
                 new InternetAddress("asd@gmail.com")};
                 emailMessage.addRecipients(Message.RecipientType.CC, cc);
                emailMessage.setFrom(new InternetAddress(from));
                emailMessage.setSubject(subject);
                emailMessage.setContent(emailMessage, "text/html");
                emailMessage.setText(text);
                emailSession.setDebug(true);
                Transport.send(emailMessage);
            }    catch (AddressException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            }

        }

    }
ndsfd ddsfd
  • 235
  • 1
  • 5
  • 13
  • Take a look at : http://www.tutorialspoint.com/javamail_api/javamail_api_send_email_with_attachment.htm – Sercan Ozdemir May 17 '15 at 05:23
  • @SercanOzdemir could not grasp that much , please modify my above program as per that functionality that will help me to grasp, Thanks inadvance – ndsfd ddsfd May 17 '15 at 05:47
  • http://stackoverflow.com/questions/16117365/sending-mail-attachment-using-java – BSMP May 17 '15 at 05:50

1 Answers1

0

You need to create a multipart. The multipart will have a message part and the attachment part. Example with JavaMail 1.4:

    MimeBodyPart messageBodyPart = new MimeBodyPart();   //message part
    messageBodyPart.setContent(text, "text/html");

    Multipart multipart = new MimeMultipart();    //multipart
    multipart.addBodyPart(messageBodyPart);

    MimeBodyPart attachedPart = new MimeBodyPart();    //attachment part
    attachedPart.attachFile("c:\abc.pdf");
    multipart.addBodyPart(attachedPart);     

    emailMessage.setContent(multipart);
    emailSession.setDebug(true);
    Transport.send(emailMessage);
JosEduSol
  • 5,268
  • 3
  • 23
  • 31