Using Javamail to Connect to Gmail Smtp Server Ignores Specified Port and Tries to Use 25

Using Javamail to connect to Gmail smtp server ignores specified port and tries to use 25

In Java you would do something similar to:

Transport transport = session.getTransport("smtps");
transport.connect (smtp_host, smtp_port, smtp_username, smtp_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

Note 'smtpS' protocol. Also socketFactory properties is no longer necessary in modern JVMs but you might need to set 'mail.smtps.auth' and 'mail.smtps.starttls.enable' to 'true' for Gmail. 'mail.smtps.debug' could be helpful too.

Sending Email via gmail smtp server in JAVA

Here I am giving some changes, that work fine for me:

Session session = Session.getInstance(props,null);

You instantiate message object as you did. And finally:

Transport transport = session.getTransport("smtp");
String mfrom = "yourGmailUsernameWithout@"// example laabidiraissi
transport.connect("smtp.gmail.com", mfrom, "thepassword");
transport.sendMessage(message, message.getAllRecipients());

Edit, would you please give me a favor and copy/paste and try this example and show what it displays:

package com.test;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Test;

public class EmailService {

@Test
public void test(){
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", true); // added this line
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "username");
props.put("mail.smtp.password", "password");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", true);

Session session = Session.getInstance(props,null);
MimeMessage message = new MimeMessage(session);

System.out.println("Port: "+session.getProperty("mail.smtp.port"));

// Create the email addresses involved
try {
InternetAddress from = new InternetAddress("username");
message.setSubject("Yes we can");
message.setFrom(from);
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("receivermail"));

// Create a multi-part to combine the parts
Multipart multipart = new MimeMultipart("alternative");

// Create your text message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("some text to send");

// Add the text part to the multipart
multipart.addBodyPart(messageBodyPart);

// Create the html part
messageBodyPart = new MimeBodyPart();
String htmlMessage = "Our html text";
messageBodyPart.setContent(htmlMessage, "text/html");

// Add html part to multi part
multipart.addBodyPart(messageBodyPart);

// Associate multi-part with message
message.setContent(multipart);

// Send message
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", "username", "password");
System.out.println("Transport: "+transport.toString());
transport.sendMessage(message, message.getAllRecipients());

} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Is it possible to connect to a mail server without hard coding the port using JavaMail api's, basically the code should be independent of ports. ?

There are standard ports for these services, which JavaMail uses by default. It's relatively rare that one of these services will use a non-standard port. But you do need to know whether the service requires SSL or not, and there are two standard ports used for SMTP. You could easily write code that tries all the common ports and you would probably cover 99.99% of the cases.

Cannot sent an Email from Gmail via Java

The timeout error most likely means that you do not have connectivity to smtp.gmail.com. Most likely this has more to do with the network environment around the java code than a programming error.

This situation is almost certainly caused by a firewall somewhere. Either your ISP or network administrator does not allow connections to that address (or any address) on port 465, or the local machine has some endpoint firewall which does not allow java to connect outwards at all. Frequently endpoint firewalls come with antivirus packages. Additionally the Windows firewall will block java.exe networking by default as well.

Hope this helps!

sending email with ssl using javax.mail

Instead of

props.put("mail.transport.protocol", "smtps");

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

Use

props.put("mail.transport.protocol", "smtp");

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

Use smtp, not smtps

I used JDK 8, Netbeans 8, JavaMail 1.5.2 and this example works fine:

public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username@gmail.com","password");
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("frommail@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("tomail@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Test Mail");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}

If you are not able connect with port 465, try port 587

Exception while sending mail using Gmail Smtp?

I think the problem was in my email Id.
I created a new one and it is working.

Thank you everyone.



Related Topics



Leave a reply



Submit