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