javaandroidobjective-cemailrfc822

RFC822 email - Formatter - Android/iOS - create MimeMessage without session or host


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);
      }

Solution

  • 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();
    }