开发者

Sending an email: Problem when connecting to host

I am attempting to send an email using Java. From reading, I have found that if I use gmail as the host I can do this for free & it should work, is that correct.

So I have my code below, & I am attempting to send an email from myself to my friends emails(the emails in my code below have been altered for privacy) but I get an exception thrown when I go to send/transfer the email (at the line Transport.send(msg);)

The output/exception thrown is:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25; nested exception is: java.net.ConnectException: Connection timed out: connect Should have succeeded: false

What do you think I am doing wrong?

/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
class SMTPAuthenticator extends javax.mail.Authenticator
{

    public PasswordAuthentication getPasswordAuthentication()
    {
        String username = "myaccount@gmail.com";
        String password = "xxxxxxx";
        return new PasswordAuthentication(username, password);
    }
}

public class SendEmail 
{

    public Sen开发者_StackOverflowdEmail() 
    {

    }

    public static boolean sendEmail( String from, String to[], String subject, String body ) 
    {
        try 
        {
            boolean debug = false;

            // Set the host smtp address
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com" );  // "smtp.jcom.net");
            props.put("mail.smtp.auth", "true");

            // create some properties and get the default Session
            // Session session = Session.getDefaultInstance(props, null);
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getDefaultInstance(props, auth);
            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[to.length];
            for (int i = 0; i < to.length; i++) 
            {
                addressTo[i] = new InternetAddress(to[i]);
            }
            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(body, "text/plain");

            System.out.println( "1" );
            Transport.send(msg);
            System.out.println( "2" );
        } 
        catch (Exception e) 
        {
            System.out.println( e );
            return false;
        }

        return true;
    }

    public static void main(String args[]) 
    {
        boolean res = SendEmail.sendEmail( "myaccount@gmail.com", new String[] {"x@y.com", "y@x.com.au"}, 
                                           "Test", "Did it work?" );
        System.out.println( "Should have succeeded: " + res );
    }

}


Google is your friend. Check this site: http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

Gmail only accepts secure connections using TLS, but you're using the standard non-secure authentication.


Try with the port 465 (found here)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