In this Example, I am showing you how to send bulk emails in Java Technology.
In Java technology one function named - sendEmail() works. this function has three parameters like: -

1). Subject - Subject is used for sending headline of any mail (can be string type).
2). emailToAddresses - this parameter is used for mailing to send mail those will receive mail.
3). emailBodyText - This parameter contains the overall body content. full message of client.

With the help of this mailing system you can send mail to anybody using server. for testing you can send mail at your email-id and then check it. you will get a mail return.

Requirements: -

mail API Jar
JDK

First of all create a class that will send emails to particular recipient.

1). Create a project in Java IDE
2). Configure mail API jar file into project to use it
3). Create class which will send you mails.


package in.estuffcage.email;
 
import java.util.List;
import javax.mail.Message;
import java.util.Properties;
import javax.mail.PasswordAuthentication;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import org.apache.log4j.xml.DOMConfigurator;
import org.apache.log4j.Logger;

/**
  * @mailauthor http://www.estuffcage.com
  */
  
 public class SendsEmail {
 
    public static void main(String[] args) {
     
        //emails list where the mails has to be sent.....
        List emails = new ArrayList();
        emails.add("estuffcage1@email.com");
        emails.add("estuffcage2@email.com");
        emails.add("estuffcage3@email.com");
        emails.add("estuffcage4@email.com");
        emails.add("estuffcage5@email.com");
         
        //email subject
        String subject = "This is a Live project Tutorial Website";
         
        //message to be sent
        String message = "This is a Body message from Estuffcage.com";
         
        //send mail to multiple recipient
        sendEmail(subject, emails, message);
         
    }
 
 public static void sendEmail(final String subject, final List emailToAddresses,
                final String emailBodyText) {
         
        // from email address
        final String USERNAME = "email address";
        // make sure you have to put correct password
        final String PASSWORD = "email password";
        // SMTP email server
        final String SMTPHOST = "your mail server SMTP host";
 
        // We have to put some property for SMTP configuration
        Properties props = new Properties();
 
        // Don't change - start
        props.put("mail.smtp.user", "USERNAME");
        props.put("mail.smtp.host", SMTPHOST);
        props.put("mail.smtp.auth", "true");
        // do not change - end
 
        // Now create session
        Session sessionstart = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(USERNAME, PASSWORD);
                    }
                });
  
  String email = null;
        try {
            // Now Create new message
            Message messagestart = new MimeMessage(sessionstart);
            messagestart.setFrom(new InternetAddress(USERNAME));
            messagestart.setSubject(subject);

            // send email with html format to the recipient
            String content = "\n\n";
            content += emailBodyText + "\n";
            content += "\n\n";
            content += "\n";
            messagestart.setContent(content, "text/html");
 
   // split the email on comma-separated
            StringBuilder sb = new StringBuilder();
            int i = 0;
            for (String email : emailToAddresses) {
                sb.append(email);
                i++;
                if (emailToAddresses.size() > i) {
                    sb.append(", ");
                   }
               }
 
            emails = sb.toString();
 
            // set 'to email address'
           
            message.setRecipients(Message.RecipientType.BCC,
                    InternetAddress.parse(sb.toString()));
 
            System.out.println("Sending Email to " + emails + " from "
                    + USERNAME + " with Subject - " + subject);
 
            // send the email
            Transport.send(messagestart);
 
            System.out.println("Email Sent Successfully at " + emails);
 
        } catch (MessagingException e) {
            System.out.println("Email Not Sent " + emails);
            System.out.println(e);
        }
    }
}
4). Now Execute this Application and run.
If you still face any difficulty then contact me through contact form.

Thanks for reading my Blog.

0 comments :

Post a Comment

 
# Top