Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    10e5x is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default Using java send email

    i have tried using java eclipse to send email. i have import the external jar file of mail.jar. i got an error that says : Could not connect to SMTP host. i do not understand this. how to resolve. can someone help me pls?

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi.
    Can you show your code?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by 10e5x View Post
    Could not connect to SMTP host.
    Is your SMTP up and running? What is the full stack trace of the error? Can you post that?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    10e5x is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    this is inside my gui listerner.


    String subject = "Chit Chit recovery";
    String message = "Your username is " + u1.getUserName() + "Your password is "
    + u1.getPwd()+ ". Thank you for using Chit Chit";
    String from = "guoxiang1610@gmail.com";
    Email e1 = new Email();
    e1.sendEmail(from, email, subject, message);

  5. #5
    10e5x is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    this is inside my gui listerner.


    String subject = "Chit Chit recovery";
    String message = "Your username is " + u1.getUserName() + "Your password is "
    + u1.getPwd()+ ". Thank you for using Chit Chit";
    String from = "guoxiang1610@gmail.com";
    Email e1 = new Email();
    e1.sendEmail(from, email, subject, message);

  6. #6
    10e5x is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    public void sendEmail(
    String aFromEmailAddr, String aToEmailAddr,
    String aSubject, String aBody
    ){
    //Here, no Authenticator argument is used (it is null).
    //Authenticators are used to prompt the user for user
    //name and password.
    Session session = Session.getDefaultInstance( fMailServerConfig, null );
    MimeMessage message = new MimeMessage( session );
    try {
    //the "from" address may be set in code, or set in the
    //config file under "mail.from" ; here, the latter style is used
    //message.setFrom( new InternetAddress(aFromEmailAddr) );
    message.addRecipient(
    Message.RecipientType.TO, new InternetAddress(aToEmailAddr)
    );
    message.setSubject( aSubject );
    message.setText( aBody );
    Transport.send( message );
    }
    catch (MessagingException ex){
    System.err.println("Cannot send email. " + ex);
    }
    }
    public static void refreshConfig() {
    fMailServerConfig.clear();
    fetchConfig();
    }

    // PRIVATE //

    private static Properties fMailServerConfig = new Properties();

    static {
    fetchConfig();
    }

    /**
    * Open a specific text file containing mail server
    * parameters, and populate a corresponding Properties object.
    */
    private static void fetchConfig() {
    InputStream input = null;
    try {
    //If possible, one should try to avoid hard-coding a path in this
    //manner; in a web application, one should place such a file in
    //WEB-INF, and access it using ServletContext.getResourceAsStream.
    //Another alternative is Class.getResourceAsStream.
    //This file contains the javax.mail config properties mentioned above.
    input = new FileInputStream( "C:\\Temp\\MyMailServer.txt" );
    fMailServerConfig.load( input );
    }
    catch ( IOException ex ){
    System.err.println("Cannot open and load mail server properties file.");
    }
    finally {
    try {
    if ( input != null ) input.close();
    }
    catch ( IOException ex ){
    System.err.println( "Cannot close mail server properties file." );
    }
    }
    }

    this is my email class

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Seems like you have stored the SMTP host details inside your MyMailServer.txt file. What I am interested to know is that, the SMTP host address provided by you is running or not?

    Your original error "Could not connect to SMTP host." is may be out of no connectivity to SMTP. Can you ping to that host and confirm?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    10e5x is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    and how do i do that?sorry i am new to java

  9. #9
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by 10e5x View Post
    and how do i do that?sorry i am new to java

    Most probably your MyMailServer.txt file must be containing some host address. Something like,

    Java Code:
    mail.host = some_address
    OR
    mail.smtp.host = some_address

    You need to first validate that it's a valid SMTP server, which can actually 'send' your mail outside. Then you need to check whether it is up and running to serve your request. For that go to command prompt and "ping it" to see whether it returns anything or not.

    Like,

    Java Code:
    ping some_address

    If it returns a valid response, then you are good to go. If it doesn't, then you need to make sure that it's running before you try to send mail from it.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  10. #10
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Sometime I use JES (Java Email Server) for testing email app. You can try too.
    but Goldest correctly pointed out the problem. You have mistake in configuration email.
    Skype: petrarsentev
    http://TrackStudio.com

  11. #11
    10e5x is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    what if i changed my coding to: this is @ Email class.
    public class Email {
    public void postMail( String recipients, String subject, String message , String from) throws MessagingException
    {
    boolean debug = false;

    //Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.email.msn.com");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length()];
    for (int i = 0; i < recipients.length(); i++)
    {
    addressTo[i] = new InternetAddress(recipients);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);


    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
    }

    this is my calling method:
    String subject = "Chit Chit recovery";
    String message = "Your username is " + u1.getUserName() + "Your password is "
    + u1.getPwd()+ ". Thank you for using Chit Chit";
    String from = "guoxiang@gmail.com";
    Email e1 = new Email();
    e1.postMail(email, subject, message, from);

    cuz right the other code, i still have the error.

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by 10e5x View Post
    //Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.email.msn.com");
    See, now here you are providing the details for SMTP host. But is that configured for you? Or is it a free SMTP server?

    Here is a list of free SMTP servers : Free SMTP Servers. See if any of them can work for you.

    But before finalizing any server, make sure that you should "ping" to it first and make sure that you are getting the response.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  13. #13
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    And please use Code Tags from next time, whenever you post your code.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  14. #14
    10e5x is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    i go to command promt and type in. ping guoxiang@gmail.com
    they methion cannot find host. what does dat mean? that is a valid mail.

  15. #15
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by 10e5x View Post
    i go to command promt and type in. ping guoxiang@gmail.com
    they methion cannot find host. what does dat mean? that is a valid mail.
    This is your personal e-mail ID. That's not SMTP host address. Go through the link I have sent you, there are free SMTP server addresses present.

    Or contact to your system administrators, if you are in office. Or try smtp.gmail.com

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  16. #16
    10e5x is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    it does ping back. ok so now i do i send email?thanks thanks so i change my sender's address to smtp.gmail.com?

  17. #17
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    It is topic about configuration gmail
    send SMTP mail using JavaMail with gmail account
    Skype: petrarsentev
    http://TrackStudio.com

  18. #18
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by 10e5x View Post
    so i change my sender's address to smtp.gmail.com?
    No No No... Why do you want to change the sender's address?

    This is SMTP host that we are talking about. You are supposed to put this in the properties as the value of your "mail.smtp.host" key. Like,

    Java Code:
    props.put("mail.smtp.host", "smtp.gmail.com");

    I hope you are clear on that,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  19. #19
    10e5x is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    i have this is my Email class.i tried send. but it still have error:

    com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. i16sm6986647ibl.0


    what does dis means? and should i do? thanks goldest, you have been an great help.

  20. #20
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by 10e5x View Post
    com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. i16sm6986647ibl.0
    This is a weird error regarding STARTTLS and may need some domain/account settings to enable SSL.

    Try a new approach, you can add some more properties to your code to get the things working. You can have,

    Java Code:
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "465");
    For host and port details.

    Java Code:
    props.put("mail.smtp.auth", "true");
    For authentication that you would need to do by using your gmail account username/password.

    Java Code:
    props.put("mail.debug", "true");
    To see the debug activities about your send process.

    Java Code:
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    These are the additional SSL socket factory settings.

    You would have to override the Authenticator's getPasswordAuthentication() method in order to validate the authentication.

    Try out these things and see what happens?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Page 1 of 2 12 LastLast

Similar Threads

  1. how to send email notification using java
    By tedy2808 in forum New To Java
    Replies: 3
    Last Post: 08-17-2010, 02:15 PM
  2. Replies: 10
    Last Post: 06-06-2010, 07:49 PM
  3. how to send an email
    By painamrata in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 02-22-2009, 05:28 PM
  4. send email using JMS
    By Heather in forum Advanced Java
    Replies: 9
    Last Post: 01-07-2009, 04:04 PM
  5. send email using apache commons email
    By jnamendi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 10-14-2008, 06:55 PM

Posting Permissions

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