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