当前位置: 首页 > 面试题库 >

发送邮件到Gmail帐户

孙斌
2023-03-14
问题内容

我正在从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;
    }
  }
}

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


问题答案:

这是一个很好的例子:

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



 类似资料:
  • 我正在开发一个WordPress插件,其中我使用PHP邮件功能发送电子邮件,但是邮件功能有一个问题。它向我的非Gmail帐户发送电子邮件,但它不会向我的Gmail帐户发送电子邮件。我正在使用以下代码: 是我的代码有问题,还是邮件功能有问题?如果有其他方法可以发送电子邮件,请给我链接。

  • 当我尝试通过运行Laravel 4的网站发送电子邮件时,我会收到这个异常: 预期响应代码为250,但收到代码“535”,消息“535-5.7.8用户名和密码不被接受。有关详细信息,请访问535 5.7.8http://support.google.com/mail/bin/answer.py?answer=14257wd7sm12843789wjc。36-gsmtp“ 这是我的邮件配置: 我第一次

  • 我有这个发邮件的密码 此代码为除之外的任何邮件提供商发送邮件。 这可能是什么原因

  • 我有发送邮件与谷歌SMTP和最新版本的PHPMailer的问题。这里有很多问题,但是答案对我不起作用。我使用免费托管与免费子域。这是我的标准PHP代码发送电子邮件从文档: 我得到了这个错误: .... 2017-06-03 15:15:33服务器- 如果将与端口一起使用,我会看到相同的错误。 在我的电子邮件帐户2中,工厂身份验证处于和处于。 我真的不知道该怎么办。

  • 我用竹子来让我的网站访问者填写并发送一份联系表。 我创建了一个服务帐户 我授予它全域权限 我添加了以下范围:https://www.googleapis.com/auth/gmail.addons.current.action.compose https://www.googleapis.com/auth/gmail.addons.current.message.metadatahttps://w

  • 问题 如何用Gmail发送邮件? 解法 安装和维护邮件服务器通常是沉闷乏味的。所以如果你有Gmail帐号,就可以使用Gmail做为SMTP服务器来发送邮件,我们唯一要做的就只是在web.config中指定Gmail的用户名和密码。 web.config.smtp_server = 'smtp.gmail.com' web.config.smtp_port = 587 web.config.smtp