javamail发送邮件

今天学习了一下JavaMail,javamail发送邮件确实是一个比较麻烦的问题。为了以后使用方便,自己写了段代码,打成jar包,以方便以后使用。呵呵

以下三段代码是我的全部代码,朋友们如果想用,直接复制即可。jar包因为我不知道怎么传到javaeye上,所以朋友们回去自己打吧。
我的代码有三个类:

第一个类:MailSenderInfo.java

  1. packagecom.util.mail;
  2. /**
  3. *发送邮件需要使用的基本信息
  4. */
  5. importjava.util.Properties;
  6. publicclassMailSenderInfo{
  7. //发送邮件的服务器的IP和端口
  8. privateStringmailServerHost;
  9. privateStringmailServerPort="25";
  10. //邮件发送者的地址
  11. privateStringfromAddress;
  12. //邮件接收者的地址
  13. privateStringtoAddress;
  14. //登陆邮件发送服务器的用户名和密码
  15. privateStringuserName;
  16. privateStringpassword;
  17. //是否需要身份验证
  18. privatebooleanvalidate=false;
  19. //邮件主题
  20. privateStringsubject;
  21. //邮件的文本内容
  22. privateStringcontent;
  23. //邮件附件的文件名
  24. privateString[]attachFileNames;
  25. /**
  26. *获得邮件会话属性
  27. */
  28. publicPropertiesgetProperties(){
  29. Propertiesp=newProperties();
  30. p.put("mail.smtp.host",this.mailServerHost);
  31. p.put("mail.smtp.port",this.mailServerPort);
  32. p.put("mail.smtp.auth",validate?"true":"false");
  33. returnp;
  34. }
  35. publicStringgetMailServerHost(){
  36. returnmailServerHost;
  37. }
  38. publicvoidsetMailServerHost(StringmailServerHost){
  39. this.mailServerHost=mailServerHost;
  40. }
  41. publicStringgetMailServerPort(){
  42. returnmailServerPort;
  43. }
  44. publicvoidsetMailServerPort(StringmailServerPort){
  45. this.mailServerPort=mailServerPort;
  46. }
  47. publicbooleanisValidate(){
  48. returnvalidate;
  49. }
  50. publicvoidsetValidate(booleanvalidate){
  51. this.validate=validate;
  52. }
  53. publicString[]getAttachFileNames(){
  54. returnattachFileNames;
  55. }
  56. publicvoidsetAttachFileNames(String[]fileNames){
  57. this.attachFileNames=fileNames;
  58. }
  59. publicStringgetFromAddress(){
  60. returnfromAddress;
  61. }
  62. publicvoidsetFromAddress(StringfromAddress){
  63. this.fromAddress=fromAddress;
  64. }
  65. publicStringgetPassword(){
  66. returnpassword;
  67. }
  68. publicvoidsetPassword(Stringpassword){
  69. this.password=password;
  70. }
  71. publicStringgetToAddress(){
  72. returntoAddress;
  73. }
  74. publicvoidsetToAddress(StringtoAddress){
  75. this.toAddress=toAddress;
  76. }
  77. publicStringgetUserName(){
  78. returnuserName;
  79. }
  80. publicvoidsetUserName(StringuserName){
  81. this.userName=userName;
  82. }
  83. publicStringgetSubject(){
  84. returnsubject;
  85. }
  86. publicvoidsetSubject(Stringsubject){
  87. this.subject=subject;
  88. }
  89. publicStringgetContent(){
  90. returncontent;
  91. }
  92. publicvoidsetContent(StringtextContent){
  93. this.content=textContent;
  94. }
  95. }
