简体   繁体   中英

JavaMail API sending attachment as forwarded Mails to Sender

I'm trying to send two attachments with the help of javamail API but the sender is receiving the mails the attachments are delivering as forwarded messages to the sender. Also i'm not able to see the body text in the mail.I'm sending to an outlook email client. its like attachments are coming as separate mail in the same mail and when i click on the separate mail in the mail then it shows me the attachments.Like i have opened another mail in the same mail. Can anyone please check what i'm doing wrong

What im getting right now 在此处输入图片说明 when i click on the internal mail

在此处输入图片说明

Below is the code

    deltaDf.coalesce(1)
    .write.mode("overwrite")
    .format("com.databricks.spark.csv")
    .option("header", "true")
    .save("/user/test/test1/Deltafile/")

  val deltahdfsname = fs.globStatus(new Path("/user/test/test1/Deltafile/part*"))(0).getPath().getName()
  val deltapath = new Path(fs.getName() + "/user/test/test1/Deltafile/" + deltahdfsname)
  val deltastream = fs.open(deltapath)
  val deltafileName = "delta.csv"

  val bodyText = "Hi All, <br> </br> Attached are the Concur Delta File and Exception Report  <br> </br>  please take the neccassory Action."
  val from = "tests@test.com"
  val smtpHost = "xmail.xxxxx.com"

  val properties = System.getProperties
  properties.put("mail.smtp.host",smtpHost)
  properties.put("mail.from", from)
  properties.put("mail.smtp.auth", "false")
  properties.put("mail.smtp.port", "25")
  properties.put("mail.smtp.starttls.enable", "true")

   val deltaBodyPart = new MimeBodyPart()
    deltaBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(deltastream, "text/csv")))
    deltaBodyPart.setFileName(deltafileName)



    val multipart = new MimeMultipart()
    multipart.addBodyPart(deltaBodyPart)
    //multipart.addBodyPart(ExceptionBodyPart)

    val session = Session.getInstance(properties)
    var message = new MimeMessage(session)

    message.setFrom(new InternetAddress("tests@test.com"))
    message.setRecipients(Message.RecipientType.TO, "suraj@xxxx.com")
    message.setSubject("test message")
    message.setText("bodyText","text/html;charset=utf-8")
    message.setContent(multipart,"text/html;charset=utf-8")
    Transport.send(message)

I just want that the sender should receive the attachments in a single window.

So many things wrong...

Calling setContent after calling setText just overwrites what was done with setText. The setText method is just a convenience method that internally calls setContent.

Calling setContent with a Multipart object and telling it to use the MIME type "text/html" means it won't be a multipart object after all.

You need to create the MimeMultipart object and add two MimeBodyPart objects to it, the first containing the main body text and the second containing the attachment. Then set the MimeMultipart object as the content of the message.

See the JavaMail FAQ and the JavaMail sample programs for examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM