Discussion:
send SMTP mail using JavaMail with gmail account
(too old to reply)
j***@hotmail.com
2005-02-21 17:04:42 UTC
Permalink
I want the Java application to send email using google gmail account.

The following is the output, and it cannot display "sendMail() 3..."
in the following sendMail() method. The program just hangs after
printing "sendMail() 2...", and there is no errors.

output
===============
sendMail()...
sendMail() 2...

Basically it get stucks on line
transport.connect(smtpHost, 465, "gmail account", "gmail password");

----------------------------------------------------------
public void sendMail() throws MessagingException
{
System.out.println("sendMail()...");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");

// props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
MimeMessage message = new MimeMessage(session);
message.setFrom(from);
Iterator itrto = toRecipients.iterator();
while(itrto.hasNext())
{
Object obj = (Address)itrto.next();
if(obj instanceof Address){
message.addRecipient(Message.RecipientType.TO, (Address)obj);
}
}
System.out.println("sendMail() 2...");

message.setSubject(subject);
message.setContent(msgText, "text/plain");

Transport transport = session.getTransport("smtp");

//***** GET STUCK HERE!!! ******
transport.connect(smtpHost, 465, "gmail account", "gmail password");

System.out.println("sendMail() 3...");

Transport.send(message);

System.out.println("sendMail() 4...");

}
--------------------------------------------------------

any ideas? please advise.
thanks!!
GaryM
2005-02-21 17:49:32 UTC
Permalink
// props.put("mail.smtp.auth", "true");
Uncomment this line.
j***@hotmail.com
2005-02-21 22:19:08 UTC
Permalink
Post by GaryM
// props.put("mail.smtp.auth", "true");
Uncomment this line.
I uncomment line props.put("mail.smtp.auth", "true");
but it still cannot display sendMail() 3...:

sendMail()...
sendMail() 2...

But it has exception this time:

javax.mail.MessagingException: Could not connect to SMTP host:
smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:867)
at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:156)
at javax.mail.Service.connect(Service.java:234)
at SMTPHandler.sendMail(SMTPHandler.java:145)
at SMTPHandlerTest.main(SMTPHandlerTest.java:33)
Exception in thread "main"



any ideas? thanks!!
GaryM
2005-02-21 23:18:28 UTC
Permalink
Post by j***@hotmail.com
any ideas? thanks!!
I have no time to try this myself, but you can give it a go and post
back:

props.put("mail.smtp.starttls.enable","true");
GaryM
2005-02-22 00:28:16 UTC
Permalink
This post might be inappropriate. Click to display it.
p***@gmail.com
2005-02-23 19:21:44 UTC
Permalink
hello
l***@gmail.com
2019-07-09 12:52:15 UTC
Permalink
Post by GaryM
// props.put("mail.smtp.auth", "true");
Uncomment this line.
l***@gmail.com
2019-07-09 12:52:46 UTC
Permalink
On

Loading...