Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a mail using SSL from a zohomail account

Tags:

java

email

ssl

zoho

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.

like image 583
Angad Singh Avatar asked Dec 18 '22 20:12

Angad Singh


1 Answers

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("[email protected]","passwordofid");
            }
        });
        try 
        {   MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipients(MimeMessage.RecipientType.TO,InternetAddress.parse("[email protected]"));
            message.setSubject("Test Subject");
            message.setText("Test Email Body");
            Transport.send(message);
        } 
        catch (MessagingException e) 
        {   e.printStackTrace();
        }
    }
}
like image 127
Angad Singh Avatar answered Dec 21 '22 10:12

Angad Singh