Java邮件发送-亚马逊Simple EMail Service (SES)作SMTP服务器
AWS SES使用介紹可見:https://docs.aws.amazon.com/zh_cn/ses/latest/DeveloperGuide/Welcome.html
總結為一下兩點即可:
1. 登陸AWS進入控制臺,然后點擊SMTP Settings,創建SMTP賬戶:Create My SMTP Credentials,按提示操作生成username and password,這個看起來類似IAM User的key。
AWS控制臺地址為:https://console.aws.amazon.com
2.驗證發送郵件地址,另外如果發給其他人,也需要在這里驗證,系統會發郵件到對方郵箱讓他確認,這樣對方才能收到SES發來的郵件,就跟我們訂閱新聞郵件類似。
開發 :直接使用文檔中得demo即可,這里我選用此demo,也可集成AWS的。
需要下載javax.mail.jar,代碼中改換的參數換掉。
?
附加我的代碼(加了圖片的),部分代碼為其他引用:
public void CreateMessageIn() throws MessagingException, IOException {Tools tls = new Tools(); //String subject = "Important feedback";// ****************************創建會話***************************************final Properties props = new Properties();String emlDestUp = ldest.toUpperCase(); // if (emlDestUp.contains("@QQ.COM") || emlDestUp.contains("@PIONEER.NET.AU") ) {if (emlDestUp.contains("@QQ.COM")) {props.put("mail.smtp.host", "smtp.mxhichina.com");// 發件人使用發郵件的電子信箱服務器props.put("mail.smtp.auth", "true");props.put("mail.smtp.starttls.enable", "true");props.put("mail.user", "service@****.com");props.put("mail.password", "P***123456");props.put("mail.smtp.port", "25"); } else {props.put("mail.smtp.host", "email-smtp.us-east-1.amazonaws.com");// 發件人使用發郵件的電子信箱服務器props.put("mail.smtp.auth", "true");props.put("mail.smtp.starttls.enable", "true");props.put("mail.smtp.port", "25");props.put("mail.user", "AKIAJPEOFR*10*");props.put("mail.password", "AuhmFfe1lMRSVxPpo44ZfQjjPGqyiZsymz*10*");}/** http://blog.csdn.net/u013076997/article/details/53760828?locationNum=14&fps=1* javax.mail發送郵件(帶附件)* http://blog.csdn.net/wangxinqn/article/details/1708705* 近日使用javamail 為公司的軟件添加了郵件收發功能。遇到了Unsupported record version Unknown-50.49異常。該異常只會在發送郵件的時候產生,而且是應為所有郵箱使用了SSL加密功能。應該是javamail包的問題初步的解決方案是在你的發送類里 加上props.put("mail.smtp.quitwait", "false");將該異常屏蔽調 */props.put("mail.smtp.quitwait", "false");Authenticator atctr = 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, atctr); // 獲得默認的session對象mailsession.setDebug(true);// *****************************構造消息**************************************MimeMessage msg = new MimeMessage(mailsession);InternetAddress from = null;if (lfrom == null ) {Properties pros = new Properties();pros.load(this.getClass().getClassLoader().getResourceAsStream("server.properties")); String fel = "service@paby.com";if (dfg > 0)fel = pros.getProperty("dreamemail");from = new InternetAddress(fel); } elsefrom = new InternetAddress(lfrom);msg.setFrom(from); // 發送者email帳號msg.setRecipient(Message.RecipientType.TO, new InternetAddress(ldest)); // 設置收件人地址并規定其類型if ( Constant.timeUtcFlag ) // true msg.setSentDate(tls.getUtcDateStrNowDate());elsemsg.setSentDate(new Date()); // 設置發信時間 msg.setSubject(ltitle); // 設置主題msg.setText(lcontent);msg.setContent(lcontent, "text/html;charset=UTF-8"); // 設置 正文if ( laddonName != null ) {/*// 向multipart對象中添加郵件的各個部分內容,包括文本內容和附件 Multipart multipart = new MimeMultipart(); // 設置郵件的文本內容 BodyPart contentPart = new MimeBodyPart(); contentPart.setText(lcontent);multipart.addBodyPart(contentPart);// 添加附件 BodyPart messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(laddonName); // 添加附件的內容 messageBodyPart.setDataHandler(new DataHandler(source));// 添加附件的標題 // 這里很重要,通過下面的Base64編碼的轉換可以保證你的中文附件標題名在發送時不會變成亂碼 messageBodyPart.setFileName(MimeUtility.encodeText("Image")); multipart.addBodyPart(messageBodyPart); msg.setContent(multipart); */ // 創建郵件正文 MimeBodyPart text = new MimeBodyPart();text.setContent(lcontent + "<br/><img src='cid:image_id'/>", "text/html;charset=UTF-8");// 創建圖片 MimeBodyPart img = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource(laddonName));//圖片路徑 img.setDataHandler(dh);img.setContentID("image_id"); // 創建圖片的一個表示用于顯示在郵件中顯示 MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(img); mm.setSubType("related");// 設置正文與圖片之間的關系 // 圖片與正文的 body MimeBodyPart all = new MimeBodyPart(); all.setContent(mm);msg.setContent(mm);}// 保存郵件 msg.saveChanges(); // 發送郵件Transport.send(msg);logger.info("email has sended to " + ldest);}? ? ? ? ? ? ? ??---------------------------------------------歡迎關注公眾號(生活不止有代碼)-------------------------------------------------------
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
總結
以上是生活随笔為你收集整理的Java邮件发送-亚马逊Simple EMail Service (SES)作SMTP服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: manjaro 安装分区以及配置方案
- 下一篇: 清华硕士炮轰字节恶意低薪:“月薪2万,硕