java实现qq邮箱发送附件和图片
生活随笔
收集整理的這篇文章主要介紹了
java实现qq邮箱发送附件和图片
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
生成授權(quán)碼
1.進(jìn)入設(shè)置賬戶里面,把第一個(gè)服務(wù)開啟
2.點(diǎn)生成授權(quán)碼,在代碼中當(dāng)成是密碼
3.集成到springboot里面,引入依賴包
4.測試郵箱代碼
public static void main(String[] args) {try {Properties prop=new Properties();//設(shè)置qq郵箱服務(wù)器、"mail.host", "smtp.qq.com")prop.setProperty("mail.host","stmp.qq.com");//郵箱發(fā)送協(xié)議"mail.transport.protocol", "smtp"prop.setProperty("mail.transport.protocol","stmp");//需要驗(yàn)證用戶賬號(hào)和密碼"mail.smtp.auth", "true"prop.setProperty("mail.smpt.protocol","stmp");//設(shè)置SSL加密prop.put("mail.smtp.ssl.enable", "true");// prop.put("mail.smtp.ssl.socketFactory", sf);MailSSLSocketFactory ssl=new MailSSLSocketFactory();ssl.setTrustAllHosts(true);prop.put("mail.stmp.protocol","true");prop.put("mail.stmp.protocol",ssl);//使用javaMail發(fā)送郵件的5個(gè)步驟//1.創(chuàng)建整個(gè)應(yīng)用程序所需要的環(huán)境信息的session信息Session session=Session.getDefaultInstance(prop, new Authenticator() {public PasswordAuthentication getPasswordAuthentication() {//發(fā)郵件的賬號(hào)和授權(quán)碼return new PasswordAuthentication("","");};});//開啟session的debug模式,這樣就可以查看程序發(fā)送email的運(yùn)行狀態(tài)session.getDebug();//2.通過session得到transportTransport transport = session.getTransport();//3.使用郵箱的用戶名和授權(quán)碼鏈接郵箱服務(wù)器//第一個(gè)參數(shù):qq郵箱服務(wù)器//第二個(gè)參數(shù):發(fā)件人賬號(hào)//第三個(gè)參數(shù):授權(quán)碼transport.connect("","","");//4.創(chuàng)建郵件//創(chuàng)建郵件對(duì)象MimeMessage message =new MimeMessage(session);//指明郵件的發(fā)件人message.setFrom(new InternetAddress(""));//指明郵件的收件人message.setRecipient(Message.RecipientType.TO,new InternetAddress(""));//郵件的標(biāo)題message.setSubject("");//郵件的文本內(nèi)容message.setContent("","text/html;charset=UTF-8");//5.發(fā)送郵transport.sendMessage(message,message.getAllRecipients());transport.close();} catch (Exception e) {e.printStackTrace();System.out.println("有異常,發(fā)送失敗");}System.out.println("發(fā)送成功");}5.把上面的代碼改一下就可以發(fā)送圖片
// //創(chuàng)建郵件對(duì)象 // MimeMessage message = new MimeMessage(session); // // //指明郵件的發(fā)件人 // message.setFrom(new InternetAddress("")); // // //指明郵件的收件人,現(xiàn)在發(fā)件人和收件人是一樣的,那就是自己給自己發(fā) // message.setRecipient(Message.RecipientType.TO, new InternetAddress("")); // // //郵件的標(biāo)題 // message.setSubject("你是一個(gè)好人"); // // //郵件的文本內(nèi)容 // // 準(zhǔn)備圖片數(shù)據(jù) // MimeBodyPart image = new MimeBodyPart(); // DataHandler dh = new DataHandler(new FileDataSource("src/resources/bz.jpg")); // image.setDataHandler(dh); // image.setContentID("bz.jpg"); // // // 準(zhǔn)備正文數(shù)據(jù) // MimeBodyPart text = new MimeBodyPart(); // text.setContent("這是一封郵件正文帶圖片<img src='cid:bz.jpg'>的郵件", "text/html;charset=UTF-8"); // // // 描述數(shù)據(jù)關(guān)系 // MimeMultipart mm = new MimeMultipart(); // mm.addBodyPart(text); // mm.addBodyPart(image); // mm.setSubType("related"); // // //設(shè)置到消息中,保存修改 // message.setContent(mm); // message.saveChanges();發(fā)送附件和圖片代碼
public static void main(String[] args) throws MessagingException, GeneralSecurityException {//創(chuàng)建一個(gè)配置文件保存并讀取信息Properties properties = new Properties();//設(shè)置qq郵件服務(wù)器properties.setProperty("mail.host","smtp.qq.com");//設(shè)置發(fā)送的協(xié)議properties.setProperty("mail.transport.protocol","smtp");//設(shè)置用戶是否需要驗(yàn)證properties.setProperty("mail.smtp.auth", "true");//=================================只有QQ存在的一個(gè)特性,需要建立一個(gè)安全的鏈接// 關(guān)于QQ郵箱,還要設(shè)置SSL加密,加上以下代碼即可MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);properties.put("mail.smtp.ssl.enable", "true");properties.put("mail.smtp.ssl.socketFactory", sf);//=================================準(zhǔn)備工作完畢//1.創(chuàng)建一個(gè)session會(huì)話對(duì)象;Session session = Session.getDefaultInstance(properties, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("", "授權(quán)碼");}});//可以通過session開啟Dubug模式,查看所有的過程session.setDebug(true);//2.獲取連接對(duì)象,通過session對(duì)象獲得Transport,需要捕獲或者拋出異常;Transport tp = session.getTransport();//3.連接服務(wù)器,需要拋出異常;tp.connect("smtp.qq.com","","授權(quán)碼");//4.連接上之后我們需要發(fā)送郵件;MimeMessage mimeMessage = imageMail(session);//5.發(fā)送郵件tp.sendMessage(mimeMessage,mimeMessage.getAllRecipients());//6.關(guān)閉連接tp.close();}public static MimeMessage imageMail(Session session) throws MessagingException {//消息的固定信息MimeMessage mimeMessage = new MimeMessage(session);//郵件發(fā)送人mimeMessage.setFrom(new InternetAddress(""));//郵件接收人,可以同時(shí)發(fā)送給很多人,我們這里只發(fā)給自己;mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(""));mimeMessage.setSubject("我也不知道是個(gè)什么東西就發(fā)給你了"); //郵件主題/*編寫郵件內(nèi)容1.圖片2.附件3.文本*///圖片MimeBodyPart body1 = new MimeBodyPart();body1.setDataHandler(new DataHandler(new FileDataSource("src/resources/yhbxb.png")));body1.setContentID("yhbxb.png"); //圖片設(shè)置ID//文本MimeBodyPart body2 = new MimeBodyPart();body2.setContent("請(qǐng)注意,我不是廣告<img src='cid:yhbxb.png'>","text/html;charset=utf-8");//附件MimeBodyPart body3 = new MimeBodyPart();body3.setDataHandler(new DataHandler(new FileDataSource("src/resources/log4j.properties")));body3.setFileName("log4j.properties"); //附件設(shè)置名字MimeBodyPart body4 = new MimeBodyPart();body4.setDataHandler(new DataHandler(new FileDataSource("src/resources/1.txt")));body4.setFileName(""); //附件設(shè)置名字//拼裝郵件正文內(nèi)容MimeMultipart multipart1 = new MimeMultipart();multipart1.addBodyPart(body1);multipart1.addBodyPart(body2);multipart1.setSubType("related"); //1.文本和圖片內(nèi)嵌成功!//new MimeBodyPart().setContent(multipart1); //將拼裝好的正文內(nèi)容設(shè)置為主體MimeBodyPart contentText = new MimeBodyPart();contentText.setContent(multipart1);//拼接附件MimeMultipart allFile =new MimeMultipart();allFile.addBodyPart(body3); //附件allFile.addBodyPart(body4); //附件allFile.addBodyPart(contentText);//正文allFile.setSubType("mixed"); //正文和附件都存在郵件中,所有類型設(shè)置為mixed;//放到Message消息中mimeMessage.setContent(allFile);mimeMessage.saveChanges();//保存修改return mimeMessage;}總結(jié)
以上是生活随笔為你收集整理的java实现qq邮箱发送附件和图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 土包子也来爆料一下贵族的生活:高尔夫球场
- 下一篇: 【操作系统】设备管理