Java发送邮件

  1. importjava.io.UnsupportedEncodingException;
  2. importjava.util.Date;
  3. importjava.util.Properties;
  4. importjavax.mail.Address;
  5. importjavax.mail.Message;
  6. importjavax.mail.MessagingException;
  7. importjavax.mail.Session;
  8. importjavax.mail.Transport;
  9. importjavax.mail.internet.AddressException;
  10. importjavax.mail.internet.InternetAddress;
  11. importjavax.mail.internet.MimeMessage;
  12. /**
  13. *邮件发送
  14. */
  15. publicclassSendMail{
  16. /**
  17. *不需要验证的邮件发送
  18. *StringfromMail发件人地址
  19. *Stringpassword发件人密码
  20. *StringtoMail收件人地址
  21. *StringmessageText发送的消息
  22. *Stringtitle发送的标题
  23. *StringserviceName使用的邮件服务器
  24. *@throwsException
  25. */
  26. publicstaticvoidsetMessage(StringfromMail,StringtoMail,
  27. StringmessageText,StringserviceName)throwsException{
  28. Propertiesprops=System.getProperties();
  29. props.put("mail.smtp.host",serviceName);//设置smtp的服务器地址:该邮件服务器不需要身份验证
  30. props.put("mail.smtp.auth","false");//设置smtp服务器要身份验证:缺省设置为false
  31. Addressfrom=newInternetAddress(fromMail);
  32. Addressto=newInternetAddress(toMail);
  33. Sessionsession=Session.getDefaultInstance(props,null);
  34. Messagemessage=newMimeMessage(session);
  35. message.setFrom(from);
  36. message.addRecipient(Message.RecipientType.TO,to);
  37. message.setText(messageText);
  38. Transport.send(message);
  39. }
  40. /**
  41. *带授权的发送邮件
  42. *StringfromMail发件人地址
  43. *Stringpassword发件人密码
  44. *StringtoMail收件人地址
  45. *StringmessageText发送的消息
  46. *Stringtitle发送的标题
  47. *StringserviceName使用的邮件服务器
  48. *@throwsException
  49. */
  50. publicstaticvoidsetMessageWithAuthentica(StringfromMail,Stringpassword,StringtoMail,StringmessageText,Stringtitle,StringserviceName)throwsException{
  51. Propertiesprops=newProperties();
  52. props.put("mail.smtp.host",serviceName);//设置smtp的服务器地址是smtp.sohu.com
  53. props.put("mail.transport.protocol","smtp");
  54. props.put("mail.smtp.auth","true");//设置smtp服务器要身份验证。
  55. StringformMailName=fromMail.split("@")[0];
  56. StringtoMailName=toMail.split("@")[0];
  57. PopupAuthenticatorauth=newPopupAuthenticator(formMailName,fromMail);
  58. Sessionsession=Session.getDefaultInstance(props,auth);
  59. session.setDebug(true);
  60. //发送人地址
  61. Transporttransport=null;
  62. try{
  63. AddressaddressFrom=newInternetAddress(fromMail,formMailName);
  64. //收件人地址
  65. Address[]addressTo=newAddress[]{newInternetAddress(toMail,toMailName)};
  66. //抄送地址
  67. //AddressaddressCopy=newInternetAddress("[email protected]","GeorgeBush");
  68. Messagemessage=newMimeMessage(session);
  69. message.setText(messageText);
  70. message.setSubject(title);
  71. message.setFrom(addressFrom);
  72. message.addRecipients(Message.RecipientType.TO,addressTo);
  73. //message.addRecipient(Message.RecipientType.CC,addressCopy);
  74. message.saveChanges();
  75. //session.setDebug(true);
  76. transport=session.getTransport("smtp");//创建连接
  77. transport.connect(serviceName,fromMail,password);//连接服务器
  78. transport.sendMessage(message,addressTo);//发送信息
  79. transport.close();//关闭连接
  80. }catch(UnsupportedEncodingExceptione){
  81. e.printStackTrace();
  82. Loggers.error("发送邮件错误:SendMail--setMessageWithAuthentica--UnsupportedEncodingException:"+e);
  83. }catch(MessagingExceptione){
  84. e.printStackTrace();
  85. Loggers.error("发送邮件错误:SendMail--setMessageWithAuthentica--MessagingException"+e);
  86. }finally{
  87. if(transport!=null&transport.isConnected()){
  88. transport.close();
  89. }
  90. }
  91. }
  92. publicstaticvoidsetMsg(StringtoMail,Stringtitle,Stringcontent)throwsMessagingException{
  93. Propertiesprops=newProperties();
  94. props.put("mail.smtp.host","smtp.163.com");
  95. props.put("mail.smtp.auth","true");
  96. Sessions=Session.getInstance(props);
  97. s.setDebug(true);
  98. MimeMessagemessage=newMimeMessage(s);
  99. Transporttransport=null;
  100. try{
  101. InternetAddressfrom=newInternetAddress("[email protected]");
  102. message.setFrom(from);
  103. InternetAddressto=newInternetAddress(toMail);
  104. message.setRecipient(Message.RecipientType.TO,to);
  105. message.setSubject(title);
  106. message.setText(content);
  107. message.setSentDate(newDate());
  108. message.saveChanges();
  109. transport=s.getTransport("smtp");
  110. transport.connect("smtp.163.com","xxx","xxx");
  111. transport.sendMessage(message,message.getAllRecipients());
  112. transport.close();
  113. }catch(AddressExceptione){
  114. e.printStackTrace();
  115. Loggers.error("发送邮件错误:SendMail--setMsg--AddressException:"+e);
  116. }catch(MessagingExceptione){
  117. e.printStackTrace();
  118. Loggers.error("发送邮件错误:SendMail--setMsg--MessagingException:"+e);
  119. }finally{
  120. if(transport!=null&transport.isConnected()){
  121. transport.close();
  122. }
  123. }
  124. }
  125. publicstaticvoidmain(String[]args){
  126. try{
  127. SendMail.setMessageWithAuthentica("您的邮箱","您的密码","对方邮箱","邮件内容","邮件标题","服务器地址");
  128. }catch(Exceptione){
  129. e.printStackTrace();
  130. }
  131. }
  132. }

  1. importjavax.mail.Authenticator;
  2. importjavax.mail.PasswordAuthentication;
  3. publicclassPopupAuthenticatorextendsAuthenticator{
  4. privateStringuname;//用户名
  5. privateStringpassword;//密码
  6. //属性方法
  7. publicStringgetUname(){
  8. returnuname;
  9. }
  10. publicvoidsetUname(Stringuname){
  11. this.uname=uname;
  12. }
  13. publicStringgetPassword(){
  14. returnpassword;
  15. }
  16. publicvoidsetPassword(Stringpassword){
  17. this.password=password;
  18. }
  19. //构造方法
  20. publicPopupAuthenticator(){
  21. }
  22. publicPopupAuthenticator(Stringuname,Stringpassword){
  23. this.uname=uname;
  24. this.password=password;
  25. }
  26. publicPasswordAuthenticationgetPasswordAuthentication(){
  27. Stringusername=uname;//邮箱登录帐号
  28. Stringpwd=password;//登录密码
  29. returnnewPasswordAuthentication(username,pwd);
  30. }
  31. }

你可能感兴趣的:(java)