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.
All fields(Subject,Body,Recipients) -> Formatter(java and/or objective-c) -> String according RC822
The problem is they are session oriented and I don't have credentials or host.
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);
}
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();
}
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--
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.