Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-07-2008, 09:05 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Sending Mail Using Sockets
This Java tip shows how to send email using sockets.

Code:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class SMTPDemo { public static void main(String args[]) throws IOException, UnknownHostException { String msgFile = "file.txt"; String from = "contact@java-tips.org"; String to = "yourEmail@yourServer.com"; String mailHost = "yourHost"; SMTP mail = new SMTP(mailHost); if (mail != null) { if (mail.send(new FileReader(msgFile), from, to)) { System.out.println("Mail sent."); } else { System.out.println("Connect to SMTP server failed!"); } } System.out.println("Done."); } static class SMTP { private final static int SMTP_PORT = 25; InetAddress mailHost; InetAddress localhost; BufferedReader in; PrintWriter out; public SMTP(String host) throws UnknownHostException { mailHost = InetAddress.getByName(host); localhost = InetAddress.getLocalHost(); System.out.println("mailhost = " + mailHost); System.out.println("localhost= " + localhost); System.out.println("SMTP constructor done\n"); } public boolean send(FileReader msgFileReader, String from, String to) throws IOException { Socket smtpPipe; InputStream inn; OutputStream outt; BufferedReader msg; msg = new BufferedReader(msgFileReader); smtpPipe = new Socket(mailHost, SMTP_PORT); if (smtpPipe == null) { return false; } inn = smtpPipe.getInputStream(); outt = smtpPipe.getOutputStream(); in = new BufferedReader(new InputStreamReader(inn)); out = new PrintWriter(new OutputStreamWriter(outt), true); if (inn == null || outt == null) { System.out.println("Failed to open streams to socket."); return false; } String initialID = in.readLine(); System.out.println(initialID); System.out.println("HELO " + localhost.getHostName()); out.println("HELO " + localhost.getHostName()); String welcome = in.readLine(); System.out.println(welcome); System.out.println("MAIL From:<" + from + ">"); out.println("MAIL From:<" + from + ">"); String senderOK = in.readLine(); System.out.println(senderOK); System.out.println("RCPT TO:<" + to + ">"); out.println("RCPT TO:<" + to + ">"); String recipientOK = in.readLine(); System.out.println(recipientOK); System.out.println("DATA"); out.println("DATA"); String line; while ((line = msg.readLine()) != null) { out.println(line); } System.out.println("."); out.println("."); String acceptedOK = in.readLine(); System.out.println(acceptedOK); System.out.println("QUIT"); out.println("QUIT"); return true; } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retreiving of mail body using mail number chandu.v09 JavaServer Pages (JSP) and JSTL 0 03-13-2008 03:25 PM
Help with Sockets Eric Networking 3 12-01-2007 09:09 PM
Problems sending file throught TCP sockets Nite Advanced Java 2 08-04-2007 10:01 PM
Sending a mail with the local mail program thedude Advanced Java 2 07-23-2007 01:19 PM
Sending mail Using JAVAMAIL peiceonly Advanced Java 3 07-19-2007 03:11 PM


All times are GMT +3. The time now is 07:57 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org