是每個圖像都需要自己MimeBodyPart的。以上解決方案僅適用於兩個圖像。萬一,如果有多個圖像,爲MimeBodyPart創建多個對象效率不高。
所以,讓我們去了功能,
public void imgUpload(Multipart multipart,String fileName) throws MessagingException
{
int no = rand.nextInt();
String contentId = Integer.toString(no);
System.out.println(contentId);
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<img align=\" center \" src=\"cid:"+contentId+"\"><br>";
messageBodyPart.setContent(htmlText+"<br>", "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
System.out.println(contentId);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<"+contentId+">");
System.out.println(contentId);
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
}
這裏要傳遞的參數是MimeBodyPart的的對象和圖像的路徑。 每個圖片需要自己的content-id,因此content-id是針對此場景隨機生成的。
全部代碼中嵌入多張圖片:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendImageEmail
{
public static void main(String [] args)
{
SendImageEmail sendEmail = new SendImageEmail();
sendEmail.SendemailwithImage();
}
public void imgUpload(Multipart multipart,String fileName) throws MessagingException
{
int no = rand.nextInt();
String contentId = Integer.toString(no);
System.out.println(contentId);
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<img align=\" center \" src=\"cid:"+contentId+"\"><br>";
messageBodyPart.setContent(htmlText+"<br>", "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
System.out.println(contentId);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<"+contentId+">");
System.out.println(contentId);
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
}
public void SendemailwithImage(){
SendImageEmail imgEmail = new SendImageEmail();
List<String> imgPath = new ArrayList<String>();
imgPath.add("D:\\img1.png");
imgPath.add("D:\\img2.png");
imgPath.add("D:\\img3.png");
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("This is message body");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
for(String fileName : imgPath)
{
imgEmail.upload(multipart,fileName);
}
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}}}
非常感謝。那是我正在做的錯誤。現在正在工作...... – user57421 2011-05-01 15:34:55