第二个类:SimpleMailSender.java
Java代码 收藏代码
  1. packagecom.util.mail;
  2. importjava.util.Date;
  3. importjava.util.Properties;
  4. importjavax.mail.Address;
  5. importjavax.mail.BodyPart;
  6. importjavax.mail.Message;
  7. importjavax.mail.MessagingException;
  8. importjavax.mail.Multipart;
  9. importjavax.mail.Session;
  10. importjavax.mail.Transport;
  11. importjavax.mail.internet.InternetAddress;
  12. importjavax.mail.internet.MimeBodyPart;
  13. importjavax.mail.internet.MimeMessage;
  14. importjavax.mail.internet.MimeMultipart;
  15. /**
  16. *简单邮件(不带附件的邮件)发送器
  17. */
  18. publicclassSimpleMailSender{
  19. /**
  20. *以文本格式发送邮件
  21. *@parammailInfo待发送的邮件的信息
  22. */
  23. publicbooleansendTextMail(MailSenderInfomailInfo){
  24. //判断是否需要身份认证
  25. MyAuthenticatorauthenticator=null;
  26. Propertiespro=mailInfo.getProperties();
  27. if(mailInfo.isValidate()){
  28. //如果需要身份认证,则创建一个密码验证器
  29. authenticator=newMyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
  30. }
  31. //根据邮件会话属性和密码验证器构造一个发送邮件的session
  32. SessionsendMailSession=Session.getDefaultInstance(pro,authenticator);
  33. try{
  34. //根据session创建一个邮件消息
  35. MessagemailMessage=newMimeMessage(sendMailSession);
  36. //创建邮件发送者地址
  37. Addressfrom=newInternetAddress(mailInfo.getFromAddress());
  38. //设置邮件消息的发送者
  39. mailMessage.setFrom(from);
  40. //创建邮件的接收者地址,并设置到邮件消息中
  41. Addressto=newInternetAddress(mailInfo.getToAddress());
  42. mailMessage.setRecipient(Message.RecipientType.TO,to);
  43. //设置邮件消息的主题
  44. mailMessage.setSubject(mailInfo.getSubject());
  45. //设置邮件消息发送的时间
  46. mailMessage.setSentDate(newDate());
  47. //设置邮件消息的主要内容
  48. StringmailContent=mailInfo.getContent();
  49. mailMessage.setText(mailContent);
  50. //发送邮件
  51. Transport.send(mailMessage);
  52. returntrue;
  53. }catch(MessagingExceptionex){
  54. ex.printStackTrace();
  55. }
  56. returnfalse;
  57. }
  58. /**
  59. *以HTML格式发送邮件
  60. *@parammailInfo待发送的邮件信息
  61. */
  62. publicstaticbooleansendHtmlMail(MailSenderInfomailInfo){
  63. //判断是否需要身份认证
  64. MyAuthenticatorauthenticator=null;
  65. Propertiespro=mailInfo.getProperties();
  66. //如果需要身份认证,则创建一个密码验证器
  67. if(mailInfo.isValidate()){
  68. authenticator=newMyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
  69. }
  70. //根据邮件会话属性和密码验证器构造一个发送邮件的session
  71. SessionsendMailSession=Session.getDefaultInstance(pro,authenticator);
  72. try{
  73. //根据session创建一个邮件消息
  74. MessagemailMessage=newMimeMessage(sendMailSession);
  75. //创建邮件发送者地址
  76. Addressfrom=newInternetAddress(mailInfo.getFromAddress());
  77. //设置邮件消息的发送者
  78. mailMessage.setFrom(from);
  79. //创建邮件的接收者地址,并设置到邮件消息中
  80. Addressto=newInternetAddress(mailInfo.getToAddress());
  81. //Message.RecipientType.TO属性表示接收者的类型为TO
  82. mailMessage.setRecipient(Message.RecipientType.TO,to);
  83. //设置邮件消息的主题
  84. mailMessage.setSubject(mailInfo.getSubject());
  85. //设置邮件消息发送的时间
  86. mailMessage.setSentDate(newDate());
  87. //MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
  88. MultipartmainPart=newMimeMultipart();
  89. //创建一个包含HTML内容的MimeBodyPart
  90. BodyParthtml=newMimeBodyPart();
  91. //设置HTML内容
  92. html.setContent(mailInfo.getContent(),"text/html;charset=utf-8");
  93. mainPart.addBodyPart(html);
  94. //将MiniMultipart对象设置为邮件内容
  95. mailMessage.setContent(mainPart);
  96. //发送邮件
  97. Transport.send(mailMessage);
  98. returntrue;
  99. }catch(MessagingExceptionex){
  100. ex.printStackTrace();
  101. }
  102. returnfalse;
  103. }
  104. }

第三个类:MyAuthenticator.java
Java代码 收藏代码
  1. packagecom.util.mail;
  2. importjavax.mail.*;
  3. publicclassMyAuthenticatorextendsAuthenticator{
  4. StringuserName=null;
  5. Stringpassword=null;
  6. publicMyAuthenticator(){
  7. }
  8. publicMyAuthenticator(Stringusername,Stringpassword){
  9. this.userName=username;
  10. this.password=password;
  11. }
  12. protectedPasswordAuthenticationgetPasswordAuthentication(){
  13. returnnewPasswordAuthentication(userName,password);
  14. }
  15. }

