javamail不发送消息内容

时间:2014-09-02 14:50:16

标签: java javamail

我正在尝试使用javamail从webapp发送电子邮件,并且在没有正文和附件的情况下发送电子邮件。我试过调试它,我看不出有什么问题。 我正在使用tomee + 1.7和javamail 1.4.7。

这是我的代码:

    // Msg body
        session.setDebug(true);
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText("Attached are your query results");
        messageBodyPart.addHeader("Content-Type", "text/html; charset=windows-1255");
        messageBodyPart.addHeader("Content-Transfer-Encoding", "base64");

        // attachment
        LOG.info("Building attachment from file " + file.getName());
        MimeBodyPart attachmentPart = new MimeBodyPart();
        attachmentPart.attachFile(file);
        attachmentPart.setFileName(file.getName());

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

        // Send message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("Query report...");          
        message.setContent(multipart);
        Transport.send(message);
        LOG.info("Sent message successfully ...");

我收到的消息:

From: x@x.x
To: x@x.x
Message-ID: <371010179.1.1409738093141.JavaMail.root@claimqa01>
Subject: Query report...
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="----=_Part_0_1099874708.1409738093124"

4 个答案:

答案 0 :(得分:3)

很抱歉提出旧话题,但我希望它会对某人有所帮助。

问题是你的classpath中有超过1个java.mail jar。 例如,我在TomEE lib文件夹中有一个名为geronimo javamail 1.4 jar的东西,它有java邮件的类。你可能认为你使用的是最新版本的java.mail,但这是错误的

答案 1 :(得分:1)

在AttachmentPart之前将messageBodyPart添加到multipart。

如果仍然无效,请发布调试输出。

您可以通过添加message.writeTo(new FileOutputStream(“msg.txt”))来检查将要发送的消息;就在Transport.send之前。

此外,您可能希望升级到latest version of JavaMail - 1.5.2

答案 2 :(得分:0)

在没有收到错误消息的情况下,很难确定根本原因。我有以下代码适用于我

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendFileEmail
{
public static void main(String [] args)
{

  // Recipient's email ID needs to be mentioned.
  String to = "abcd@gmail.com";

  // Sender's email ID needs to be mentioned
  String from = "web@gmail.com";

  // Assuming you are sending email from localhost
  String host = "localhost";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  try{
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
     message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
     message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));

     // Set Subject: header field
     message.setSubject("This is the Subject Line!");

     // Create the message part 
     BodyPart messageBodyPart = new MimeBodyPart();

     // Fill the message
     messageBodyPart.setText("This is message body");

     // Create a multipar message
     Multipart multipart = new MimeMultipart();

     // Set text message part
     multipart.addBodyPart(messageBodyPart);

     // Part two is attachment
     messageBodyPart = new MimeBodyPart();
     String filename = "file.txt";
     DataSource source = new FileDataSource(filename);
     messageBodyPart.setDataHandler(new DataHandler(source));
     messageBodyPart.setFileName(filename);
     multipart.addBodyPart(messageBodyPart);

     // Send the complete message parts
     message.setContent(multipart );

     // Send message
     Transport.send(message);
     System.out.println("Sent message successfully....");
  }catch (MessagingException mex) {
     mex.printStackTrace();
  }
 }
}

答案 3 :(得分:0)

最后,我删除了邮件的文本部分,并将文件内容加载为String:

        Message message = new MimeMessage(session);
        message.setDataHandler(new DataHandler(new ByteArrayDataSource(csv.getBytes("8859_8"),
                "text/csv")));
        message.setFileName("data.csv");
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("Query report...");
        Transport.send(message);
        LOG.info("Sent message successfully ...");