email java java mail Latest

Java – Sending Email – Mkyong.com

Email Icon

To send e mail in Java we’d like JavaMail

pom.xml

com.solar.mail
javax.mail
1.6.2

1. Ship Email

Send odd e mail in text format.

EmailEmailSMTP.java

package deal com.mkyong;

import com.solar.mail.smtp.SMTPTransport;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.web.InternetAddress;
import javax.mail.web.MimeMessage;
import java.util.Date;
import java.util.Real property;

public class SendEmailSMTP

// for instance smtp.mailgun.org
personal static last string SMTP_SERVER = "smtp server";
personal static remaining string USERNAME = "";
personal static last string PASSWORD = "";

personal static last string EMAIL_FROM = "From@gmail.com";
personal static remaining string EMAIL_TO = "email_1@yahoo.com, email_2@gmail.com";
personal static ultimate string EMAIL_TO_CC = "";

personal static remaining string EMAIL_SUBJECT = "Test Send Email via SMTP";
personal static last string EMAIL_TEXT = "Hello Java Mail's ABC123";

public Static void primary (String [] args)

Features prop = System.getProperties ();
prop.put ("mail.smtp.host", SMTP_SERVER); // non-compulsory, defined in SMTPTransport
prop.put ("mail.smtp.auth", "true");
prop.put ("mail.smtp.port", "25"); // default port 25

Session = Session.getInstance (prop, null);
Message msg = new MimeMessage (session);

attempt

// beginning with
msg.setFrom (new InternetAddress (EMAIL_FROM));

// –
msg.setRecipients (Message.RecipientType.TO,
InternetAddress.parse (EMAIL_TO, false));

// cc
msg.setRecipients (Message.RecipientType.CC,
InternetAddress.parse (EMAIL_TO_CC, false));

// subject
msg.setSubject (EMAIL_SUBJECT);

// content
msg.setText (EMAIL_TEXT);

msg.setSentDate (new date ());

// Get SMTPTransport
SMTPTransport t = (SMTPTransport) session.getTransport ("smtp");

// join
t.join (SMTP_SERVER, USERNAME, PASSWORD);

// ship
t.sendMessage (msg, msg.getAllRecipients ());

System.out.println ("Answer:" + t.getLastServerResponse ());

t.close ();

in the hall (MessagingException e)
e.printStackTrace ();

2. Sending Email in HTML

The hot button is to create a customized javax.activation.DataSource that handles HTML format.

SendEmailHTML.java

package deal com.mkyong;

import com.solar.mail.smtp.SMTPTransport;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.web.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Real estate;

public class SendEmailHTML

// for instance smtp.mailgun.org
personal static ultimate string SMTP_SERVER = "smtp server";
personal static ultimate string USERNAME = "";
personal static last string PASSWORD = "";

personal static remaining string EMAIL_FROM = "From@gmail.com";
personal static ultimate string EMAIL_TO = "email_1@yahoo.com, email_2@gmail.com";
personal static ultimate string EMAIL_TO_CC = "";

personal static last string EMAIL_SUBJECT = "Test Send Email via SMTP (HTML)";
personal static remaining string EMAIL_TEXT = "

Hi Java Mail n ABC123

";

public Static void foremost (String [] args)

Options prop = System.getProperties ();
prop.put ("mail.smtp.auth", "true");

Session = Session.getInstance (prop, null);
Message msg = new MimeMessage (session);

attempt

msg.setFrom (new InternetAddress (EMAIL_FROM));

msg.setRecipients (Message.RecipientType.TO,
InternetAddress.parse (EMAIL_TO, false));

msg.setSubject (EMAIL_SUBJECT);

// TEXT Email
//msg.setText(EMAIL_TEXT);

// HTML e mail
msg.setDataHandler (new DataHandler (new HTMLDataSource (EMAIL_TEXT)));

SMTPTransport t = (SMTPTransport) session.getTransport ("smtp");

// connect
t.join (SMTP_SERVER, USERNAME, PASSWORD);

// ship
t.sendMessage (msg, msg.getAllRecipients ());

System.out.println ("Answer:" + t.getLastServerResponse ());

t.close ();

in the hall (MessagingException e)
e.printStackTrace ();

static class HTMLDataSource implements DataSource

personal string html;

public HTMLDataSource (String htmlString)
html = htmlString;

@Bypass
public InputStream getInputStream () throws IOException
if (html == null) throws a brand new IOException ("html message is zero!");
restore new ByteArrayInputStream (html.getBytes ());

@Bypass
public OutputStream getOutputStream () throws IOException
throw a brand new IOException ("This DataHandler cannot write HTML");

@Bypass
public String getContentType ()
returns to "text / html";

@Bypass
public String getName ()
returns to "HTMLDataSource";

three. Email with attachment

3.1 Key creates MimeBodyPart and adds it to Multipart

import javax.mail.web.MimeBodyPart;
import javax.mail.web.MimeMessage;
import javax.mail.web.MimeMultipart;

// textual content
MimeBodyPart p1 = new MimeBodyPart ();
p1.setText (EMAIL_TEXT);

// file
MimeBodyPart p2 = new MimeBodyPart ();

FileDataSource fds = new FileDataSource ("path / example.txt");
p2.setDataHandler (new DataHandler (fds));
p2.setFileName (fds.getName ());

Multipart mp = new MimeMultipart ();
mp.addBodyPart (p1);
mp.addBodyPart (p2);

msg.setContent (mp);

three.2 An ideal instance of attaching and sending an attachment.

SendEmailAttachment.java

package deal com.mkyong;

import com.solar.mail.smtp.SMTPTransport;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.web.InternetAddress;
import javax.mail.web.MimeBodyPart;
import javax.mail.web.MimeMessage;
import javax.mail.web.MimeMultipart;
import java.util.Actual estate;

public class SendEmailAttachment

// for instance smtp.mailgun.org
personal static last string SMTP_SERVER = "smtp server";
personal static remaining string USERNAME = "";
personal static remaining string PASSWORD = "";

personal static ultimate string EMAIL_FROM = "From@gmail.com";
personal static remaining string EMAIL_TO = "email_1@yahoo.com, email_2@gmail.com";
personal static last string EMAIL_TO_CC = "";

personal Static remaining String EMAIL_SUBJECT = "Test email sending via SMTP (ATTACHMENT)";
personal static last string EMAIL_TEXT = "Hello Java Mail's ABC123";

public Static void major (String [] args)

Features prop = System.getProperties ();
prop.put ("mail.smtp.auth", "true");

Session = Session.getInstance (prop, null);
Message msg = new MimeMessage (session);

attempt

msg.setFrom (new InternetAddress (EMAIL_FROM));

msg.setRecipients (Message.RecipientType.TO,
InternetAddress.parse (EMAIL_TO, false));

msg.setSubject (EMAIL_SUBJECT);

// textual content
MimeBodyPart p1 = new MimeBodyPart ();
p1.setText (EMAIL_TEXT);

// file
MimeBodyPart p2 = new MimeBodyPart ();
FileDataSource fds = new FileDataSource ("path / example.txt");
p2.setDataHandler (new DataHandler (fds));
p2.setFileName (fds.getName ());

Multipart mp = new MimeMultipart ();
mp.addBodyPart (p1);
mp.addBodyPart (p2);

msg.setContent (mp);

SMTPTransport t = (SMTPTransport) session.getTransport ("smtp");

// join
t.connect (SMTP_SERVER, USERNAME, PASSWORD);

// send
t.sendMessage (msg, msg.getAllRecipients ());

System.out.println ("Answer:" + t.getLastServerResponse ());

t.shut ();

within the corridor (MessagingException e)
e.printStackTrace ();

References