2012-09-22 173 views
0

如何使用Gmail的服务器发送包含附件但没有JavaMail的电子邮件?Gmail SMTP身份验证

我碰到过这个code

如何使用此代码进行身份验证(使用我的Gmail帐户作为发件人)?该代码是否可以通过可以指定主题的方式进行编辑?我猜file.txt充当附件?

编辑:这里的代码从链接,适应我的需要(或多或少)。唯一不同的是文件位置,smtp服务器和“from”和“to”字符串。

public static void blabla() throws IOException, UnknownHostException { 
     String msgFile = "C:\\file.txt"; 
     String from = "[email protected]"; 
     String to = "[email protected]"; 
     String mailHost = "smtp.gmail.com"; 
     SMTP mail = new SMTP(mailHost); 
     if (mail != null) { 
      if (mail.send(new FileReader(msgFile), from, to)) { 
       System.out.println("Mail sent."); 
      } else { 
       System.out.println("Connect to SMTP server failed!"); 
      } 
     } 
     System.out.println("Done."); 
    } 



    static class SMTP { 
     private final static int SMTP_PORT = 25; 

     InetAddress mailHost; 

     InetAddress localhost; 

     BufferedReader in; 

     PrintWriter out; 

     public SMTP(String host) throws UnknownHostException { 
      mailHost = InetAddress.getByName(host); 
      localhost = InetAddress.getLocalHost(); 
      System.out.println("mailhost = " + mailHost); 
      System.out.println("localhost= " + localhost); 
      System.out.println("SMTP constructor done\n"); 
     } 

     @SuppressWarnings({"resource", "unused"}) 
     public boolean send(FileReader msgFileReader, String from, String to) 
       throws IOException { 
      Socket smtpPipe; 
      InputStream inn; 
      OutputStream outt; 
      BufferedReader msg; 
      msg = new BufferedReader(msgFileReader); 
      smtpPipe = new Socket(mailHost, SMTP_PORT); 
      if (smtpPipe == null) { 
       return false; 
      } 
      inn = smtpPipe.getInputStream(); 
      outt = smtpPipe.getOutputStream(); 
      in = new BufferedReader(new InputStreamReader(inn)); 
      out = new PrintWriter(new OutputStreamWriter(outt), true); 
      if (inn == null || outt == null) { 
       System.out.println("Failed to open streams to socket."); 
       return false; 
      } 

      String initialID = in.readLine(); 

      System.out.println(initialID); 
      System.out.println("HELO " + localhost.getHostName()); 
      out.println("HELO " + localhost.getHostName()); 

      String welcome = in.readLine(); 

      System.out.println(welcome); 
      System.out.println("MAIL From:<" + from + ">"); 
      out.println("MAIL From:<" + from + ">"); 

      String senderOK = in.readLine(); 

      System.out.println(senderOK); 
      System.out.println("RCPT TO:<" + to + ">"); 
      out.println("RCPT TO:<" + to + ">"); 

      String recipientOK = in.readLine(); 

      System.out.println(recipientOK); 
      System.out.println("DATA"); 
      out.println("DATA"); 

      String line; 
      while ((line = msg.readLine()) != null) { 
       out.println(line); 
      } 

      System.out.println("."); 
      out.println("."); 

      String acceptedOK = in.readLine(); 

      System.out.println(acceptedOK); 
      System.out.println("QUIT"); 
      out.println("QUIT"); 

      return true; 
     } 
+0

我已经链接到该代码。我已经适应了我的需要,只有SMTP认证失败...我可以在这里复制它。 –

回答

0
+0

我明确表示没有JavaMail。 –

+0

是的,但他是最重要的,他们可能会为您提供一些想法。我的第一个想法是SSL,接下来是TLS。我的第一个提示是,端口不是25.读一读,它可能只是指出你在正确的方向 – MadProgrammer

+0

你也可以看看[SMTP客户端SSL/TLS](http://www.codeproject.com/文章/ 98355/SMTP客户端与 - SSL-TLS)。它是用C/C++编写的,但是这个决定可能值得一读 – MadProgrammer