JavaMail Exchange服务器MimeContent

时间:2017-07-06 03:19:48

标签: java javamail exchangewebservices

我正在使用JavaMail api,我必须通过Exchange服务器发送电子邮件,然后将图像嵌入到电子邮件正文中。为此,我使用ExchangeService与Exchange服务器连接。注意:如果是SMTP,我会通过Session和Authenticator。

我面临的挑战是我必须使用EmailMessage最终发送邮件。我没有选择在EmailMessage中设置嵌入消息(MIMEContent)并发送它。

try{

                    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
                    ExchangeCredentials credentials = new WebCredentials(userName, password, domain);
                    service.setCredentials(credentials);
                    service.setUrl(new URI(host));
                    EmailMessage msg = new EmailMessage(service);
                    msg.setSubject(subject); //email subject
                    msg.getFrom().setAddress(from);
                    msg.getToRecipients().add(to); //email receiver
                    MimeBodyPart messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setContent(bodyStart, "text/html");

                    // creates multi-part
                    MimeMultipart multipart = new MimeMultipart();
                    multipart.addBodyPart(messageBodyPart);
                    MimeBodyPart imagePart = new MimeBodyPart();
                    imagePart.setHeader("Content-ID", "<123234325>");
                    imagePart.setDisposition(MimeBodyPart.INLINE);
                    imagePart.attachFile("C:/selenium/ma000xsspf01.png");
                    multipart.addBodyPart(imagePart);
                    msg.send(); //send email
}catch(Exception e){
}

有人请知道如何使用对象&#34; msg&#34;将图像设置为嵌入图像。

2 个答案:

答案 0 :(得分:0)

使用module MyHelper def hello_world(name) "hello #{name}" end end 。我认为这是您正在寻找的mail with attachment

答案 1 :(得分:0)

尝试以下几行:

MimeMessage msg = new MimeMessage(objSession);
MimeBodyPart imagePart = new MimeBodyPart();
String origFileName = null;
origFileName = /* Get File Name here*/;

// set the byte content
DataSource fds = new ByteArrayDataSource(/*Get Image Stream or content array*/, "image/jpeg");
imagePart.setDataHandler(new DataHandler(fds));

// this header connects the html content to the image mime part
imagePart.addHeader("Content-ID", "<" + newContentId + ">");

// set the name of resource - after encoding it with Mime utility
imagePart.setFileName(MimeUtility.encodeText(origFileName, "utf-8", null).replaceAll("\r\n", ""));

// set disposition as inline
imagePart.setDisposition(MimeBodyPart.INLINE);

MimeMultipart relatedMail = new MimeMultipart("related");
relatedMail.addBodyPart(imagePart);
msg.setContent(relatedMail);