<small id='qmwnR'></small><noframes id='qmwnR'>

    <legend id='qmwnR'><style id='qmwnR'><dir id='qmwnR'><q id='qmwnR'></q></dir></style></legend>

      <bdo id='qmwnR'></bdo><ul id='qmwnR'></ul>
  • <i id='qmwnR'><tr id='qmwnR'><dt id='qmwnR'><q id='qmwnR'><span id='qmwnR'><b id='qmwnR'><form id='qmwnR'><ins id='qmwnR'></ins><ul id='qmwnR'></ul><sub id='qmwnR'></sub></form><legend id='qmwnR'></legend><bdo id='qmwnR'><pre id='qmwnR'><center id='qmwnR'></center></pre></bdo></b><th id='qmwnR'></th></span></q></dt></tr></i><div id='qmwnR'><tfoot id='qmwnR'></tfoot><dl id='qmwnR'><fieldset id='qmwnR'></fieldset></dl></div>

        <tfoot id='qmwnR'></tfoot>

        向 Gmail 帐户发送邮件

        send mail to Gmail account(向 Gmail 帐户发送邮件)
        <legend id='XTnoa'><style id='XTnoa'><dir id='XTnoa'><q id='XTnoa'></q></dir></style></legend>
      1. <i id='XTnoa'><tr id='XTnoa'><dt id='XTnoa'><q id='XTnoa'><span id='XTnoa'><b id='XTnoa'><form id='XTnoa'><ins id='XTnoa'></ins><ul id='XTnoa'></ul><sub id='XTnoa'></sub></form><legend id='XTnoa'></legend><bdo id='XTnoa'><pre id='XTnoa'><center id='XTnoa'></center></pre></bdo></b><th id='XTnoa'></th></span></q></dt></tr></i><div id='XTnoa'><tfoot id='XTnoa'></tfoot><dl id='XTnoa'><fieldset id='XTnoa'></fieldset></dl></div>

          <tbody id='XTnoa'></tbody>

          <small id='XTnoa'></small><noframes id='XTnoa'>

            <bdo id='XTnoa'></bdo><ul id='XTnoa'></ul>

            <tfoot id='XTnoa'></tfoot>

                • 本文介绍了向 Gmail 帐户发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在从我的 Java 应用程序向 Gmail 帐户发送邮件.我使用了 Java Mail API,它运行良好.但是是否可以不使用 java 中的邮件 API 来发送电子邮件?

                  I am sending mail from my Java app to Gmail Account. I had used the Java Mail API and it worked fine. But is it possible to send an e-mail without using the mail API in java?

                  我的意思是使用套接字:

                  I mean just by using sockets:

                  public class Main {
                    public static void main(String[] args) throws Exception {
                      String host = "smtp.gmail.com";
                      int port = 465;
                      String from = "sh2rpzain@gmail.com";
                      String toAddr = "sharpzian@gmail.com";
                  
                  
                      Socket servSocket = new Socket(host, port);
                      DataOutputStream os = new DataOutputStream(servSocket.getOutputStream());
                      DataInputStream is = new DataInputStream(servSocket.getInputStream());
                  
                      if (servSocket != null && os != null && is != null) {
                        os.writeBytes("HELO
                  ");
                        os.writeBytes("MAIL From:" + from + " 
                  ");
                        os.writeBytes("RCPT To:" + toAddr + "
                  ");
                        os.writeBytes("DATA
                  ");
                        os.writeBytes("X-Mailer: Java
                  ");
                        os.writeBytes("DATE: " + DateFormat.getDateInstance(DateFormat.FULL, 
                                                     Locale.US).format(new Date()) + "
                  ");
                        os.writeBytes("From:" + from + "
                  ");
                        os.writeBytes("To:" + toAddr + "
                  ");
                      }
                  
                      os.writeBytes("Subject:
                  ");
                      os.writeBytes("body
                  ");
                      os.writeBytes("
                  .
                  ");
                      os.writeBytes("QUIT
                  ");
                      String responseline;
                      while ((responseline = is.readUTF()) != null) { 
                        if (responseline.indexOf("Ok") != -1)
                          break;
                      }
                    }
                  }
                  

                  但它不工作,它不发送邮件.谁能告诉我可能是什么问题?

                  But it is not working, it doesn't send out the mail. Can anyone tell me what could be the problem?

                  推荐答案

                  这是一个很好的例子:

                  public class SMTPDemo {
                  
                    public static void main(String args[]) throws IOException,
                        UnknownHostException {
                      String msgFile = "file.txt";
                      String from = "java2s@java2s.com";
                      String to = "yourEmail@yourServer.com";
                      String mailHost = "yourHost";
                      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
                  ");
                      }
                  
                      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;
                      }
                    }
                  }
                  

                  -> http://www.java2s.com/Code/Java/网络协议/SendingMailUsingSockets.htm

                  这篇关于向 Gmail 帐户发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)

                      <i id='tGzdF'><tr id='tGzdF'><dt id='tGzdF'><q id='tGzdF'><span id='tGzdF'><b id='tGzdF'><form id='tGzdF'><ins id='tGzdF'></ins><ul id='tGzdF'></ul><sub id='tGzdF'></sub></form><legend id='tGzdF'></legend><bdo id='tGzdF'><pre id='tGzdF'><center id='tGzdF'></center></pre></bdo></b><th id='tGzdF'></th></span></q></dt></tr></i><div id='tGzdF'><tfoot id='tGzdF'></tfoot><dl id='tGzdF'><fieldset id='tGzdF'></fieldset></dl></div>

                        <tbody id='tGzdF'></tbody>
                    1. <small id='tGzdF'></small><noframes id='tGzdF'>

                        • <bdo id='tGzdF'></bdo><ul id='tGzdF'></ul>
                          <tfoot id='tGzdF'></tfoot>
                          • <legend id='tGzdF'><style id='tGzdF'><dir id='tGzdF'><q id='tGzdF'></q></dir></style></legend>