java 163邮件发送_使用SMTP发送163简单实现发送邮件(一)

java中使用qq进行邮件发送邮件的时候,需要在qq邮箱中开启smtp,获取授权码,具体详情可以登录http://service.mail.qq.com/cgi-bin/help?id=28进行查看

尽量不要使用qq邮箱进行发送邮件。比较麻烦,后期持续更新qq发送

注意:本教程在mac下进行操作的,倒入jar的同时不会像windows那样的麻烦,不需要手动把mail.jar和activation.jar文件添加到您的 CLASSPATH 中。要是windows别忘记添加路径!

使用163进行发送邮件:

第一步:导入activation.jar、mail.jar包到工程中

您可以从 Java 网站下载最新版本的JavaMail,打开网页右侧有个Downloads链接,点击它下载。

您可以从 Java 网站下载最新版本的JAF(版本 1.1.1)。

你也可以使用本站提供的下载链接:

第二步:上代码

packageEmploee;

importjava.io.*;

importjava.net.PasswordAuthentication;

importjava.util.*;

importjavax.mail.*;

importjavax.mail.internet.*;

importsun.security.util.Password;

importjavax.activation.*;

publicclassEmploee {

publicEmploee() {

//TODOAuto-generated constructor stub

}

publicstaticvoidmain(String[]args) {

//TODOAuto-generated methodstu

Stringto="对方的邮箱账户";//对方的邮箱账户

Stringfrome="自己的@163.com";

Stringpsd="密码";

// 指定发送邮件的主机为localhost

Stringhost="smtp.163.com";

// 获取系统属性

Propertiesproperties= System.getProperties();

// 设置邮件服务器

properties.setProperty("mail.smtp.host",host);

properties.put("mail.smtp.auth","true") ;//是否设置验证码

MyAuthenticatormyauth=newMyAuthenticator(frome,psd);

// 获取默认session对象

Sessionsession= Session.getDefaultInstance(properties,myauth);

try{

// 创建默认的 MimeMessage 对象

MimeMessagemessage=newMimeMessage(session);

// Set From: 头部头字段

message.setFrom(newInternetAddress(frome));

// Set To: 头部头字段

message.addRecipient(Message.RecipientType.TO,

newInternetAddress(to));

// Set Subject: 头部头字段

message.setSubject("这个是重要的邮件");

// 设置消息体

message.setText("我靠,qq邮箱好像需要进行验证!!!夜晚去郑大打球,我新学会一个姿势");

// 发送消息

Transport.send(message);

System.out.println("已发送!");

}catch(MessagingExceptionmex) {

mex.printStackTrace();

}

}

staticclassMyAuthenticator

extendsjavax.mail.Authenticator {

publicStringstrUser;

publicStringstrPwd;

publicMyAuthenticator(Stringuser, Stringpassword) {

this.strUser=user;

this.strPwd=password;

}

protectedjavax.mail.PasswordAuthentication getPasswordAuthentication() {

returnnewjavax.mail.PasswordAuthentication(strUser,strPwd);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值