Results 1 to 2 of 2

Thread: Java mail

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

    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("mail.smtp.host", "smtp.gmail.com");
            prps.put("mail.smtp.port", "465");
            
            //Create some properties and get the default Session
            Session ses = Session.getInstance(prps,null);
            ses.setDebug(true);
            
            //Create a message
            Message msg = new MimeMessage(ses);
            
            //Set the FROM and TO address
            InternetAddress addrFrom = new InternetAddress(from);
            /*msg.setFrom(addrFrom);*/
            
            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!");*/
            try{
                msg.setFrom(addrFrom);
                msg.addHeader("VLS-01:", "Online feedback!");
                msg.setRecipients(Message.RecipientType.TO, addrTo);
                msg.setSubject(subject);
                msg.setText(body);
            
            Transport.send(msg);
    
            }catch(Exception err){
                System.out.println(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 "smtp.gmail.com", port 465, isSSL false
    Sep 15, 2011 7:42:36 PM SendMail actionPerformed
    SEVERE: null
    javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
      nested exception is:
    	java.net.ConnectException: Connection refused: connect
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
    	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
    	at javax.mail.Service.connect(Service.java:275)
    	at javax.mail.Service.connect(Service.java:156)
    	at javax.mail.Service.connect(Service.java:105)
    	at javax.mail.Transport.send0(Transport.java:168)
    	at javax.mail.Transport.send(Transport.java:98)
    	at SendMail.sendMail(SendMail.java:139)
    	at SendMail.actionPerformed(SendMail.java:93)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6463)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6228)
    	at java.awt.Container.processEvent(Container.java:2213)
    	at java.awt.Component.dispatchEventImpl(Component.java:4819)
    	at java.awt.Container.dispatchEventImpl(Container.java:2271)
    	at java.awt.Component.dispatchEvent(Component.java:4645)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4816)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4476)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4406)
    	at java.awt.Container.dispatchEventImpl(Container.java:2257)
    	at java.awt.Component.dispatchEvent(Component.java:4645)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Caused by: java.net.ConnectException: Connection refused: connect
    	at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
    	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)
    	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
    	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
    	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    	at java.net.Socket.connect(Socket.java:578)
    	at java.net.Socket.connect(Socket.java:527)
    	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
    	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
    	... 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 smtp.gmail.com 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
    Posts
    1
    Rep Power
    0

    Default Re: Java mail

    package sify.com;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream.GetField;
    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() {

    l.add("123abc@gmail.com");
    l.add("123qwer@gmail.com");
    l.add("123mnop@gmail.com");


    String to = "1234abcd@yahoo.com";
    String from = "12345nnn@gmail.com";
    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("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.user", "");//put your username with domain exp.. asdf@gmail.com
    props.put("mail.smtp.password", "");//put your password

    props.put("mail.smtp.port", "587");
    Session session = Session.getInstance(props, new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("", "");//put your username with domain exp.. abcd@gmail.com
    //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.
    session.setDebug(sessionDebug);
    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)i.next();
    ccs.add(new InternetAddress(s));

    }
    msg.setRecipients(Message.RecipientType.CC,
    (InternetAddress[]) ccs.toArray(new InternetAddress[ccs.size()]));


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

    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(messageText);
    // Hand the message to the default transport service
    // for delivery.
    Transport transport = session.getTransport("smtp");
    System.out.println("msg.getSubject() "+msg.getSubject());
    transport.connect("smtp.gmail.com", 587, "aaaa2003@gmail.com", "password");//put your username with domain exp.. xyz123@gmail.com
    //username.......//password // put your password
    transport.sendMessage(msg, msg.getAllRecipients());
    System.out.println("after Transport:");
    }
    catch (MessagingException mex) {
    mex.printStackTrace();
    System.out.println(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();
    dateMatcher.sendMsg();



    }
    }

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, 05: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
  •