Results 1 to 2 of 2

Thread: Java mail

  1. #1
    mitra is offline Member
    Join Date
    Jun 2011
    Rep Power

    Default Java mail

    Hi everybody..........
    I have a serious problem about java mail.

    Java Code:
        public void sendMail(String to[],String from,String subject,String body) throws AddressException,MessagingException{      
            //Set the host SMTP address
            Properties prps = new Properties();
            prps.put("", "");
            prps.put("mail.smtp.port", "465");
            //Create some properties and get the default Session
            Session ses = Session.getInstance(prps,null);
            //Create a message
            Message msg = new MimeMessage(ses);
            //Set the FROM and TO address
            InternetAddress addrFrom = new InternetAddress(from);
            InternetAddress addrTo[] = new InternetAddress[to.length];
            for(int i=0; i<to.length; i++){
                addrTo[i] = new InternetAddress(to[i]);
            /*msg.setRecipients(Message.RecipientType.TO, addrTo);*/
            //Optional: Set custom headers in the email
            /*msg.addHeader("VLS-01:", "Online feedback!");*/
                msg.addHeader("VLS-01:", "Online feedback!");
                msg.setRecipients(Message.RecipientType.TO, addrTo);
            }catch(Exception err){
    By using above code I try to send mail.
    but its not working. It shows the following error if I uncomment the last try catch statement----

    Java Code:
    DEBUG: setDebug: JavaMail version 1.4ea
    DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth false
    DEBUG SMTP: trying to connect to host "", port 465, isSSL false
    Sep 15, 2011 7:42:36 PM SendMail actionPerformed
    SEVERE: null
    javax.mail.MessagingException: Could not connect to SMTP host:, port: 465;
      nested exception is: Connection refused: connect
    	at com.sun.mail.smtp.SMTPTransport.openServer(
    	at com.sun.mail.smtp.SMTPTransport.protocolConnect(
    	at javax.mail.Service.connect(
    	at javax.mail.Service.connect(
    	at javax.mail.Service.connect(
    	at javax.mail.Transport.send0(
    	at javax.mail.Transport.send(
    	at SendMail.sendMail(
    	at SendMail.actionPerformed(
    	at javax.swing.AbstractButton.fireActionPerformed(
    	at javax.swing.AbstractButton$Handler.actionPerformed(
    	at javax.swing.DefaultButtonModel.fireActionPerformed(
    	at javax.swing.DefaultButtonModel.setPressed(
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(
    	at java.awt.Component.processMouseEvent(
    	at javax.swing.JComponent.processMouseEvent(
    	at java.awt.Component.processEvent(
    	at java.awt.Container.processEvent(
    	at java.awt.Component.dispatchEventImpl(
    	at java.awt.Container.dispatchEventImpl(
    	at java.awt.Component.dispatchEvent(
    	at java.awt.LightweightDispatcher.retargetMouseEvent(
    	at java.awt.LightweightDispatcher.processMouseEvent(
    	at java.awt.LightweightDispatcher.dispatchEvent(
    	at java.awt.Container.dispatchEventImpl(
    	at java.awt.Component.dispatchEvent(
    	at java.awt.EventQueue.dispatchEventImpl(
    	at java.awt.EventQueue.access$000(
    	at java.awt.EventQueue$
    	at java.awt.EventQueue$
    	at Method)
    	at java.awt.EventQueue$
    	at java.awt.EventQueue$
    	at Method)
    	at java.awt.EventQueue.dispatchEvent(
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(
    	at java.awt.EventDispatchThread.pumpEventsForFilter(
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(
    	at java.awt.EventDispatchThread.pumpEvents(
    	at java.awt.EventDispatchThread.pumpEvents(
    Caused by: Connection refused: connect
    	at Method)
    	at com.sun.mail.util.SocketFetcher.createSocket(
    	at com.sun.mail.util.SocketFetcher.getSocket(
    	at com.sun.mail.smtp.SMTPTransport.openServer(
    	... 43 more
    I can't figure out how can I do it?
    I also added the actionvation.jar and mail.jar to my netbeans library.

    Another question is---
    I run this program from my local system and my computer is internet enable.
    So is it possible to connect or whatever else from my local system or I should upload it to my online server?

    Suggest me please.
    Thank you.

  2. #2
    Join Date
    Sep 2011
    Rep Power

    Default Re: Java mail

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    import java.util.TreeSet;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import javax.swing.JTextField;
    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    //public class SimpleSendEmail {

    public class DateMatcher

    Properties props = new Properties();
    ArrayList l = new ArrayList();

    private void sendMsg() {


    String to = "";
    String from = "";
    String subject = "This is test Email. I am sending this " +
    "mail only for testing perpose. Please Don't reply";
    String messageText = "I am sending a message using the"
    + " JavaMail API.\n"
    + "Here type your message.";
    boolean sessionDebug = false;
    // Create some properties and get the default Session.
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.starttls.enable" , "true");
    props.put("", "");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.user", "");//put your username with domain exp..
    props.put("mail.smtp.password", "");//put your password

    props.put("mail.smtp.port", "587");
    Session session = Session.getInstance(props, new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("", "");//put your username with domain exp..
    //and put your password in second parameter
    System.out.println("after session:");
    // Set debug on the Session so we can see what is going on
    // Passing false will not echo debug info, and passing true
    // will.
    try {
    // Instantiate a new MimeMessage and fill it with the
    // required information.
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));

    ArrayList ccs=new ArrayList();
    for(Iterator i = l.iterator();i.hasNext();){
    String s = (String);
    ccs.add(new InternetAddress(s));

    (InternetAddress[]) ccs.toArray(new InternetAddress[ccs.size()]));

    InternetAddress[] address = {new InternetAddress(to)};

    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSentDate(new Date());
    // Hand the message to the default transport service
    // for delivery.
    Transport transport = session.getTransport("smtp");
    System.out.println("msg.getSubject() "+msg.getSubject());
    transport.connect("", 587, "", "password");//put your username with domain exp..
    //username.......//password // put your password
    transport.sendMessage(msg, msg.getAllRecipients());
    System.out.println("after Transport:");
    catch (MessagingException mex) {
    public static void main(String[] args) {
    // Collect the necessary information to send a simple message
    // Make sure to replace the values for host, to, and from with
    // valid information.
    // host - must be a valid smtp server that you currently have
    // access to.
    // to - whoever is going to get your email
    // from - whoever you want to be. Just remember that many smtp
    // servers will validate the domain of the from address
    // before allowing the mail to be sent.
    DateMatcher dateMatcher = new DateMatcher();


Similar Threads

  1. send mail via java without java mail API
    By majidvadoostan in forum Networking
    Replies: 2
    Last Post: 04-26-2011, 09:30 PM
  2. problem with sending mail usin javaX.mail api
    By sandeepsai39 in forum New To Java
    Replies: 4
    Last Post: 11-25-2009, 06:37 AM
  3. Replies: 2
    Last Post: 07-25-2009, 09:36 AM
  4. Replies: 2
    Last Post: 06-04-2009, 01:42 PM
  5. Replies: 2
    Last Post: 05-05-2008, 06:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts