`
hbhgjiangkun
  • 浏览: 95956 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

javamail发送带附件的邮件

 
阅读更多
  1. packagemail;
  2. importjava.util.*;
  3. importjava.io.*;
  4. importjavax.mail.*;
  5. importjavax.mail.internet.*;
  6. importjavax.activation.*;
  7. publicclassMail{
  8. //定义发件人、收件人、SMTP服务器、用户名、密码、主题、内容等
  9. privateStringdisplayName;
  10. privateStringto;
  11. privateStringfrom;
  12. privateStringsmtpServer;
  13. privateStringusername;
  14. privateStringpassword;
  15. privateStringsubject;
  16. privateStringcontent;
  17. privatebooleanifAuth;//服务器是否要身份认证
  18. privateStringfilename="";
  19. privateVectorfile=newVector();//用于保存发送附件的文件名的集合
  20. /**
  21. *设置SMTP服务器地址
  22. */
  23. publicvoidsetSmtpServer(StringsmtpServer){
  24. this.smtpServer=smtpServer;
  25. }
  26. /**
  27. *设置发件人的地址
  28. */
  29. publicvoidsetFrom(Stringfrom){
  30. this.from=from;
  31. }
  32. /**
  33. *设置显示的名称
  34. */
  35. publicvoidsetDisplayName(StringdisplayName){
  36. this.displayName=displayName;
  37. }
  38. /**
  39. *设置服务器是否需要身份认证
  40. */
  41. publicvoidsetIfAuth(booleanifAuth){
  42. this.ifAuth=ifAuth;
  43. }
  44. /**
  45. *设置E-mail用户名
  46. */
  47. publicvoidsetUserName(Stringusername){
  48. this.username=username;
  49. }
  50. /**
  51. *设置E-mail密码
  52. */
  53. publicvoidsetPassword(Stringpassword){
  54. this.password=password;
  55. }
  56. /**
  57. *设置接收者
  58. */
  59. publicvoidsetTo(Stringto){
  60. this.to=to;
  61. }
  62. /**
  63. *设置主题
  64. */
  65. publicvoidsetSubject(Stringsubject){
  66. this.subject=subject;
  67. }
  68. /**
  69. *设置主体内容
  70. */
  71. publicvoidsetContent(Stringcontent){
  72. this.content=content;
  73. }
  74. /**
  75. *该方法用于收集附件名
  76. */
  77. publicvoidaddAttachfile(Stringfname){
  78. file.addElement(fname);
  79. }
  80. publicMail(){
  81. }
  82. /**
  83. *初始化SMTP服务器地址、发送者E-mail地址、用户名、密码、接收者、主题、内容
  84. */
  85. publicMail(StringsmtpServer,Stringfrom,StringdisplayName,Stringusername,Stringpassword,Stringto,Stringsubject,Stringcontent){
  86. this.smtpServer=smtpServer;
  87. this.from=from;
  88. this.displayName=displayName;
  89. this.ifAuth=true;
  90. this.username=username;
  91. this.password=password;
  92. this.to=to;
  93. this.subject=subject;
  94. this.content=content;
  95. }
  96. /**
  97. *初始化SMTP服务器地址、发送者E-mail地址、接收者、主题、内容
  98. */
  99. publicMail(StringsmtpServer,Stringfrom,StringdisplayName,Stringto,Stringsubject,Stringcontent){
  100. this.smtpServer=smtpServer;
  101. this.from=from;
  102. this.displayName=displayName;
  103. this.ifAuth=false;
  104. this.to=to;
  105. this.subject=subject;
  106. this.content=content;
  107. }
  108. /**
  109. *发送邮件
  110. */
  111. publicHashMapsend(){
  112. HashMapmap=newHashMap();
  113. map.put("state","success");
  114. Stringmessage="邮件发送成功!";
  115. Sessionsession=null;
  116. Propertiesprops=System.getProperties();
  117. props.put("mail.smtp.host",smtpServer);
  118. if(ifAuth){//服务器需要身份认证
  119. props.put("mail.smtp.auth","true");
  120. SmtpAuthsmtpAuth=newSmtpAuth(username,password);
  121. session=Session.getDefaultInstance(props,smtpAuth);
  122. }else{
  123. props.put("mail.smtp.auth","false");
  124. session=Session.getDefaultInstance(props,null);
  125. }
  126. session.setDebug(true);
  127. Transporttrans=null;
  128. try{
  129. Messagemsg=newMimeMessage(session);
  130. try{
  131. Addressfrom_address=newInternetAddress(from,displayName);
  132. msg.setFrom(from_address);
  133. }catch(java.io.UnsupportedEncodingExceptione){
  134. e.printStackTrace();
  135. }
  136. InternetAddress[]address={newInternetAddress(to)};
  137. msg.setRecipients(Message.RecipientType.TO,address);
  138. msg.setSubject(subject);
  139. Multipartmp=newMimeMultipart();
  140. MimeBodyPartmbp=newMimeBodyPart();
  141. mbp.setContent(content.toString(),"text/html;charset=gb2312");
  142. mp.addBodyPart(mbp);
  143. if(!file.isEmpty()){//有附件
  144. Enumerationefile=file.elements();
  145. while(efile.hasMoreElements()){
  146. mbp=newMimeBodyPart();
  147. filename=efile.nextElement().toString();//选择出每一个附件名
  148. FileDataSourcefds=newFileDataSource(filename);//得到数据源
  149. mbp.setDataHandler(newDataHandler(fds));//得到附件本身并至入BodyPart
  150. mbp.setFileName(fds.getName());//得到文件名同样至入BodyPart
  151. mp.addBodyPart(mbp);
  152. }
  153. file.removeAllElements();
  154. }
  155. msg.setContent(mp);//Multipart加入到信件
  156. msg.setSentDate(newDate());//设置信件头的发送日期
  157. //发送信件
  158. msg.saveChanges();
  159. trans=session.getTransport("smtp");
  160. trans.connect(smtpServer,username,password);
  161. trans.sendMessage(msg,msg.getAllRecipients());
  162. trans.close();
  163. }catch(AuthenticationFailedExceptione){
  164. map.put("state","failed");
  165. message="邮件发送失败!错误原因:/n"+"身份验证错误!";
  166. e.printStackTrace();
  167. }catch(MessagingExceptione){
  168. message="邮件发送失败!错误原因:/n"+e.getMessage();
  169. map.put("state","failed");
  170. e.printStackTrace();
  171. Exceptionex=null;
  172. if((ex=e.getNextException())!=null){
  173. System.out.println(ex.toString());
  174. ex.printStackTrace();
  175. }
  176. }
  177. //System.out.println("/n提示信息:"+message);
  178. map.put("message",message);
  179. returnmap;
  180. }
  181. }


SmtpAuth.java 代码
  1. packagemail;
  2. publicclassSmtpAuthextendsjavax.mail.Authenticator{
  3. privateStringusername,password;
  4. publicSmtpAuth(Stringusername,Stringpassword){
  5. this.username=username;
  6. this.password=password;
  7. }
  8. protectedjavax.mail.PasswordAuthenticationgetPasswordAuthentication(){
  9. returnnewjavax.mail.PasswordAuthentication(username,password);
  10. }
  11. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics