我有一个应用程序,我必须生成一个文件,它可能是一个文本文件。我想要的只是通过邮件或附件邮件发送一些邮件报告。
请帮我怎么做?
数据格式类似于表格格式。
//create a multi part
Multipart mp = new Multipart();
//data for the content of the file
String fileData = "<html>just a simple test</html>";
String messageData = "Mail Attachment Demo";
//create the file
SupportedAttachmentPart sap = new SupportedAttachmentPart(mp,"text/html","file.html",fileData.getBytes());
TextBodyPart tbp = new TextBodyPart(mp,messageData);
//add the file to the multipart
mp.addBodyPart(tbp);
mp.addBodyPart(sap);
//create a message in the sent items folder
Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);
Message message = new Message(folders[0]);
//add recipients to the message and send
try {
Address toAdd = new Address("nilanchala_p@spanservices.com","Nilanchala");
Address toAdds[] = new Address[1];
toAdds[0] = toAdd;
message.addRecipients(Message.RecipientType.TO,toAdds);
message.setContent(mp);
Transport.send(message);
} catch (Exception e) {
Dialog.inform(e.toString());
}
答案 0 :(得分:3)
最好的方法是使用带有html格式的纯文本发送表格
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
无需文件......只需格式化并发送
即可好的,试试这个:
//create a multi part
Multipart mp = new Multipart();
//data for the content of the file
String fileData = "<html>just a simple test</html>";
//create the file
TextBodyPart tbp = new TextBodyPart(mp,fileData );
//add the file to the multipart
mp.addBodyPart(tbp);
//create a message in the sent items folder
Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);
Message message = new Message(folders[0]);
//add recipients to the message and send
try {
Address toAdd = new Address("nilanchala_p@spanservices.com","Nilanchala");
Address toAdds[] = new Address[1];
toAdds[0] = toAdd;
message.addRecipients(Message.RecipientType.TO,toAdds);
message.setContent(mp);
Transport.send(message);
} catch (Exception e) {
Dialog.inform(e.toString());
}