Send email in Java with gmail SMTP example.
MailUtil.java
Main.java
Can add multiple recipients and BCC recipients
Instead of msg.setContent(message, "text/plain") you can use msg.setContent(message, "text/html") then you can add html tags to message body.
MailUtil.java
- package mailtest;
- import java.io.UnsupportedEncodingException;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.mail.Authenticator;
- 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 MailUtil {
- private String SMTP_HOST = "smtp.gmail.com";
- private String FROM_ADDRESS = "yourname@gmail.com";
- private String PASSWORD = "yourpassword";
- private String FROM_NAME = "Sameera";
- public boolean sendMail(String[] recipients, String[] bccRecipients, String subject, String message) {
- try {
- Properties props = new Properties();
- props.put("mail.smtp.host", SMTP_HOST);
- props.put("mail.smtp.auth", "true");
- props.put("mail.debug", "false");
- props.put("mail.smtp.ssl.enable", "true");
- Session session = Session.getInstance(props, new SocialAuth());
- Message msg = new MimeMessage(session);
- InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME);
- msg.setFrom(from);
- InternetAddress[] toAddresses = new InternetAddress[recipients.length];
- for (int i = 0; i < recipients.length; i++) {
- toAddresses[i] = new InternetAddress(recipients[i]);
- }
- msg.setRecipients(Message.RecipientType.TO, toAddresses);
- InternetAddress[] bccAddresses = new InternetAddress[bccRecipients.length];
- for (int j = 0; j < bccRecipients.length; j++) {
- bccAddresses[j] = new InternetAddress(bccRecipients[j]);
- }
- msg.setRecipients(Message.RecipientType.BCC, bccAddresses);
- msg.setSubject(subject);
- msg.setContent(message, "text/plain");
- Transport.send(msg);
- return true;
- } catch (UnsupportedEncodingException ex) {
- Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- } catch (MessagingException ex) {
- Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- }
- class SocialAuth extends Authenticator {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(FROM_ADDRESS, PASSWORD);
- }
- }
- }
Main.java
Note: mail.jar is required.
- package mailtest;
- public class Main {
- public static void main(String[] args) {
- String[] recipients = new String[]{"youremail@yahoo.com"};
- String[] bccRecipients = new String[]{"youremail@gmail.com"};
- String subject = "Hi this is test Mail";
- String messageBody = "Test Mail from codesstore.blogspot.com";
- new MailUtil().sendMail(recipients, bccRecipients, subject, messageBody);
- }
- }
Can add multiple recipients and BCC recipients
- String[] recipients = new String[]{"youremail@yahoo.com","youremail2@yahoo.com"};
Instead of msg.setContent(message, "text/plain") you can use msg.setContent(message, "text/html") then you can add html tags to message body.
This is an informative article. Thanks for this post. This is a beautiful and informative post. Keep it up dear. Black magic specialist in india - Love Samrat
ReplyDelete