Reputation: 44862
I've got something like the following...
public boolean sendmail (String host, String to, String from,
String message, String subject, String cc){
try
{
//Created TCP Connection to server
Socket s = new Socket(host, 25);
//Open our streams.
InputStream inStream = s.getInputStream();
OutputStream outStream = s.getOutputStream();
in = new Scanner(inStream);
out = new PrintWriter(outStream, true );
//get my info!
String hostName = InetAddress.getLocalHost().getHostName();
//e-mail time!
receive();
send("HELO" + host);
receive();
send("MAIL FROM: <" + from + "\n");
receive();
send("RCPT TO: <" + to + "\n");
receive();
send("DATA");
receive();
send("DATA");
receive();
//Make sure to close the everything again!!
out.close();
in.close();
s.close();
return true;
}
catch (Exception e)
{
appendtolog("ERROR: " + e);
return false;
}
}
private void send(String s) throws IOException{
appendtolog(s);
out.print(s.replaceAll("\n", "\r\n"));
out.print("\r\n");
out.flush();
}
private void receive(){
String line = in.nextLine();
appendtolog(line);
}
is it possible to just put an attachment somewhere in there? I realise there is ways of doing this using the API more but I'm wondering there a way I can hammer functionality for attachments into that or is using something like..
// Set the email attachment file
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(filename) {
@Override
public String getContentType() {
return "application/octet-stream";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(filename);
Upvotes: 1
Views: 209
Reputation: 9777
I would recommend going back to basics and read the RFCs on this.
Internet Message Format http://www.faqs.org/rfcs/rfc2822.html
Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies http://www.faqs.org/rfcs/rfc2045.html
These are more or less straight forward, there is some arcane stuff in there but you should be able to suss out what you need.
I would suggest trying to separate the communication (which you've got above) with the body (message content) as much as possible, else you'll muddy the waters.
Hope that helps.
Upvotes: 2
Reputation: 115388
It is obviously possible, but I'd suggest you to use JavaMail API instead of dealing with gory details of SMTP protocol. It is correct unless you are implementing a student exercise.
Upvotes: 1