Hi.

I am learing JSP and testing mail. but I don't know why error generate. Please help me.

Source code.

package tags;

import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 

public class HelloMail { 
  public static void main(String args[])throws MessagingException {   
    String name ="jsp learning"; 
    String from = "kim.ilikeenglish.gc@gmail.com"; 
    String to = "kim.ilikeenglish.gc@gmail.com"; 
    String title = "mail test"; 
    String contents = "Hi. Nice to meet you"; 
    String host = "gmail.com"; 
     
    Properties prop = System.getProperties(); 
    Session session = Session.getInstance(prop, null);     
    MimeMessage message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(name+"<"+from+">")); 
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));     
    message.setSubject(title, "KSC5601"); 
    message.setText(contents);   
     
    Transport transport = session.getTransport("smtp"); 
    transport.connect(host, "", "" );     
    transport.sendMessage(message, message.getAllRecipients()); 
    transport.close(); 
  } 
}

error message.

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: gmail.com, port: 25, response: 421
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1533)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at tags.HelloMail.main(HelloMail.java:25)


thanks you.

Have you inserted username and password in Service.connect()?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.