Pessoal,
Estou com um projeto na disciplina de Redes para criar um servidor de E-mail implementando os protocolos de conexão (sem uso de API’s)
Não conheço muito bem o uso de sockets em Java por isso pedi ajuda a alguns amigos, um deles me mandou esse exemplo:
package smtp.exemplo;
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class SmtpSender {
private Socket smtpSocket = null;
private DataOutputStream os = null;
private DataInputStream is = null;
private boolean podeEnviar=false;
Date dDate = new Date();
DateFormat dFormat = DateFormat.getDateInstance(DateFormat.FULL,Locale.US);
public SmtpSender(String m_sHostName, int m_iPort){
try
{ // Open port to server
smtpSocket = new Socket(m_sHostName, m_iPort);
os = new DataOutputStream(smtpSocket.getOutputStream());
is = new DataInputStream(smtpSocket.getInputStream());
if(smtpSocket != null && os != null && is != null)
{ // Connection was made. Socket is ready for use.
podeEnviar = true;
}
}
catch(Exception e)
{ System.out.println("Host " + m_sHostName + "unknown"); }
}
public boolean Enviar(String From, String To, String CC, String Subject, String sMessage){
if(!podeEnviar)
return false;
try
{ os.writeBytes("HELO\r\n");
// You will add the email address that the server
// you are using know you as.
os.writeBytes("MAIL From: " + From + "\r\n");
// Who the email is going to.
os.writeBytes("RCPT To:"+ To +"\r\n");
//IF you want to send a CC then you will have to add this
os.writeBytes("RCPT Cc:" + CC + "\r\n");
// Now we are ready to add the message and the
// header of the email to be sent out.
os.writeBytes("DATA\r\n");
os.writeBytes("X-Mailer: Via Java\r\n");
os.writeBytes("DATE: " + dFormat.format(dDate) + "\r\n");
os.writeBytes("From: " + From + "\r\n");
os.writeBytes("To: " + To + "\r\n");
//Again if you want to send a CC then add this.
//os.writeBytes("Cc: CCDUDE <[email removido]>\r\n");
//Here you can now add a BCC to the message as well
//os.writeBytes("RCPT Bcc: BCCDude<[email removido]>\r\n");
//sMessage = "Your subjectline.";
os.writeBytes("Subject: " + Subject + "\r\n");
os.writeBytes(sMessage + "\r\n");
os.writeBytes("\r\n.\r\n");
os.writeBytes("QUIT\r\n");
// Now send the email off and check the server reply.
// Was an OK is reached you are complete.
String responseline;
while((responseline = is.readLine())!=null)
{ System.out.println(responseline);
if(responseline.indexOf("Ok") != -1)
break;
}
return true;
}
catch(Exception e)
{
System.out.println("Cannot send email as an error occurred.");
return false;
}
}
}
Parecia ser exatamente o necessário para eu me basear e apenas modificar poucas coisas, porém não estou conseguindo realizar a conexão pois não vejo forma de autenticação.
O objetivo é através de um socket enviar uma mensagem para algum servidor de e-mail real (gmail por ex.), não sendo necessário receber retorno.
Alguém poderia por favor me dar alguma luz?
Obrigado.
