Java电子邮件内容为空 [英] Java email content is empty

查看:239
本文介绍了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电子邮件内容为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