分享

java发送邮件的方法

 昵称22326956 2015-06-16

1使用spring的邮件服务
/** 
  *用spring mail 发送邮件,依赖jar:spring.jar,activation.jar,mail.jar  
  */     
  public static void sendFileMail() throws MessagingException {  
          JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  
    
          // 设定mail server  
          senderImpl.setHost("smtp.126.com");  
          senderImpl.setUsername("yuhan0");  
          senderImpl.setPassword("******");  
          // 建立HTML邮件消息  
          MimeMessage mailMessage = senderImpl.createMimeMessage();  
          // true表示开始附件模式  
          MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");  
    
          // 设置收件人,寄件人  
          messageHelper.setTo("slimes@126.com");  
          messageHelper.setFrom("yuhan0@126.com");  
          messageHelper.setSubject("测试邮件!");  
          // true 表示启动HTML格式的邮件  
          messageHelper.setText("<html><head></head><body><h1>你好:附件!!</h1></body></html>", true);  
    
          FileSystemResource file1 = new FileSystemResource(new File("d:/logo.jpg"));  
          FileSystemResource file2 = new FileSystemResource(new File("d:/读书.txt"));  
          // 添加2个附件  
          messageHelper.addAttachment("logo.jpg", file1);  
            
          try {  
              //附件名有中文可能出现乱码  
              messageHelper.addAttachment(MimeUtility.encodeWord("读书.txt"), file2);  
          } catch (UnsupportedEncodingException e) {  
              e.printStackTrace();  
              throw new MessagingException();  
          }  
          // 发送邮件  
          senderImpl.send(mailMessage);  
          System.out.println("邮件发送成功.....");  
    
      }




代码2:使用apache commons-email 发送邮件


/** 
  *用apache commons-email 发送邮件 
  *依赖jar:commons-email.jar,activation.jar,mail.jar 
  */  
  public static void sendMutiMessage() {  
    
          MultiPartEmail email = new MultiPartEmail();  
          String[] multiPaths = new String[] { "D:/1.jpg", "D:/2.txt" };  
    
          List<EmailAttachment> list = new ArrayList<EmailAttachment>();  
          for (int j = 0; j < multiPaths.length; j++) {  
              EmailAttachment attachment = new EmailAttachment();  
              //判断当前这个文件路径是否在本地  如果是:setPath  否则  setURL;   
              if (multiPaths[j].indexOf("http") == -1) {  
                  attachment.setPath(multiPaths[j]);  
              } else {  
                  try {  
                      attachment.setURL(new URL(multiPaths[j]));  
                 } catch (MalformedURLException e) {  
                      e.printStackTrace();  
                }  
              }  
              attachment.setDisposition(EmailAttachment.ATTACHMENT);  
              attachment.setDescription("Picture of John");  
              list.add(attachment);  
          }  
    
          try {  
              // 这里是发送服务器的名字:  
              email.setHostName("smtp.126.com");  
              // 编码集的设置  
              email.setCharset("utf-8");  
              // 收件人的邮箱                 
              email.addTo("slimes@126.com");  
              // 发送人的邮箱  
              email.setFrom("yuhan0@126.com");  
              // 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码  
              email.setAuthentication("yuhan0", "******");  
              email.setSubject("这是一封测试邮件");  
              // 要发送的信息  
              email.setMsg("<b><a href=\"http://www.baidu.com\">邮件测试内容</a></b>");  
    
              for (int a = 0; a < list.size(); a++) //添加多个附件     
              {  
                  email.attach(list.get(a));  
              }  
              // 发送  
              email.send();  
          } catch (EmailException e) {  
              e.printStackTrace();  
          }  
      }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多