2013-02-18 90 views
0

你好我工作在这个代码弄清楚如何SMTP工程与Java程序,我真的很远,但我不断收到这个错误,我不知道最新的错误。这一切都是我看到的其他类似计划所做的,但他们的工作似乎有效。 它得到一路下跌到FROM行,然后打印出此错误SMTP和Java电子邮件错误

530 5.7.0 Must issue a STARTTLS command first. n1sm21348109bkv.14 - gsmtp 
Exception in thread "main" java.lang.Exception: 250 reply not received from server. 

at emailagent.EmailAgent.main(EmailAgent.java:73) 

任何帮助,将不胜感激 感谢

import java.io.*; 
import java.net.*; 
import java.util.*; 

public class EmailAgent 
{ 
public static void main(String[] args) throws Exception 
{ 

    // Establish a TCP connection with the mail server. 
    System.out.println("Enter the mail server you wish to connect to (example: pop.gmail.com):\n"); 
    String hostName = new String(); 
    Scanner emailScanner = new Scanner(System.in); 
    hostName = emailScanner.next(); 
    Socket socket = new Socket(hostName, 25); 

    // Create a BufferedReader to read a line at a time. 
    InputStream is = socket.getInputStream(); 
    InputStreamReader isr = new InputStreamReader(is); 
    BufferedReader br = new BufferedReader(isr); 

    // Read greeting from the server. 
    String response = br.readLine(); 
    System.out.println(response); 
    if (!response.startsWith("220")) 
    { 
     throw new Exception("220 reply not received from server."); 
    } 

    // Get a reference to the socket's output stream. 
    OutputStream os = socket.getOutputStream(); 

    // Send HELO command and get server response. 
    String command = "HELO alice\r\n"; 
    System.out.print(command); 
    os.write(command.getBytes("US-ASCII")); 
    response = br.readLine(); 
    System.out.println(response); 

    if (!response.startsWith("250")) 
    { 
     throw new Exception("250 reply not received from server."); 
    } 


    // Send HELO command and get server response. 
    System.out.println("Enter the name of your mail domain (example: hotmail.com):"); 
    String heloDomain = emailScanner.next(); 
    String fullHeloCommand = "HELO " + heloDomain + "\r\n"; 
    System.out.print(fullHeloCommand); 
    os.write(fullHeloCommand.getBytes("US-ASCII")); 
    response = br.readLine(); 
    System.out.println(response); 

    if (!response.startsWith("250")) 
    { 
     throw new Exception("250 reply not received from server.\n"); 
    } 

    // Send MAIL FROM command. 
    System.out.println("Please enter your e-mail address (example: [email protected]:\n"); 
    String sourceAddress = emailScanner.next(); 
    String mailFromCommand = "MAIL FROM: <" + sourceAddress + ">\r\n"; 
    System.out.println(mailFromCommand); 
    os.write(mailFromCommand.getBytes("US-ASCII")); 
    response = br.readLine(); 
    System.out.println(response); 

    if (!response.startsWith("250")) 
    { 
     throw new Exception("250 reply not received from server.\n"); 
    } 

    // Send RCPT TO command. 
    System.out.println("Please type the destination e-mail address (example: [email protected]):\n"); 
    String destEmailAddress = new String(); 
    destEmailAddress = emailScanner.next(); 
    String fullAddress = new String(); 
    fullAddress = "RCPT TO: <" + destEmailAddress + ">\r\n"; 
    System.out.println(fullAddress); 
    os.write(fullAddress.getBytes("US-ASCII")); 
    response = br.readLine(); 
    System.out.println(response); 

    if(!response.startsWith("250")) 
    { 
     throw new Exception("250 reply not received from server.\n"); 
    } 

    // Send DATA command.  
    String dataString = new String(); 
    dataString = "DATA"; 
    System.out.println(dataString); 
    os.write(dataString.getBytes("US-ASCII")); 
    response = br.readLine(); 

    if(!response.startsWith("354")) 
    { 
     throw new Exception("354 reply not received from server.\n"); 
    } 

    System.out.println(response); 


    // Send message data. 
    System.out.println("Enter your message, enter '.' on a separate line to end message data entry:\n"); 
    String input = new String(); 
    while(input.charAt(0) != '.') 
    { 
     input = emailScanner.next(); 
     os.write(input.getBytes("US-ASCII")); 
    } 
     //End with line with a single period. 
    os.write(input.getBytes("US-ASCII")); 
    response = br.readLine(); 
    System.out.println(response); 

    if(!response.startsWith("250")) 
    { 
     throw new Exception("250 reply not received from server\n"); 
    } 


    // Send QUIT command. 
    String quitCommand = new String(); 
    quitCommand = "QUIT"; 
    os.write(quitCommand.getBytes("US-ASCII")); 

    } 
    } 

回答

1

要尝试连接到邮件服务器要求您建立安全连接(TLS)以使用邮件服务。那就是你得到的错误。

就解决方案而言,我强烈建议使用JavaMail library,因为它提供了许多此功能,并且已经在“野外”进行了稳健测试。

+0

我看到了,我的目标是试图弄清楚smtp和TCP如何与语言细节进行深入研究。 – MNM 2013-02-18 21:20:26

+0

没问题,我们都去过那里。我建议使用不需要TLS或SSL的邮件服务器,因为安全握手增加了很多复杂性。随着您对协议更加熟悉,您可以转到更高级的方面。 – Perception 2013-02-18 21:26:05

+0

你能够建议一个不需要TLS或SSL的邮件服务器吗,这样我就可以感受到它的工作原理了吗? – MNM 2013-02-19 14:15:40