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. }

你可能感兴趣的:(html)