Java电子邮件内容为空

编程入门 行业动态 更新时间:2024-10-26 19:28:29
本文介绍了Java电子邮件内容为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一段代码,用于发送带有excel文件附件的电子邮件。在我可以看到标题甚至文件附件的地方,一切正常。唯一没有出现的是电子邮件内容。我已经测试过我的emailContent变量不为空。我还能做些什么使它出现?我什至启用了这行代码messageBodyPart.setText(emailContent);还是一样。 但如果启用,则此部分multipart1.addBodyPart(emailContent);我收到错误

I have snippet of codes where I send out email with excel file attachment. All works fine where I can see title and even the file attachment. The only thing does not appear is the email content. I have tested that my emailContent variable is not empty. What else can I do to make it appear ? I have even enabled this line of codes messageBodyPart.setText(emailContent); yet the same. But if enabled this part multipart1.addBodyPart(emailContent); I get error

error: no suitable method found for addBodyPart(String) multipart1.addBodyPart(emailContent); try { Message emailMessage = new MimeMessage(mailSession); emailMessage.setFrom(new InternetAddress(origin1)); emailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(receiptnt1)); emailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(receiptnt2)); emailMessage.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc1)); emailMessage.setSubject(emailTitle); emailMessage.setText(emailContent); BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message //messageBodyPart.setText(emailContent);*/ Multipart multipart1 = new MimeMultipart(); // Part two is attachment messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart1.addBodyPart(messageBodyPart); // Put parts in message emailMessage.setContent(multipart1); //System.out.println("\n\nSend email :"+eMArray[0]); transport.sendMessage(emailMessage, emailMessage.getAllRecipients()); } catch (Exception e) { System.out.println("Transport Problem"); e.printStackTrace(); }

推荐答案

您已初始化

BodyPart messageBodyPart = new MimeBodyPart();

两次。在第二次初始化之前,您要添加主体内容。 因此删除该行

Two times. And before the second initialization you're adding the body contents. So remove the line

messageBodyPart = new MimeBodyPary();

行,它将正常工作。

使用以下代码。

Message emailMessage = new MimeMessage(mailSession); emailMessage.setFrom(new InternetAddress(origin1)); emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiptnt1)); emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiptnt2)); emailMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc1)); emailMessage.setSubject(emailTitle); // emailMessage.setText(emailContent); Multipart multipart1 = new MimeMultipart(); BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText(emailContent); // Part two is attachment BodyPart attachment = new MimeBodyPart(); DataSource source = new FileDataSource(filename); attachment.setDataHandler(new DataHandler(source)); attachment.setFileName(filename); multipart1.addBodyPart(attachment); multipart1.addBodyPart(messageBodyPart); // Put parts in message emailMessage.setContent(multipart1); //System.out.println("\n\nSend email :"+eMArray[0]); transport.sendMessage(emailMessage, emailMessage.getAllRecipients());

更多推荐

Java电子邮件内容为空

本文发布于:2023-05-27 23:09:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/306076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   电子邮件   内容   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!