Email afsender som har virket
Jeg har et eksempel taget direkte fra en java bog som jeg har haft til at virke men nu gør det pludselig ikke mere og jeg aner ikke hvad som skulle have ændret sig.Programmet ser ud som følger:
import java.io.*;
import java.net.*;
import java.util.*;
public class SMTPClientDemo
{
protected int port = 25;
protected String hostname = "localhost";
protected String from = "";
protected String to = "";
protected String subject = "";
protected String body = "";
protected Socket socket;
protected BufferedReader br;
protected PrintWriter pw;
public SMTPClientDemo() throws Exception
{
try
{
getInput();
sendEmail();
}
catch(Exception e)
{
System.out.println("Error sending message - " + e);
}
}
public static void main(String []args) throws Exception
{
SMTPClientDemo client = new SMTPClientDemo();
}
protected int readResponseCode() throws Exception
{
String line = br.readLine();
System.out.println("> " + line);
line = line.substring(0, line.indexOf(" "));
return Integer.parseInt(line);
}
protected void writeMsg(String msg) throws Exception
{
pw.println(msg);
pw.flush();
System.out.println("> " + msg);
}
protected void closeConnection() throws Exception
{
pw.flush();
pw.close();
br.close();
socket.close();
}
protected void sendQuit() throws Exception
{
System.out.println("Sending QUIT");
writeMsg("QUIT");
readResponseCode();
System.out.println("Closing Connection");
closeConnection();
}
protected void sendEmail() throws Exception
{
System.out.println("Sender besked");
System.out.println("------------------------------");
System.out.println("Opening Socket");
socket = new Socket(this.hostname, this.port);
System.out.println("Creating Reader & Writer");
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("Læser første linie...");
int code = readResponseCode();
if(code != 220)
{
socket.close();
throw new Exception("Invalid SMTP server");
}
System.out.println("Sending HELO command");
writeMsg("HELO " + InetAddress.getLocalHost().getHostName());
code = readResponseCode();
if(code != 250)
{
sendQuit();
throw new Exception("Invalid SMTP server");
}
System.out.println("Sending mail from command");
writeMsg("MAIL FROM:<" + this.from + ">");
code = readResponseCode();
if(code != 250)
{
sendQuit();
throw new Exception("Invalid SMTP server");
}
System.out.println("Sending rcpt to command");
writeMsg("RCPT TO:<" + this.to + ">");
code = readResponseCode();
if(code != 250)
{
sendQuit();
throw new Exception("Invalid SMTP server");
}
System.out.println("Sending data command");
writeMsg("DATA");
code = readResponseCode();
if(code != 354)
{
sendQuit();
throw new Exception("Data entry not accepted");
}
System.out.println("Sending message");
writeMsg("Subject: " + this.subject);
writeMsg("To: " + this.to);
writeMsg("FROM: " + this.from);
writeMsg("");
writeMsg(body);
code = readResponseCode();
sendQuit();
if(code != 250)
throw new Exception("Beskeden er ikke nået korrekt frem");
else
System.out.println("Beskeden er sendt");
}
protected void getInput() throws Exception
{
String data = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter SMTP server hostname: ");
data = br.readLine();
if(data == null || data.equals(""))
hostname = "localhost";
else
hostname = data;
System.out.println("Please enter FROM email address: ");
data = br.readLine();
from = data;
System.out.println("Please enter TO email address: ");
data = br.readLine();
if(!(data == null || data.equals("")))
to = data;
System.out.println("Please enter subject: ");
data = br.readLine();
subject = data;
System.out.println("Please enter plain-text message ('.' character on a blank line signals end of message):");
StringBuffer buffer = new StringBuffer();
String line = br.readLine();
while(line != null)
{
if(line.equalsIgnoreCase("."))
{
break;
}
buffer.append(line);
buffer.append("\n");
line = br.readLine();
}
buffer.append(".\n");
body = buffer.toString();
}
}
