RFC822 电子邮件 - 格式化程序 - Android/iOS - 创建没有会话或主机的 MimeMessage
RFC822 email - Formatter - Android/iOS - create MimeMessage without session or host
我有以下电子邮件:
Subject = Hello!
Body = Bla bla bla bla.
Recipients = "carlos@mail.com", "mike@mail.com"
现在我想解析RFC822之后的那个字段,但是我找不到它。
我需要什么?
所有字段(主题、正文、收件人)-> 格式化程序(java and/or objective-c)-> 符合 RC822 的字符串
我尝试了什么?
问题是它们是面向会话的,我没有凭据或主机。
更新
我需要 this 之类的东西,但我想要 String dataRFC822 = message.getRFC822String();
之类的东西,而不是使用 message.writeTo(...)
// 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);
}
您可以使用 ByteArrayOutputStream
,它是 OutputStream
的一个实现,它写入 byte
数组。您可以创建一个实例并将其传递给您方法上的 writeTo(OutputStream)
,然后从那里调用 ByteArrayOutputStream
上的 toString
以检索消息的 String
表示。
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeto(out);
String s = out.toString();
您可以手动制作一个 RFC822 兼容字符串,如果您不附加文件或绝对需要多部分 mime 消息,则更简单:
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();
}
正如@Shadowfacts 所指出的,您可以使用 ByteArrayOutputStream。
诀窍是您不需要会话对象的凭据,只要您不真正使用它连接到服务器即可。然后我就重新使用了你的大部分代码:
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);
我得到的结果:
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--
我有以下电子邮件:
Subject = Hello!
Body = Bla bla bla bla.
Recipients = "carlos@mail.com", "mike@mail.com"
现在我想解析RFC822之后的那个字段,但是我找不到它。
我需要什么?
所有字段(主题、正文、收件人)-> 格式化程序(java and/or objective-c)-> 符合 RC822 的字符串
我尝试了什么?
问题是它们是面向会话的,我没有凭据或主机。
更新
我需要 this 之类的东西,但我想要 String dataRFC822 = message.getRFC822String();
message.writeTo(...)
// 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);
}
您可以使用 ByteArrayOutputStream
,它是 OutputStream
的一个实现,它写入 byte
数组。您可以创建一个实例并将其传递给您方法上的 writeTo(OutputStream)
,然后从那里调用 ByteArrayOutputStream
上的 toString
以检索消息的 String
表示。
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeto(out);
String s = out.toString();
您可以手动制作一个 RFC822 兼容字符串,如果您不附加文件或绝对需要多部分 mime 消息,则更简单:
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();
}
正如@Shadowfacts 所指出的,您可以使用 ByteArrayOutputStream。
诀窍是您不需要会话对象的凭据,只要您不真正使用它连接到服务器即可。然后我就重新使用了你的大部分代码:
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);
我得到的结果:
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--