JAVAMAIL发邮件

java 代码
  1. public   class   EMail   {      
  2.           Properties   props   =   new   Properties();      
  3.           Session   sendMailSession;      
  4.           Store   store;      
  5.           Transport   transport;      
  6.        
  7.           String   usr   =   "";   //这里填写你发信者的邮箱地址      
  8.           String   pwd   =   "";   //这里填写你发信者的邮箱密码      
  9.           String   smtpServer   =   "";   //这里填写smtp服务器的名称      
  10.           String   mailContent   =   "";   //这里填写邮件内容      
  11.           String   toAddress   =   "";   //这里填写收件人的地址;      
  12.           String   fromAddress   =   "";   //这里填写发件人的地址      
  13.           String   mailSubject   =   "";   //填写邮件的主题      
  14.        
  15.           public   EMail()   {      
  16.        
  17.           }      
  18.        
  19.           public   boolean   Init()   {      
  20.                   try   {      
  21.                           Properties   p   =   new   Properties();      
  22.                           String   fileName   =   "/ApplicationResources.properties";   //指定资源文件      
  23.                           InputStream   is   =   getClass().getResourceAsStream(fileName);   //打开资源文件      
  24.                           p.load(is);   //装载资源文件      
  25.                           this.smtpServer=p.getProperty("mail.smtp.host");      
  26.                           this.usr=p.getProperty("mail.from.user");      
  27.                           this.pwd=p.getProperty("mail.from.psw");      
  28.                           this.fromAddress=p.getProperty("mail.from.address");      
  29.                   }   catch   (Exception   ex)   {      
  30.                           return   false;      
  31.                   }      
  32.                   return   true;      
  33.           }      
  34.        
  35.           public   boolean   SendMail()   {      
  36.                   try   {      
  37.        
  38.                           props.put("mail.smtp.host",smtpServer);   //这里填写你发信者的SMTP主机,如:smtp.sohu.com      
  39.                           props.put("mail.smtp.user",   usr);      
  40.                           props.put("mail.smtp.password",   pwd);      
  41.                           props.put("mail.smtp.auth",   "true");      
  42.                           sendMailSession   =   Session.getInstance(props,   new   Authenticator()   {      
  43.                                   public   PasswordAuthentication   getPasswordAuthentication()   {      
  44.                                           return   new   PasswordAuthentication(usr,pwd);      
  45.                                   }   //这里填写你发信者的邮箱地址和密码      
  46.                           });   //如果邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的,我的邮箱要验证,所以得这么写。      
  47.                           Message   newMessage   =   new   MimeMessage(sendMailSession);      
  48.                           newMessage.setFrom(new   InternetAddress(fromAddress));      
  49.                           newMessage.setRecipient(Message.RecipientType.TO,      
  50.                                                                           new      
  51.                                                                           InternetAddress(toAddress));      
  52.                           newMessage.setSubject(mailSubject);      
  53.                           newMessage.setSentDate(new   Date());      
  54.                           newMessage.setText(mailContent);      
  55.                           transport   =   sendMailSession.getTransport("smtp");      
  56.                           transport.send(newMessage);      
  57.                   }   catch   (Exception   m)   {      
  58.                           m.printStackTrace();      
  59.                           return   false;      
  60.                   }      
  61.                   return   true;      
  62.           }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值