利用mail.jar发送邮件(简单版)
2016-03-31 10:19
489 查看
下载mail.jar
打开邮箱的smtp服务(以QQ邮箱为例)
点击设置,进入账户
开启SMTP服务
记住授权码,千万别泄露
测试代码:
打开邮箱的smtp服务(以QQ邮箱为例)
点击设置,进入账户
开启SMTP服务
记住授权码,千万别泄露
测试代码:
package test; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.MimeMessage; public class Test { static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; public static void main(String[] args) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.qq.com");//服务器名称 //设置SSL,否则QQ邮箱不允许发送 props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.from", "2028953318@qq.com");//发送方邮箱地址 props.put("mail.smtp.auth", "true");//需要验证,不验证会提示没有权限发送 props.put("mail.smtp.user", "YuFeng");//发送方的发送名; props.put("mail.debug", "true");//输出相关信息(可以设置false不输出) Authenticator auth = new Authenticator() {//设置验证信息 public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("2028953318@qq.com", "*******");//用户名+授权码 } }; Session session = Session.getInstance(props, auth); try { MimeMessage msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, "*@163.com");// msg.setSubject("主题"); msg.setSentDate(new Date()); try { msg.setText(new String("正文\n".getBytes(),"UTF-8"));//设置编码格式 } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } Transport.send(msg); } catch (MessagingException mex) { System.out.println("send failed, exception: " + mex); } } }
相关文章推荐
- [POJ 3411]Paid Roads[BFS][状压]
- LVM逻辑卷管理-软RAID磁盘阵列
- 听说alphago又要挑战sc2了?——我眼中的人工智能
- LeetCode Contains Duplicate
- LeetCode Contains Duplicate II
- linux下设置SMTP服务器并用mail命令发送邮件精解
- 什么是IO(七)- RAID
- 使用UltraISO制作U盘启动盘
- metaio AR 官方教学视频 Edge Based Initialization and Tracking
- metaio AR 官方教学视频 Edge Based Initialization and Tracking
- Metaio识别图片要求(Image Tracking)
- Metaio识别图片要求(Image Tracking)
- 黑苹果 安装卡在 GeforceSensors:still waiting for accelerator to start
- 黑苹果 安装卡在 GeforceSensors:still waiting for accelerator to start
- Maven创建项目: Failed to execute goal org.apache.maven.plugin( mvn archetype:create)
- Rails之migration总结
- Maven创建项目: Failed to execute goal org.apache.maven.plugin( mvn archetype:create)
- 笔记:unsupervised domain adaptation by backpropagation
- 【USACO题库】5.2.1 Snail Trails蜗牛的旅行
- POJ - 1363 Rails