Click here to Skip to main content
16,004,974 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to send email from using smtp server with help of java programming

What I have tried:

<pre lang="java">package com.techstar.com;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class IpAdress
{
 public static void main(String [] args){
	 // email ID of Recipient.
	 String to = "msr2858@gmail.com";//change accordingly  
     String from = "siddareddy.moolam@gmail.com";//change accordingly  
     String host = "smtp.gmail.com";//or IP address  
 
    //Get the session object  
     Properties properties = System.getProperties();  
     properties.setProperty("mail.smtp.host", host);  
     Session session = Session.getDefaultInstance(properties);  
 
    //compose the message  
     try{  
        MimeMessage message = new MimeMessage(session);  
        message.setFrom(new InternetAddress(from));  
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
        message.setSubject("Ping");  
        message.setText("Hello, this is example of sending email  ");  
 
        // Send message  
        Transport.send(message);  
        System.out.println("message sent successfully....");  
 
     }catch (MessagingException mex) {mex.printStackTrace();}  
  }  
}  


output:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out (Connection timed out)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at com.techstar.com.IpAdress.main(IpAdress.java:34)
Caused by: java.net.ConnectException: Connection timed out (Connection timed out)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 7 more
Posted
Updated 15-Jun-18 0:05am
Comments
Mohibur Rashid 15-Jun-18 5:59am    
have you installed smtp server?
Member 13809409 15-Jun-18 6:02am    
noo

1 solution

Just search the web for something like "java smtp gmail" to know how to set the properties for using the Gmail SMTP server:
Java
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
    new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
});
Note also that you must configure the used GMail account (specified in the From field and which credentials are passed) to allow mails to be send from untrusted applications.

You should not post valid email addresses. I suggest to edit your question using the green 'Improve question' link to remove the user names.
 
Share this answer
 
Comments
Member 13809409 15-Jun-18 6:33am    
Thanks worked..i turned on in security access in gmail application

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900