下面给出使用上面三个类的代码:
Java代码 收藏代码
  1. publicstaticvoidmain(String[]args){
  2. //这个类主要是设置邮件
  3. MailSenderInfomailInfo=newMailSenderInfo();
  4. mailInfo.setMailServerHost("smtp.163.com");
  5. mailInfo.setMailServerPort("25");
  6. mailInfo.setValidate(true);
  7. mailInfo.setUserName("han2000lei@163.com");
  8. mailInfo.setPassword("**********");//您的邮箱密码
  9. mailInfo.setFromAddress("han2000lei@163.com");
  10. mailInfo.setToAddress("han2000lei@163.com");
  11. mailInfo.setSubject("设置邮箱标题");
  12. mailInfo.setContent("设置邮箱内容");
  13. //这个类主要来发送邮件
  14. SimpleMailSendersms=newSimpleMailSender();
  15. sms.sendTextMail(mailInfo);//发送文体格式
  16. sms.sendHtmlMail(mailInfo);//发送html格式
  17. }

最后,给出朋友们几个注意的地方:
1、使用此代码你可以完成你的javamail的邮件发送功能。三个类缺一不可。
2、这三个类我打包是用的com.util.mail包,如果不喜欢,你可以自己改,但三个类文件必须在同一个包中
3、不要使用你刚刚注册过的邮箱在程序中发邮件,如果你的163邮箱是刚注册不久,那你就不要使用“smtp.163.com”。因为你发不出去。刚注册的邮箱是不会给你这种权限的,也就是你不能通过验证。要使用你经常用的邮箱,而且时间比较长的。
4、另一个问题就是mailInfo.setMailServerHost("smtp.163.com");与mailInfo.setFromAddress("han2000lei@163.com");这两句话。即如果你使用163smtp服务器,那么发送邮件地址就必须用163的邮箱,如果不的话,是不会发送成功的。
5、关于javamail验证错误的问题,网上的解释有很多,但我看见的只有一个。就是我的第三个类。你只要复制全了代码,我想是不会有问题的。




-------------------------------------------------------------------------------------------------华丽分割线-----------------------------------------------------------------------------------------------------------------------

老师让做的项目要用到邮箱验证,网上找了半天,这个亲测成功!原帖地址

jar包要在http://www.oracle.com/technetwork/java/javamail/index-138643.html下载!

随便附上常用邮箱:

常用的邮箱服务器(SMTP、POP3)地址、端口

sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110) SMTP服务器地址:smtp.sina.com.cn(端口:25)
sinaVIP:
POP3服务器:pop3.vip.sina.com (端口:110) SMTP服务器:smtp.vip.sina.com (端口:25)
sohu.com:
POP3服务器地址:pop3.sohu.com(端口:110) SMTP服务器地址:smtp.sohu.com(端口:25)
126邮箱:
POP3服务器地址:pop.126.com(端口:110) SMTP服务器地址:smtp.126.com(端口:25)
139邮箱:
POP3服务器地址:POP.139.com(端口:110) SMTP服务器地址:SMTP.139.com(端口:25)
163.com:
POP3服务器地址:pop.163.com(端口:110) SMTP服务器地址:smtp.163.com(端口:25)
QQ邮箱
POP3服务器地址:pop.qq.com(端口:110)
SMTP服务器地址:smtp.qq.com (端口:25)
QQ企业邮箱
POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995) SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)
yahoo.com:
POP3服务器地址:pop.mail.yahoo.com SMTP服务器地址:smtp.mail.yahoo.com
yahoo.com.cn:
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995) SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587

HotMail
POP3服务器地址:pop3.live.com (端口:995) SMTP服务器地址:smtp.live.com (端口:587)
gmail(google.com)
POP3服务器地址:pop.gmail.com(SSL启用 端口:995) SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)
263.net:
POP3服务器地址:pop3.263.net(端口:110) SMTP服务器地址:smtp.263.net(端口:25)
263.net.cn:
POP3服务器地址:pop.263.net.cn(端口:110) SMTP服务器地址:smtp.263.net.cn(端口:25)
x263.net:
POP3服务器地址:pop.x263.net(端口:110) SMTP服务器地址:smtp.x263.net(端口:25)
21cn.com:
POP3服务器地址:pop.21cn.com(端口:110) SMTP服务器地址:smtp.21cn.com(端口:25)
Foxmail:
POP3服务器地址:POP.foxmail.com(端口:110) SMTP服务器地址:SMTP.foxmail.com(端口:25)
china.com:
POP3服务器地址:pop.china.com(端口:110) SMTP服务器地址:smtp.china.com(端口:25)
tom.com:
POP3服务器地址:pop.tom.com(端口:110) SMTP服务器地址:smtp.tom.com(端口:25)
etang.com:
POP3服务器地址:pop.etang.com SMTP服务器地址:smtp.etang.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值