Home arrow static arrow Java Programming [Archive] - sending email
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - sending email
This topic has 2 replies on 1 page.

Posts:64
Registered: 12/23/03
sending email  
Aug 3, 2004 11:56 PM



 
i want to send email from my java
in php i do this simple code :
[CODE]
ini_set("SMTP","192.168.4.200");
ini_set("smtp_port","25");
ini_set("sendmail_from","best@email.com");
mail("who@ever.com", "problem on doors :", "error : ".$msg);
[/CODE]

how cam i do the same in java?
thnaks in advance
peleg
 

Posts:13
Registered: 8/3/04
Re: sending email  
Aug 4, 2004 1:50 AM (reply 1 of 2)



 
http://www.javaworld.com/javaworld/jw-10-2001/jw-1026-javamail.html
 

Posts:26
Registered: 7/18/04
Re: sending email  
Aug 4, 2004 2:08 AM (reply 2 of 2)



 
Heres a quick fix for you.


/****************************************************************
* This program connects to an email server and sends an email *
* it requires the computer upon which it is running to be *
* connected to the internet and will NOT connect the computer *
* to the internet. *
****************************************************************
*/

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

public class SendMail {

private static final String mailHost = "mail.freeserve.com";
private static final String from = "name@domain.com";
private static final String to = "person@hotmail.com";
private static final String subj = "Subject of email";
private static final String message = "This is an email message,\nsent via a Java program!";

public static void main(String[] args){
try {
System.getProperties().put("mail.host",mailHost);

System.out.println("Connecting to network.");

//Establish a network connection for sending email
URL u = new URL("mailto:"+to);
URLConnection c = u.openConnection();
System.out.println("URLConnection opened.");
c.setDoInput(false);
c.setDoOutput(true);
c.connect(); //connect to mail host

System.out.println("Connected to mail host.");

System.out.println("Preparing to send email.");
PrintWriter out = new PrintWriter(new OutputStreamWriter(c.getOutputStream()));
out.print(from + "\n");
out.print(to + "\n");
out.print(subj + "\n\n");
out.print(message);
out.close();

System.out.println("Email sent!");
}
catch( UnknownHostException uhe ) {
System.out.println("Unkown mailhost "mailHost" or unable to connect to mailhost!");
System.exit(1);
}
catch( Exception e ){
System.err.println("Error: "+e.toString() );
System.exit(1);
}
}//main method

}//End of SendMail class

 
This topic has 2 replies on 1 page.