【问题标题】:send mail to Gmail account向 Gmail 帐户发送邮件
【发布时间】:2011-10-09 11:03:16
【问题描述】:

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

我的意思是使用套接字:

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\r\n");
      os.writeBytes("MAIL From:" + from + " \r\n");
      os.writeBytes("RCPT To:" + toAddr + "\r\n");
      os.writeBytes("DATA\r\n");
      os.writeBytes("X-Mailer: Java\r\n");
      os.writeBytes("DATE: " + DateFormat.getDateInstance(DateFormat.FULL, 
                                   Locale.US).format(new Date()) + "\r\n");
      os.writeBytes("From:" + from + "\r\n");
      os.writeBytes("To:" + toAddr + "\r\n");
    }

    os.writeBytes("Subject:\r\n");
    os.writeBytes("body\r\n");
    os.writeBytes("\r\n.\r\n");
    os.writeBytes("QUIT\r\n");
    String responseline;
    while ((responseline = is.readUTF()) != null) { 
      if (responseline.indexOf("Ok") != -1)
        break;
    }
  }
}

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

【问题讨论】:

  • 最后尝试关闭套接字,我前段时间遇到过这个问题。
  • 它没有显示任何错误但邮件没有被转发
  • 530 5.7.0 必须先发出 STARTTLS 命令。 ei16sm26532474wbb.21
  • 请尝试通过此处的答案解决问题,而不是立即添加另一个问题。

标签: java http sockets smtp gmail


【解决方案1】:

这是一个很好的例子:

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\n");
    }

    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/Network-Protocol/SendingMailUsingSockets.htm

【讨论】:

  • msgfile变量的逻辑是什么?
  • 找到了要发送的消息。
  • 530 5.7.0 必须先发出 STARTTLS 命令。 ei16sm26532474wbb.21
  • 看起来谷歌在你需要 STARTTLS 的地方添加了一些限制,但是我在任何地方都找不到这方面的任何信息。您现在几乎必须使用JavaMail...我今天正在尝试使用套接字来做同样的事情,但无法弄清楚。
【解决方案2】:

如果您有动态 IP,您可能无法向 Googlemail 发送邮件。

【讨论】:

    【解决方案3】:

    GMail 不允许不安全的邮件传输。您需要在实现中建立 SSL/TLS 连接。

    【讨论】:

      【解决方案4】:

      默认情况下,Google 只允许加密连接。在我看来,这实际上是一件好事。

      但如果您必须以纯文本形式发送邮件,您可以在设置下的Account Page 上启用它。

      【讨论】:

        【解决方案5】:

        为了使用安全连接,请使用 SSLSocket 而不是 Socket,如下所示:

        SSLSocket socket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(InetAddress.getByName("smtp.gmail.com"), 465);
        

        【讨论】:

          猜你喜欢
          • 2011-12-15
          • 1970-01-01
          • 2016-03-15
          • 2021-10-22
          • 1970-01-01
          • 1970-01-01
          • 2013-03-24
          • 2012-03-30
          • 1970-01-01
          相关资源
          最近更新 更多