gpt4 book ai didi

java - RFC822 电子邮件 - Formatter - Android/iOS - 创建没有 session 或主机的 MimeMessage

转载 作者:行者123 更新时间:2023-11-29 19:27:30 26 4
gpt4 key购买 nike

我有以下电子邮件:

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

现在我想解析遵循 RFC822 的那些字段,但找不到它。

我需要什么?

所有字段(主题、正文、收件人)-> 格式化程序(java 和/或 objective-c)-> 根据 RC822 的字符串

我尝试了什么?

问题是它们是面向 session 的,我没有凭据或主机。

更新

我需要像 this 这样的东西但不是使用 message.writeTo(...) 我想要类似 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);
}

最佳答案

正如@Shadowfacts 所指出的,您可以使用 ByteArrayOutputStream。

诀窍是您不需要 session 对象的凭据,只要您不真正使用它来连接服务器即可。然后我只是重新使用了你的大部分代码:

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--

关于java - RFC822 电子邮件 - Formatter - Android/iOS - 创建没有 session 或主机的 MimeMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40975736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com