4

I have been looking for a method to send an email from my zohomail account using Java mailing api, I have been through many examples available online but none of them did worked.There was always a problem with setting up properties. After being through the forums of zohomail, I have figured out that the following code worked for me.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Angad Singh
  • 1,032
  • 1
  • 17
  • 36

1 Answers1

11

Below is a Java program to send an email from an email id registered on zohomail. The program uses java mailing API.

import java.util.Properties;

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 MailTest 
{   public static void main(String[] args) 
    {   Properties properties = new Properties();
        properties.setProperty("mail.smtp.host", "smtp.zoho.com");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.port", "465");
        properties.setProperty("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.debug", "true");
        properties.put("mail.store.protocol", "pop3");
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.debug.auth", "true");
        properties.setProperty( "mail.pop3.socketFactory.fallback", "false");
        Session session = Session.getDefaultInstance(properties,new javax.mail.Authenticator() 
        {   @Override
            protected PasswordAuthentication getPasswordAuthentication() 
            {   return new PasswordAuthentication("sendersid@anymail.com","passwordofid");
            }
        });
        try 
        {   MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("sendersid@anymail.com"));
            message.setRecipients(MimeMessage.RecipientType.TO,InternetAddress.parse("recieversid@anymail.com"));
            message.setSubject("Test Subject");
            message.setText("Test Email Body");
            Transport.send(message);
        } 
        catch (MessagingException e) 
        {   e.printStackTrace();
        }
    }
}
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Angad Singh
  • 1,032
  • 1
  • 17
  • 36