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

Posts:474
Registered: 3/12/04
Email Code  
Jul 14, 2004 11:02 AM



 
Hi,
This is the code for sending and recieving mails through SMTP server.
Problem is that how i should bring it into real action.I have a stand alone pc.
I connect using a dialup connection and my motive is to be able to send/recieve
to any personal id of my choice could be yahoo/hotmail etc.
Kindly elaborate in steps,i will pleased to listen to this response .
It is copied from book of Advanced Core java and works ok.But i dont know what SMTP server,
From: and To: to state

/**
@version 1.01 2001-06-26
@author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

/**
This program shows how to use sockets to send plain text
mail messages.
*/
public class MailTest
{
public static void main(String[] args)
{
JFrame frame = new MailTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

/**
The frame for the mail GUI.
*/
class MailTestFrame extends JFrame
{
public MailTestFrame()
{
setSize(WIDTH, HEIGHT);
setTitle("MailTest");

getContentPane().setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
gbc.weighty = 0;

gbc.weightx = 0;
add(new JLabel("From:"), gbc, 0, 0, 1, 1);
gbc.weightx = 100;
from = new JTextField(20);
add(from, gbc, 1, 0, 1, 1);

gbc.weightx = 0;
add(new JLabel("To:"), gbc, 0, 1, 1, 1);
gbc.weightx = 100;
to = new JTextField(20);
add(to, gbc, 1, 1, 1, 1);

gbc.weightx = 0;
add(new JLabel("SMTP server:"), gbc, 0, 2, 1, 1);
gbc.weightx = 100;
smtpServer = new JTextField(20);
add(smtpServer, gbc, 1, 2, 1, 1);

gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 100;
message = new JTextArea();
add(new JScrollPane(message), gbc, 0, 3, 2, 1);

communication = new JTextArea();
add(new JScrollPane(communication), gbc, 0, 4, 2, 1);

gbc.weighty = 0;
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
new
Thread()
{
public void run()
{
sendMail();
}
}.start();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(sendButton);
add(buttonPanel, gbc, 0, 5, 2, 1);
}

/**
Add a component to this frame.
@param c the component to add
@param gbc the grid bag constraints
@param x the grid bax column
@param y the grid bag row
@param w the number of grid bag columns spanned
@param h the number of grid bag rows spanned
*/
private void add(Component c, GridBagConstraints gbc,
int x, int y, int w, int h)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
getContentPane().add(c, gbc);
}

/**
Sends the mail message that has been authored in the GUI.
*/
public void sendMail()
{
try
{
Socket s = new Socket(smtpServer.getText(), 25);

out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new
InputStreamReader(s.getInputStream()));

String hostName
= InetAddress.getLocalHost().getHostName();

receive();
send("HELO " + hostName);
receive();
send("MAIL FROM: <">");
receive();
send("RCPT TO: <">");
receive();
send("DATA");
receive();
StringTokenizer tokenizer = new StringTokenizer(
message.getText(), "\n");
while (tokenizer.hasMoreTokens())
send(tokenizer.nextToken());
send(".");
receive();
s.close();
}
catch (IOException exception)
{
communication.append("Error: " + exception);
}
}

/**
Sends a string to the socket and echoes it in the
communication text area.
@param s the string to send.
*/
public void send(String s) throws IOException
{
communication.append(s);
communication.append("\n");
out.print(s);
out.print("\r\n");
out.flush();
}

/**
Receives a string from the socket and displays it
in the communication text area.
*/
public void receive() throws IOException
{
String line = in.readLine();
if (line != null)
{
communication.append(line);
communication.append("\n");
}
}

private BufferedReader in;
private PrintWriter out;
private JTextField from;
private JTextField to;
private JTextField smtpServer;
private JTextArea message;
private JTextArea communication;

public static final int WIDTH = 300;
public static final int HEIGHT = 300;
}

 

Posts:7,258
Registered: 1/31/02
Re: Email Code  
Jul 14, 2004 11:06 AM (reply 1 of 13)



 
Hi,
This is the code for sending and recieving mails
through SMTP server.
Problem is that how i should bring it into real
action.I have a stand alone pc.
I connect using a dialup connection and my motive is
to be able to send/recieve
to any personal id of my choice could be yahoo/hotmail
etc.
Kindly elaborate in steps,i will pleased to listen to
this response .
It is copied from book of Advanced Core java and works
ok.But i dont know what SMTP server,
From: and To: to state

I don't see how you would know if it works ok without an SMTP Server to test it against. You need to talk to your ISP or mail service about what SMTP server you can use - since I don't know your particulars, here's some info:

http://help.yahoo.com/help/us/mail/pop/pop-11.html
http://www.uic.edu/depts/accc/ecomm/smtpmove/isps.html
http://www.more-solutions.co.uk/email/smtp.html

real hip hop, my man

Good Luck

Lee
 

Posts:474
Registered: 3/12/04
Re: Email Code  
Jul 14, 2004 11:23 AM (reply 2 of 13)



 
Thanx,well i will try to manage such issues but what steps are required actually
 

Posts:7,258
Registered: 1/31/02
Re: Email Code  
Jul 14, 2004 11:26 AM (reply 3 of 13)



 
Well, I've only glanced t your code, but I couldna help but notice you have a JTextField called "smtpServer", so once you manage the issue of what SMTP server to use, key that into the text field on your gui and cross your fingers that your code works ok.

Lee
 

Posts:11
Registered: 7/5/03
Re: Email Code  
Jul 14, 2004 12:04 PM (reply 4 of 13)



 
You could try with a Open Relay SMTP server or a your ISP's one. There are a lot of servers which allow you to send emails without the need to have an account with they. Another thing, the code you have only permits you to send messages adn not receive them. You could have a look in any JavaMail tutorial and check how you can receive emails.
 

Posts:474
Registered: 3/12/04
Re: Email Code  
Jul 14, 2004 1:21 PM (reply 5 of 13)



 
Hi, following this link

http://help.yahoo.com/help/us/mail/pop/pop-11.html

i got a way to do so -->i followed the same procedure given in the link.At the end both Outlook Express and
this java program gives same error ie Authentication required 530 error something...

However by using different settings other than these it fails to validate the password.
 

Posts:183
Registered: 5/28/04
Re: Email Code  
Jul 14, 2004 6:08 PM (reply 6 of 13)



 
To test SMTP without dealing with firewalls and availability, I've setup and run Apache James on my laptop. Its easy to use, fast, and small. Makes for reliable testing.

http://james.apache.org/

-Scott
http://www.swiftradius.com
 

Posts:474
Registered: 3/12/04
Re: Email Code  
Jul 16, 2004 12:42 AM (reply 7 of 13)



 
Hi folks i have made arrangement for SMTP server.It is my own machine at present ie localhost and
i also managed a POP server and an authenticated id associated with it.

My program shows delivering message but on checking mail id ,it doesnot reaches.I examined this code
particularily and i feel it is a kindergarten try of a mail program given in the book.

Does any body feel that it should work .Kindly go through the top code and let me know?

My main purpose is to send mails through SMTP to other mail ids.Please let me know if u have a
JDK1.3 and J2EE 1.3 compliant code for the same.I will be very obliged.
 

Posts:474
Registered: 3/12/04
Re: Email Code  
Jul 16, 2004 2:08 AM (reply 8 of 13)



 
Waiting eagerly for response
 

Posts:31,095
Registered: 4/30/99
Re: Email Code  
Jul 16, 2004 8:13 AM (reply 9 of 13)



 
Hi folks i have made arrangement for SMTP server.It is
my own machine at present ie localhost and
i also managed a POP server and an authenticated id
associated with it.

My program shows delivering message but on checking
mail id ,it doesnot reaches.I examined this code
particularily and i feel it is a kindergarten try of a
mail program given in the book.

You should talk to the administrator of the SMTP server to find out why the message was not sent.
 

Posts:3,258
Registered: 00-08-28
Re: Email Code  
Jul 16, 2004 8:17 AM (reply 10 of 13)



 
I guess you could use the Default SMTP server on your windows machine. It by no means is of production quality but it might be good for testing purposes.

control panel>adminstrative tools>IIS>SMTP
 

Posts:474
Registered: 3/12/04
Re: Email Code  
Jul 17, 2004 10:56 PM (reply 11 of 13)



 
There r 50 smtp server available download on web.So we can make localhost.No probs
 

Posts:474
Registered: 3/12/04
Re: Email Code  
Jul 17, 2004 10:56 PM (reply 12 of 13)



 
There r 50 smtp server available download on web.So we can make localhost.No probs
 

Posts:50
Registered: 7/5/04
Re: Email Code  
Jul 29, 2004 7:06 AM (reply 13 of 13)



 
If you want to send an email without knowing any mail server, try using this third party mail component

http://www.lavantech.com/smtpmailer

This SMTP component provides an easy API to send E-Mails from an application. The API has option to send emails directly to the recipients mail server without a relaying mail server. Plain Authentication is supported for mail server authentication. Mail Attachments are supported using MIME.
 
This topic has 13 replies on 1 page.