How Can I send EMail through Proxy Server.
843830Jul 14 2004 — edited Jul 14 2004HI,
I m a novice Java programmer. I came to know about Java Mail API recently and don't know detail about Mail API. I use internet through proxy server and am facing problem sending email from my program.
Is there any restrictions sending emails through proxy servers. Can anyone help me how to send email through proxy server.
Noteice that: I have tested my program using a dial-up connection to internet and that works nicely.
my programs code is given below:
/*******************Start*******************/
import java.util.*;
import javax.swing.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.awt.*;
import java.awt.event.*;
public class TestSend extends JFrame{
public static String usr,pass;
private JButton btnsend;
private JTextArea msgtxt;
private JTextField smtpserver,usrname,mailto,mailfrom,subject;
private JPasswordField password;
public TestSend(){
setTitle("Message Sending Program");
Container c = getContentPane();
c.setLayout(new BorderLayout(5,5));
smtpserver = new JTextField("-----.-----.-----.-----");
usrname = new JTextField(15);
password = new JPasswordField(15);
subject = new JTextField("This is a test message.");
mailfrom = new JTextField("-------@-----.com");
mailto = new JTextField("-----@-----.com");
Panel p1 = new Panel( new GridLayout(6,2,5,5));
p1.add( new JLabel("MailServer Url"));
p1.add( smtpserver);
p1.add( new JLabel("Username"));
p1.add( usrname );
p1.add( new JLabel("Password"));
p1.add( password);
p1.add( new JLabel("Subject:"));
p1.add( subject );
p1.add( new JLabel("Email From:"));
p1.add( mailfrom);
p1.add( new JLabel("Email To:"));
p1.add( mailto);
c.add(p1,BorderLayout.NORTH);
msgtxt = new JTextArea(20,20);
msgtxt.setCaretColor(Color.yellow);
msgtxt.setBackground (Color.black);
msgtxt.setForeground(Color.green);
msgtxt.setFont(new Font("courier new",Font.BOLD,13));
c.add(new JScrollPane(msgtxt));
btnsend = new JButton("Send");
btnsend.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
sendMessage();
}
}
);
c.add(btnsend,BorderLayout.SOUTH);
this.pack();
this.setSize(500,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(new Color(87,121,172));
this.setVisible(true);
}
public void sendMessage(){
usr = usrname.getText();
pass = password.getText();
boolean success = true;
Properties props = System.getProperties ();
props.put("mail.smtp.host",smtpserver.getText());
props.put("mail.smtp.auth", "true");
Authenticator auth = new MyAuthenticator();
Session session = Session.getInstance (props,auth);
MimeMessage msg = new MimeMessage(session);
try{
msg.setSubject(subject.getText());
msg.setFrom (new InternetAddress(mailfrom.getText()));
msg.setRecipient(Message.RecipientType.TO,new InternetAddress(mailto.getText()));
msg.setText(msgtxt.getText());
}catch( Exception ex ){
success = false;
JOptionPane.showMessageDialog(this,ex.toString(),"Exception Occured!!",JOptionPane.ERROR_MESSAGE);
}
try{
Transport.send(msg);
}catch(MessagingException ex ){
success = false;
JOptionPane.showMessageDialog(this,ex.toString(),"Exception Occured!!",JOptionPane.ERROR_MESSAGE);
}
if( success )
JOptionPane.showMessageDialog(this,"Message Sent!!","Confirm",-1);
else
JOptionPane.showMessageDialog(this,"Message Not Sent!!","Confirm",-1);
}
public static void main( String[] args ){
new TestSend().show();
}
}
class MyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthenticaton(){
if( TestSend.usr == "" || TestSend.usr == null ){
JOptionPane.showMessageDialog(null,"Please provide name and password.","Message",-1);
}
return new PasswordAuthentication(TestSend.usr,TestSend.pass);
}
}
/****************End*********************/
Thanks in advance.