java springboot+maven发送邮件
生活随笔
收集整理的這篇文章主要介紹了
java springboot+maven发送邮件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
springboot+maven發(fā)送郵件
廢話不多說直接上代碼
1. pom 文件導(dǎo)入jar包
<!--郵件發(fā)送--><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version></dependency>?
2. 郵件方法 我用的是163 郵箱發(fā)送
?
/*** 項(xiàng)目文件根路徑* @return*/public static String rootPath() {return System.getProperty("user.dir"); }}?
?
// 發(fā)送郵件public static Boolean sendEmail() {final Properties props = new Properties();//登入郵箱服務(wù)器是需要驗(yàn)證的props.put("mail.smtp.auth", "true");props.put("mail.smtp.host", "smtp.163.com");props.put("mail.smtp.port", 25);//設(shè)置協(xié)議props.put("mail.transport.protocol", "smtp");// 發(fā)件人的賬號(hào)props.put("mail.user", "ddddddddd@163.com");// 訪問SMTP服務(wù)時(shí)需要提供的密碼 非常重要 不是你登 陸郵箱的密碼 是需要到163 郵箱設(shè)置的SMTP的密碼props.put("mail.password", "mima");// 構(gòu)建授權(quán)信息,用于進(jìn)行SMTP進(jìn)行身份驗(yàn)證Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// 用戶名、密碼String userName = props.getProperty("mail.user");String password = props.getProperty("mail.password");return new PasswordAuthentication(userName, password);}};// 使用環(huán)境屬性和授權(quán)信息,創(chuàng)建郵件會(huì)話Session mailSession = Session.getInstance(props, authenticator); // mailSession.setDebug(true);// 創(chuàng)建郵件消息MimeMessage message = new MimeMessage(mailSession);try { String nick = "";nick = javax.mail.internet.MimeUtility.encodeText("title"); // 設(shè)置發(fā)件人InternetAddress from = new InternetAddress(nick + " <"+props.getProperty("mail.user") + ">");message.setFrom(from);Address[] a = new Address[1];// 接收方回復(fù)的郵件地址a[0] = new InternetAddress("eeeeeee@qq.com");message.setReplyTo(a);// 設(shè)置收件人InternetAddress to = new InternetAddress("shoujianren@qq.com");message.setRecipient(MimeMessage.RecipientType.TO, to);// 設(shè)置郵件標(biāo)題message.setSubject("mailtitle");//添加附件部分//郵件內(nèi)容部分1---文本內(nèi)容MimeBodyPart body0 = new MimeBodyPart(); //郵件中的文字部分body0.setContent("<p>啦啦啦啦</p>","text/html;charset=utf-8");//郵件內(nèi)容部分2---附件1MimeBodyPart body1 = new MimeBodyPart(); //附件1body1.setDataHandler( new DataHandler( new FileDataSource (UlegalZCUtil.rootPath() +File.separator + "pdf" + File.separator + "templateOL" + ".pdf")) ) ;//./代表項(xiàng)目根目錄下 body1.setFileName( MimeUtility.encodeText("拉拉.pdf") );//中文附件名,解決亂碼//把上面的3部分組裝在一起,設(shè)置到msg中MimeMultipart mm = new MimeMultipart();mm.addBodyPart(body0);mm.addBodyPart(body1);message.setContent(mm);// 設(shè)置郵件的內(nèi)容體 // message.setContent("題在我使用postman來上傳圖片時(shí)候 ,死活都沒過。。顯示這個(gè),問題在哪呢?","text/html;charset=UTF-8");// 發(fā)送郵件 Transport.send(message);}catch (Exception e) {String err = e.getMessage();// 在這里處理message內(nèi)容, 格式是固定的System.out.println("====:"+err);return false;}return true;}? 3. 如果是qq郵箱的話需要在上面的配置添加ssl加密
//開啟了 SSL 加密//開啟安全協(xié)議MailSSLSocketFactory sf = null;try {sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);} catch (GeneralSecurityException e1) {e1.printStackTrace();}props.put("mail.smtp.ssl.enable", "true");props.put("mail.smtp.ssl.socketFactory", sf);?4. 經(jīng)過的我的實(shí)驗(yàn)如果將項(xiàng)目部署到阿里云服務(wù)器,以163郵箱 為基準(zhǔn)發(fā)送郵件
的話是不能成功的,以為163郵箱是25端口與阿里沖突,
?? 后期我以qq郵箱為基準(zhǔn)發(fā)送郵件,但是163郵箱接收不到郵件,目前還沒有找到解決辦法
?? 我的想法是采用阿里云郵箱,應(yīng)該沒有問題。。。
?
?
注意 密碼不是郵箱的登陸密碼
轉(zhuǎn)載于:https://www.cnblogs.com/memoryXudy/p/7680610.html
總結(jié)
以上是生活随笔為你收集整理的java springboot+maven发送邮件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [译] 虚拟现实是如何改变用户体验的:从
- 下一篇: Mac下显示隐藏文件