1

I have the following email:

Subject = Hello!
Body = Bla bla bla bla.
Recipients = "carlos@mail.com", "mike@mail.com"

Now I want to parse that fields following RFC822, but I can't find it.

What I need?

All fields(Subject,Body,Recipients) -> Formatter(java and/or objective-c) -> String according RC822

What I tried?

The problem is they are session oriented and I don't have credentials or host.

Update

I need something like this but instead using message.writeTo(...) I want something like String dataRFC822 = message.getRFC822String();

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

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

      // Get the Session object. Which I have not and I don't want it.
      Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
            }
         });

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

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

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

         // Set Subject: header field
         message.setSubject("Testing Subject");

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

         // Now set the actual 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 = "/home/manisha/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);

         //instead write it on a stream i I want get back the string formated according to rfc822
         message.writeTo(...);


      } catch (MessagingException e) {
         throw new RuntimeException(e);
      }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ricardo
  • 7,921
  • 14
  • 64
  • 111

3 Answers3

2

As @Shadowfacts pointed out, you can use a ByteArrayOutputStream.

The trick is you don't need credentials on the session object, as long as you don't really use it to connect to a server. Then I just re-used most of your code:

String to = "destinationemail@gmail.com";
String from = "fromemail@gmail.com";

// Empty properties and null credentials makes a valid session.
Properties props = new Properties();
Session session = Session.getInstance(props, null);

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("Testing Subject");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("This is message body");

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

messageBodyPart = new MimeBodyPart();
String filename = "/etc/hostname";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
String rfc822message = baos.toString();
System.out.print(rfc822message);

The result I got:

From: fromemail@gmail.com
To: destinationemail@gmail.com
Message-ID: <392292416.1.1481815905219@nitoshpas>
Subject: Testing Subject
MIME-Version: 1.0
Content-Type: multipart/mixed; 
boundary="----=_Part_0_59559151.1481815905197"

------=_Part_0_59559151.1481815905197
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is message body
------=_Part_0_59559151.1481815905197
Content-Type: application/octet-stream; name="/etc/hostname"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="/etc/hostname"

nitoshpas

------=_Part_0_59559151.1481815905197--
Nicolas Defranoux
  • 2,646
  • 1
  • 10
  • 13
1

You can manually make a RFC822 compatible string manually, if you don't attach a file or absolutely need a multipart mime message, it's way more simple:

String makeRfc822email(String from, String to, String subject, String message) {
  StringBuilder builder = new StringBuilder();
  builder.append("From: ");
  builder.append(from);
  builder.append("\n");

  builder.append("To: ");
  builder.append(to);
  builder.append("\n");

  builder.append("Subject: ");
  builder.append(subject);
  builder.append("\n\n");  // Blank line before message.

  builder.append(message);

  return builder.toString();
}
Nicolas Defranoux
  • 2,646
  • 1
  • 10
  • 13
  • I know my questions does not mention attachments, but what about I want send attachments using this builder. – Ricardo Dec 12 '16 at 19:50
  • If you require attachments the code you had is pretty good. Creating manually the payload would be more complicated, the builder is just appending strings while attachments are not defined at RFC822 level but are included withing the message using Mime encoding (this page http://www.livinginternet.com/e/ea_att_mime.htm give a list of relevant RFCs). – Nicolas Defranoux Dec 12 '16 at 21:17
  • I read rfc but is not clear how is printed as string. I found this but is not working neither. https://msdn.microsoft.com/en-us/library/ms526560(v=exchg.10).aspx – Ricardo Dec 15 '16 at 10:42
0

You can use ByteArrayOutputStream which is an implementation of OutputStream that writes to a byte array. You can create an instance of this and pass it to writeTo(OutputStream) on your method and from there call toString on the ByteArrayOutputStream to retrieve a String representation of the message.

ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeto(out);
String s = out.toString();
Shadowfacts
  • 1,038
  • 10
  • 22
  • As I said, I don't have credentials to create a session object. And I can't find the way to create MimeMessage object without parameters. The only data that I have is Subject, Body, from, to(Recipients) – Ricardo Dec 12 '16 at 01:16