java發帶圖片正文和附件的郵件mail
阿新 • • 發佈:2019-02-17
要求 ins code javamail com setfile .proto gic and
package com.mail; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; public class mailtest { public static MimeMessage createmail(Session se,String send,String recv1,String recv2,String recv3) throws MessagingException, UnsupportedEncodingException { MimeMessage message=new MimeMessage(se); message.setSubject("標題","utf-8"); //message.setContent("正文。。。。。","text/html;charset=utf-8");本例用圖片代替正文 Address address=new InternetAddress(send,"sengname","utf-8"); message.setFrom(address); //創建圖片文本節點 MimeBodyPart imagePart=new MimeBodyPart(); DataHandler dataHandler=new DataHandler(new FileDataSource("t1.jpg")); imagePart.setDataHandler(dataHandler); imagePart.setContentID("myimage"); MimeBodyPart textPart=new MimeBodyPart(); textPart.setContent("<image src=‘cid:myimage ‘/>","text/html;charset=utf-8"); //組裝文本、圖片節點 MimeMultipart mimeMultipart=new MimeMultipart(); mimeMultipart.addBodyPart(imagePart); mimeMultipart.addBodyPart(textPart); mimeMultipart.setSubType("related");//關聯關系 //圖片-文本--復合--轉普通節點 MimeBodyPart tex_image_tPart=new MimeBodyPart(); tex_image_tPart.setContent(mimeMultipart); //附件 MimeBodyPart aPart=new MimeBodyPart(); DataHandler dataHandler1=new DataHandler(new FileDataSource("t1.jpg")); aPart.setDataHandler(dataHandler1); //附件文件名 aPart.setFileName(MimeUtility.encodeText(dataHandler1.getName())); //組裝 MimeMultipart mimeMultipart1=new MimeMultipart(); mimeMultipart1.addBodyPart(tex_image_tPart); mimeMultipart1.addBodyPart(aPart); mimeMultipart1.setSubType("mixd");//混合關系 message.setContent(mimeMultipart1,"text/html;charset=utf-8"); message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recv1,"shoujianren1","utf-8"));//發送shojianren message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(recv2,"shoujianren2","utf-8"));//抄送 message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(recv3,"shoujianren3","utf-8"));//密送 message.setSentDate(new Date()); message.saveChanges(); return message; } public static void main(String[] args) throws MessagingException, UnsupportedEncodingException { // TODO Auto-generated method stub Properties props=new Properties(); props.setProperty("mail.transport.protocol", "smtp");// 使用的協議(JavaMail規範要求) props.setProperty("mail.smtp.host", "smtp.qq.com"); props.setProperty("mail.smtp.port","465");//設置端口 // 發件人的郵箱的 SMTP 服務器地址 props.setProperty("mail.smtp.auth", "true");// 需要請求認證 // 需要請求認證;只有qq有這一項 props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.socketFactory.port", "465"); Session se=Session.getInstance(props); se.setDebug(true);//dayin運行信息 MimeMessage message=createmail(se, "[email protected]", "[email protected]", "[email protected]", "[email protected]"); Transport transport=(Transport) se.getTransport(); transport.connect("[email protected]", "slxptfnnjocgicdi");//動態授權碼 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } }
java發帶圖片正文和附件的郵件mail