2011-10-09 22 views
5

私はJavaアプリケーションからGmailアカウントにメールを送信しています。私はJava Mail APIを使用していましたが、うまくいきました。しかし、javaのメールAPIを使用せずに電子メールを送信することは可能ですか?私はちょうどソケット使用して意味Gmailアカウントにメールを送信

public class Main { 
    public static void main(String[] args) throws Exception { 
    String host = "smtp.gmail.com"; 
    int port = 465; 
    String from = "[email protected]"; 
    String toAddr = "[email protected]"; 


    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; 
    } 
    } 
} 

しかし、それは動作していないが、それはメールを送信しません。誰が問題になるのか教えていただけますか?ここで

+1

最後にソケットを閉じるようにしてください、私はいくつかの時間前にこれでバグがありました。 – Sibbo

+0

エラーは何ですか? – Mob

+0

エラーは表示されませんが、メールは送信されません – Sharpzain120

答えて

2

が良い例です:

public class SMTPDemo { 

    public static void main(String args[]) throws IOException, 
     UnknownHostException { 
    String msgFile = "file.txt"; 
    String from = "[email protected]"; 
    String to = "[email protected]"; 
    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

msgfile変数の論理は何ですか? – Sharpzain120

+0

送信されるメッセージがあります。 – MasterCassim

+0

530 5.7.0最初にSTARTTLSコマンドを発行する必要があります。 ei16sm26532474wbb.21 – Sharpzain120

0

あなたは、動的IPを持っている場合は、おそらくGooglemailにメッセージを送信することができません。

0

Gmailでは、安全でないメール転送を許可していません。実装では、SSL/TLS接続を確立する必要があります。

0

デフォルトでは、Googleでは暗号化された接続のみ許可します。それは私の意見では、実際には良いことです。

メールを平文で送信する必要がある場合は、Account Pageの設定でメールを有効にすることができます。

0

このように、代わりにソケットのSSLSocketを使用し、安全な接続を使用するために:

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