API JavaMail - Gmail SMPT Server

In tutti i capitoli precedenti abbiamo utilizzato il server JangoSMPT per inviare e-mail. In questo capitolo impareremo a conoscere il server SMPT fornito da Gmail. Gmail (tra gli altri) offre l'uso gratuito del proprio server SMTP pubblico.

I dettagli del server SMTP di Gmail sono disponibili qui . Come puoi vedere nei dettagli, possiamo utilizzare la connessione TLS o SSL per inviare e-mail tramite il server SMTP di Gmail.

La procedura per inviare e-mail utilizzando il server SMTP di Gmail è simile a quella spiegata nel capitolo Invio di e-mail , tranne per il fatto che cambieremo il server host. Come prerequisito, l'indirizzo e-mail del mittente deve essere un account Gmail attivo. Facciamo un esempio.

Crea classe Java

Crea un file Java SendEmailUsingGMailSMTP, i cui contenuti sono i seguenti:

package com.tutorialspoint;

import java.util.Properties;

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

public class SendEmailUsingGMailSMTP {
   public static void main(String[] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "[email protected]";//change accordingly

      // Sender's email ID needs to be mentioned
      String from = "[email protected]";//change accordingly
      final String username = "abc";//change accordingly
      final String password = "*****";//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = "smtp.gmail.com";

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

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

      try {
         // Create a default MimeMessage object.
         Message message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,
         InternetAddress.parse(to));

         // Set Subject: header field
         message.setSubject("Testing Subject");

         // Now set the actual message
         message.setText("Hello, this is sample for to check send "
            + "email using JavaMailAPI ");

         // Send message
         Transport.send(message);

         System.out.println("Sent message successfully....");

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

Qui l'host è impostato come smtp.gmail.com e la porta è impostata come 587 . Qui abbiamo abilitato la connessione TLS.

Compila ed esegui

Ora che la nostra classe è pronta, compiliamo la classe precedente. Ho salvato la classe SendEmailUsingGMailSMTP.java nella directory:/home/manisha/JavaMailAPIExercise. Avremmo bisogno dei jar javax.mail.jar e activation.jar nel classpath. Esegui il comando seguente per compilare la classe (entrambi i jar sono posizionati nella directory / home / manisha /) dal prompt dei comandi:

javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendEmailUsingGMailSMTP.java

Ora che la classe è compilata, esegui il comando seguente per eseguire:

java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendEmailUsingGMailSMTP

Verifica output

Dovresti vedere il seguente messaggio sulla console di comando:

Sent message successfully....