czupe czupe - 1 year ago 130
Java Question

Sending Email via gmail smtp server in JAVA

What is the problem with this code? Somehow it is getting in to an infinity loop at the line

line, no error message, no exception, just maybe infinite loop (i don't know because i don't wait more than 5-10minute)

final String username = "<mail_name>";
final String password = "<password>";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("", "");
props.put("mail.smtp.port", "465");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("<mail_from>"));
message.setSubject("Test Subject");



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

Answer Source

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("", 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 {

public void test(){
    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", true); // added this line
    props.put("", "");
    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.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

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

        // Add html part to multi part

        // Associate multi-part with message

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

    } catch (AddressException e) {
        // TODO Auto-generated catch block
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download