实现java发送邮件的过程大体有以下几步:

  1. 准备一个properties文件,该文件中存放SMTP服务器地址等参数。
  2. 利用properties创建一个Session对象
  3. 利用Session创建Message对象,然后设置邮件主题和正文
  4. 利用Transport对象发送邮件

需要的jar有2个:activation.jar和mail.jar

直接看个demo代码

#----------------这两个是构建session必须的字段----------
#smtp服务器
mail.smtp.host=smtp.qq.com
#身份验证
mail.smtp.auth=true
#--------------------------------------------------------------

#发送者的邮箱用户名
mail.sender.username=xxx@xx.com
#发送者的邮箱密码
mail.sender.password=xxxxxxxxxx
MailServer.properties

相关文章: