send mail to Gmail account

后端 未结 5 1499
無奈伤痛
無奈伤痛 2020-12-17 03:03

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

相关标签:
5条回答
  • 2020-12-17 03:06

    By default, Google only allows encrypted connections. That's actually a good thing, in my opinion.

    But if you must send your mails in plaintext, you can enable it on your Account Page under settings.

    0 讨论(0)
  • 2020-12-17 03:09

    In order to use a secure connection, use SSLSocket instead of Socket, like this:

    SSLSocket socket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(InetAddress.getByName("smtp.gmail.com"), 465);
    
    0 讨论(0)
  • 2020-12-17 03:11

    If you have a dynamic IP you are probably not able to send messages to Googlemail.

    0 讨论(0)
  • 2020-12-17 03:23

    GMail doesn't allow non-secure mail transfer. You need to make an SSL/TLS connection in your implementation.

    0 讨论(0)
  • 2020-12-17 03:31

    Here is a good example:

    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

    0 讨论(0)
提交回复
热议问题