JavaMail发邮件

  1. packagecom.usernet.stk.mail.util;
  2. importjava.util.Date;
  3. importjava.util.Properties;
  4. importjavax.mail.Authenticator;
  5. importjavax.mail.Message;
  6. importjavax.mail.MessagingException;
  7. importjavax.mail.PasswordAuthentication;
  8. importjavax.mail.Session;
  9. importjavax.mail.Transport;
  10. importjavax.mail.internet.AddressException;
  11. importjavax.mail.internet.InternetAddress;
  12. importjavax.mail.internet.MimeMessage;
  13. importconf.Property;
  14. publicclassSender{
  15. /**
  16. *@return
  17. *@throwsMessagingException
  18. *@throwsAddressException
  19. */
  20. publicstaticbooleansender(Stringto,Stringcontent)
  21. throwsAddressException,MessagingException{
  22. try{
  23. Propertiesprops=newProperties();
  24. props.put("mail.smtp.host",Property.getPara("conf","mailhost"));
  25. props.put("mail.smtp.auth","true");
  26. //session
  27. SessionsendMailSession=Session.getInstance(props,
  28. newAuthenticator(){
  29. publicPasswordAuthenticationgetPasswordAuthentication(){
  30. returnnewPasswordAuthentication(Property.getPara(
  31. "conf","mailaccounts"),Property.getPara(
  32. "conf","mailpasswod"));
  33. }
  34. });
  35. //message
  36. MessagenewMessage=newMimeMessage(sendMailSession);
  37. //from
  38. newMessage.setFrom(newInternetAddress(Property.getPara("conf",
  39. "from")));
  40. //to
  41. newMessage.setRecipient(Message.RecipientType.TO,
  42. newInternetAddress(to));
  43. //subject
  44. newMessage.setSubject(Property.getPara("conf","subject"));
  45. //senddate
  46. newMessage.setSentDate(newDate());
  47. //content
  48. newMessage.setContent(content,"text/html;charset=UTF-8");
  49. //newMessage.setText(content);
  50. //newMessage.setHeader("Content-Type","text/plain;charset=gb2312");
  51. //newMessage.setHeader("Content-Transfer-Encoding","8bit");
  52. //
  53. Transporttransport=sendMailSession.getTransport("smtp");
  54. transport.send(newMessage);
  55. returntrue;
  56. }catch(AddressExceptionae){
  57. ae.printStackTrace();
  58. returnfalse;
  59. }catch(MessagingExceptionme){
  60. me.printStackTrace();
  61. returnfalse;
  62. }
  63. }
  64. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值