我使用下面的代码发送xls文件并以表格格式显示一些数据。我能够将xls文件作为附件,但html代码不是表格格式。如果我取消注释 msg.setContent(正文," text / html")行,那么我只能在正文中获取表格格式但附件不存在
public synchronized static void sendEmail(Session session, String toEmail, String subject, String body, String recipient) {
try {
MimeMessage msg = new MimeMessage(session);
// msg.addHeader("Content-type", "text/html");
msg.setFrom(new InternetAddress("abc@xyz", "abc@xyz"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
msg.setReplyTo(InternetAddress.parse(recipient, false));
msg.setSubject(subject);
msg.setSentDate(new Date());
//==================================================================
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource("D:/Shashank/output.xls");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// msg.setContent(body, "text/html");
msg.saveChanges();
//==================================================================
System.out.println("Message is ready");
Transport.send(msg);
System.out.println("EMail Sent Successfully!!");
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
如果您希望发送正文部分(文字)和附件,则必须创建mime多部分留言。
或使用apache commons email来处理此问题。
import org.apache.commons.mail.*;
...
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("mypictures/john.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("John");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// add the attachment
email.attach(attachment);
// send the email
email.send();
答案 1 :(得分:0)
本规范对我来说100%罚款......
File file;//Attached file
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(EMAIL_FROM));
final String[] to = emailsTo;
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("", "text/html");
message.setSubject("");
/////////////Attach File/////////////
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("", "text/html");
// Create a multipart message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart, "text/html");
/////////////////////////////////////
Transport.send(message);
答案 2 :(得分:-1)
use this :
#!/usr/bin/ksh
export MAILTO="spam@ebay.com"
export SUBJECT="Mail Subject"
export BODY="/tmp/email_body.html"
export ATTACH="/tmp/attachment.xls"
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode -m $ATTACH $(basename $ATTACH)
echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail $MAILTO