JavaMail 发送邮件
生活随笔
收集整理的這篇文章主要介紹了
JavaMail 发送邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JavaMail郵件發送
引用maven jar包
1 <dependency> 2 <groupId>javax.mail</groupId> 3 <artifactId>mail</artifactId> 4 <version>1.4.5</version> 5 </dependency>?
發送郵件函數
private static void sendMail(){// 配置發送郵件的環境屬性final Properties props = new Properties();/** 可用的屬性: mail.store.protocol / mail.transport.protocol / mail.host /* mail.user / mail.from*/// 表示SMTP發送郵件,需要進行身份驗證props.put("mail.smtp.auth", "true");props.put("mail.smtp.host", "smtp.163.com");// 發件人的賬號props.put("mail.user", "*****@163.com");// 訪問SMTP服務時需要提供的密碼props.put("mail.password", "******");// 構建授權信息,用于進行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);// 創建郵件消息MimeMessage message = new MimeMessage(mailSession);try {// 設置發件人InternetAddress form = null;form = new InternetAddress(props.getProperty("mail.user"));message.setFrom(form);// 設置收件人InternetAddress to = new InternetAddress(****@163.com);message.setRecipient(Message.RecipientType.TO, to);// 設置郵件標題message.setSubject("測試郵件");// 設置郵件的內容體message.setContent("<a href='http://www.cnblogs.com/dawnheaven/'>測試的HTML郵件</a>", "text/html;charset=UTF-8");// 發送郵件 Transport.send(message);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}?
?
轉載于:https://www.cnblogs.com/dawnheaven/p/5057150.html
總結
以上是生活随笔為你收集整理的JavaMail 发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PhpForm表单验证
- 下一篇: 【总结】在VirtualBox上面安装M