打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
简单的邮件收发程序 - AJava
  1. import javax.mail.*;       
  2.      
  3.         
  4.      
  5.         
  6.      
  7. public class MailAuthenticator extends Authenticator       
  8.      
  9. {       
  10.      
  11.     //******************************       
  12.      
  13.     //由于发送邮件的地方比较多,       
  14.      
  15.     //下面统一定义用户名,口令.       
  16.      
  17.     //******************************       
  18.      
  19. //    public static String HUAWEI_MAIL_USER = "cecfhelp";       
  20.      
  21. //    public static String HUAWEI_MAIL_PASSWORD = "hEl66P666";       
  22.      
  23.     public static String HUAWEI_MAIL_USER = "lisunchang678";       
  24.     public static String HUAWEI_MAIL_PASSWORD = "lsc6312217";       
  25.      
  26. //  public static String HUAWEI_MAIL_USER = "309341935";       
  27. //      
  28. //  public static String HUAWEI_MAIL_PASSWORD = "lsc6028517";       
  29.      
  30.     public MailAuthenticator()       
  31.      
  32.     {       
  33.      
  34.     }       
  35.      
  36.         
  37.      
  38.     protected PasswordAuthentication getPasswordAuthentication()       
  39.      
  40.     {       
  41.      
  42.         return new PasswordAuthentication(HUAWEI_MAIL_USER, HUAWEI_MAIL_PASSWORD);       
  43.      
  44.     }       
  45.      
  46.         
  47.      
  48. }      
  1. package net.csdn.blog.johnston.mail;      
  2.      
  3. /**     
  4.    
  5.  * 此处插入类型说明。     
  6.    
  7.  * 创建日期:(2009-06-23)     
  8.    
  9.  * @author:johnston    
  10.    
  11.  */     
  12.      
  13. import java.util.*;      
  14.      
  15. import java.io.*;      
  16.      
  17. import javax.mail.*;      
  18.      
  19. import javax.mail.internet.*;      
  20.      
  21. import javax.activation.*;      
  22.      
  23. public class SendMail {      
  24.      
  25.     // 要发送Mail地址      
  26.      
  27.     private String mailTo = null;      
  28.      
  29.     // Mail发送的起始地址      
  30.      
  31.     private String mailFrom = null;      
  32.      
  33.     // SMTP主机地址      
  34.      
  35.     private String smtpHost = null;      
  36.      
  37.     // 是否采用调试方式      
  38.      
  39.     private boolean debug = false;      
  40.      
  41.     private String messageBasePath = null;      
  42.      
  43.     // Mail主题      
  44.      
  45.     private String subject;      
  46.      
  47.     // Mail内容      
  48.      
  49.     private String msgContent;      
  50.      
  51.     private Vector attachedFileList;      
  52.      
  53.     private String mailAccount = null;      
  54.      
  55.     private String mailPass = null;      
  56.      
  57.     private String messageContentMimeType = "text/html; charset=gb2312";      
  58.      
  59.     private String mailbccTo = null;      
  60.      
  61.     private String mailccTo = null;      
  62.      
  63.     /**    
  64.      *     
  65.      * SendMailService 构造子注解。    
  66.      *     
  67.      */     
  68.      
  69.     public SendMail() {      
  70.      
  71.         super();      
  72.      
  73.     }      
  74.      
  75.     private void fillMail(Session session, MimeMessage msg) throws IOException,      
  76.             MessagingException {      
  77.      
  78.         String fileName = null;      
  79.      
  80.         Multipart mPart = new MimeMultipart();      
  81.      
  82.         if (mailFrom != null) {      
  83.      
  84.             msg.setFrom(new InternetAddress(mailFrom));      
  85.      
  86.             System.out.println("发送人Mail地址:" + mailFrom);      
  87.      
  88.         } else {      
  89.      
  90.             System.out.println("没有指定发送人邮件地址!");      
  91.      
  92.             return;      
  93.      
  94.         }      
  95.      
  96.         if (mailTo != null) {      
  97.      
  98.             InternetAddress[] address = InternetAddress.parse(mailTo);      
  99.      
  100.             msg.setRecipients(Message.RecipientType.TO, address);      
  101.      
  102.             System.out.println("收件人Mail地址:" + mailTo);      
  103.      
  104.         } else {      
  105.      
  106.             System.out.println("没有指定收件人邮件地址!");      
  107.      
  108.             return;      
  109.      
  110.         }      
  111.      
  112.         if (mailccTo != null) {      
  113.      
  114.             InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);      
  115.      
  116.             System.out.println("CCMail地址:" + mailccTo);      
  117.      
  118.             msg.setRecipients(Message.RecipientType.CC, ccaddress);      
  119.      
  120.         }      
  121.      
  122.         if (mailbccTo != null) {      
  123.      
  124.             InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);      
  125.      
  126.             System.out.println("BCCMail地址:" + mailbccTo);      
  127.      
  128.             msg.setRecipients(Message.RecipientType.BCC, bccaddress);      
  129.      
  130.         }      
  131.      
  132.         msg.setSubject(subject);      
  133.      
  134.         InternetAddress[] replyAddress = { new InternetAddress(mailFrom) };      
  135.      
  136.         msg.setReplyTo(replyAddress);      
  137.      
  138.         // create and fill the first message part      
  139.      
  140.         MimeBodyPart mBodyContent = new MimeBodyPart();      
  141.      
  142.         if (msgContent != null)      
  143.      
  144.             mBodyContent.setContent(msgContent, messageContentMimeType);      
  145.      
  146.         else     
  147.      
  148.             mBodyContent.setContent("", messageContentMimeType);      
  149.      
  150.         mPart.addBodyPart(mBodyContent);      
  151.      
  152.         // attach the file to the message      
  153.      
  154.         if (attachedFileList != null) {      
  155.      
  156.             for (Enumeration fileList = attachedFileList.elements(); fileList      
  157.                     .hasMoreElements();) {      
  158.      
  159.                 fileName = (String) fileList.nextElement();      
  160.      
  161.                 MimeBodyPart mBodyPart = new MimeBodyPart();      
  162.      
  163.                 // attach the file to the message      
  164.      
  165.                 FileDataSource fds = new FileDataSource(messageBasePath      
  166.                         + fileName);      
  167.      
  168.                 System.out.println("Mail发送的附件为:" + messageBasePath + fileName);      
  169.      
  170.                 mBodyPart.setDataHandler(new DataHandler(fds));      
  171.      
  172.                 mBodyPart.setFileName(fileName);      
  173.      
  174.                 mPart.addBodyPart(mBodyPart);      
  175.      
  176.             }      
  177.      
  178.         }      
  179.      
  180.         msg.setContent(mPart);      
  181.      
  182.         msg.setSentDate(new Date());      
  183.      
  184.     }      
  185.      
  186.     /**    
  187.      *     
  188.      * 此处插入方法说明。    
  189.      *     
  190.      */     
  191.      
  192.     public void init()      
  193.      
  194.     {      
  195.      
  196.     }      
  197.      
  198.     /**    
  199.      *     
  200.      * 发送e_mail,返回类型为int    
  201.      *     
  202.      * 当返回值为0时,说明邮件发送成功    
  203.      *     
  204.      * 当返回值为3时,说明邮件发送失败    
  205.      *     
  206.      */     
  207.      
  208.     public int sendMail() throws IOException, MessagingException {      
  209.      
  210.         int loopCount;      
  211.      
  212.         Properties props = System.getProperties();      
  213.      
  214.         props.put("mail.smtp.host", smtpHost);      
  215.      
  216.         props.put("mail.smtp.auth""true");      
  217.      
  218.         MailAuthenticator auth = new MailAuthenticator();      
  219.      
  220.         Session session = Session.getInstance(props, auth);      
  221.      
  222.         session.setDebug(debug);      
  223.      
  224.         MimeMessage msg = new MimeMessage(session);      
  225.      
  226.         Transport trans = null;      
  227.      
  228.         try {      
  229.      
  230.             fillMail(session, msg);      
  231.      
  232.             // send the message      
  233.      
  234.             trans = session.getTransport("smtp");      
  235.      
  236.             try {      
  237.      
  238.                 trans.connect(smtpHost, MailAuthenticator.HUAWEI_MAIL_USER,      
  239.                         MailAuthenticator.HUAWEI_MAIL_PASSWORD);      
  240.                                                                       
  241.      
  242.             } catch (AuthenticationFailedException e) {      
  243.      
  244.                 e.printStackTrace();      
  245.      
  246.                 System.out.println("连接邮件服务器错误1:");      
  247.      
  248.                 return 3;      
  249.      
  250.             } catch (MessagingException e) {      
  251.                 e.printStackTrace();      
  252.                 System.out.println("连接邮件服务器错误2:");      
  253.      
  254.                 return 3;      
  255.      
  256.             }      
  257.      
  258.             trans.send(msg);      
  259.      
  260.             trans.close();      
  261.      
  262.         } catch (MessagingException mex) {      
  263.      
  264.             System.out.println("发送邮件失败:");      
  265.      
  266.             mex.printStackTrace();      
  267.      
  268.             Exception ex = null;      
  269.      
  270.             if ((ex = mex.getNextException()) != null) {      
  271.      
  272.                 System.out.println(ex.toString());      
  273.      
  274.                 ex.printStackTrace();      
  275.      
  276.             }      
  277.      
  278.             return 3;      
  279.      
  280.         } finally {      
  281.      
  282.             try {      
  283.      
  284.                 if (trans != null && trans.isConnected())      
  285.      
  286.                     trans.close();      
  287.      
  288.             } catch (Exception e) {      
  289.      
  290.                 System.out.println(e.toString());      
  291.      
  292.             }      
  293.      
  294.         }      
  295.      
  296.         System.out.println("发送邮件成功!");      
  297.      
  298.         return 0;      
  299.      
  300.     }      
  301.      
  302.     public void setAttachedFileList(java.util.Vector filelist)      
  303.      
  304.     {      
  305.      
  306.         attachedFileList = filelist;      
  307.      
  308.     }      
  309.      
  310.     public void setDebug(boolean debugFlag)      
  311.      
  312.     {      
  313.      
  314.         debug = debugFlag;      
  315.      
  316.     }      
  317.      
  318.     public void setMailAccount(String strAccount) {      
  319.      
  320.         mailAccount = strAccount;      
  321.      
  322.     }      
  323.      
  324.     public void setMailbccTo(String bccto) {      
  325.      
  326.         mailbccTo = bccto;      
  327.      
  328.     }      
  329.      
  330.     public void setMailccTo(String ccto) {      
  331.      
  332.         mailccTo = ccto;      
  333.      
  334.     }      
  335.      
  336.     public void setMailFrom(String from)      
  337.      
  338.     {      
  339.      
  340.         mailFrom = from;      
  341.      
  342.     }      
  343.      
  344.     public void setMailPass(String strMailPass) {      
  345.      
  346.         mailPass = strMailPass;      
  347.      
  348.     }      
  349.      
  350.     public void setMailTo(String to)      
  351.      
  352.     {      
  353.      
  354.         mailTo = to;      
  355.      
  356.     }      
  357.      
  358.     public void setMessageBasePath(String basePath)      
  359.      
  360.     {      
  361.      
  362.         messageBasePath = basePath;      
  363.      
  364.     }      
  365.      
  366.     public void setMessageContentMimeType(String mimeType)      
  367.      
  368.     {      
  369.      
  370.         messageContentMimeType = mimeType;      
  371.      
  372.     }      
  373.      
  374.     public void setMsgContent(String content)      
  375.      
  376.     {      
  377.      
  378.         msgContent = content;      
  379.      
  380.     }      
  381.      
  382.     public void setSMTPHost(String host)      
  383.      
  384.     {      
  385.      
  386.         smtpHost = host;      
  387.      
  388.     }      
  389.      
  390.     public void setSubject(String sub)      
  391.      
  392.     {      
  393.      
  394.         subject = sub;      
  395.      
  396.     }      
  397.      
  398.     public static void main(String[] argv) throws Exception      
  399.      
  400.     {      
  401.      
  402.         for (int i = 0; i < 10; i++) {      
  403.      
  404.             SendMail sm = new SendMail();      
  405.      
  406. //          sm.setSMTPHost("mail.ec.com.cn");      
  407. //          sm.setMailFrom("cecfhelp@ec.com.cn");      
  408.             //sm.setMailTo("309341935@qq.com");               
  409.                   
  410.                   
  411.             sm.setSMTPHost("smtp.163.com");      
  412.             sm.setMailFrom("lisunchang678@163.com");                  
  413.             sm.setMailTo("johnston678@163.com");      
  414.      
  415. //          sm.setSMTPHost("smtp.qq.com");      
  416. //          sm.setMailFrom("309341935@qq.com");      
  417. //          sm.setMailTo("495794927@qq.com");      
  418.                   
  419.             sm.setMsgContent("邮件测试程序" + i);      
  420.      
  421.             sm.setSubject("邮件测试程序" + i);      
  422.      
  423.             sm.sendMail();      
  424.                   
  425.             Thread t = new Thread();      
  426.             t.sleep(10000);      
  427.             System.out.println("count:" + i);      
  428.      
  429.         }      
  430.      
  431.     }      
  432.      
  433. }     
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JavaMail 深入浅出
利用JavaMail收/发Gmail邮件SSL
Java发邮件 实现步骤+代码
JAVA MAIL 实例
用JavaMail API编写可带附件的邮件发送程序
简单聊天程序java socket
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服