JavaMail is a Java API used to send and receive email via SMTP, POP3 and IMAP. JavaMail is built into the Java EE platform, but also provides an optional package for use in Java SE. You may refer to the code below.
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
// Send a simple, single part, text/plain e-mail
public class TestEmail {
public static void main(String[] args) {
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = "sendToMailAddress";
String from = "sendFromMailAddress";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "smtp.yourisp.invalid";
// Create properties, get Session
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
// To see what is going on behind the scene
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
try {
// Instantiate a message
Message msg = new MimeMessage(session);
//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through Java");
msg.setSentDate(new Date());
// Set message content
msg.setText("This is a test of sending a " +
"plain text e-mail through Java.\n" +
"Here is line 2.");
//Send the message
Transport.send(msg);
}
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
}
}//End of class
<p>JavaMail<b> </b>is a <a href="http://www.keepautomation.com/products/java_barcode/" mce_href="http://www.keepautomation.com/products/java_barcode/">Java <span class="mw-redirect">API</span></a> used to send and receive email via <span class="mw-redirect">SMTP</span>, <span class="mw-redirect">POP3</span> and <span class="mw-redirect">IMAP</span>. JavaMail is built into the <span class="mw-redirect">Java EE</span> platform, but also provides an optional package for use in <span class="mw-redirect">Java SE</span>. You may refer to the code below.</p><pre class="de1"><span class="kw2">import</span> <span class="co2">java.util.*</span><span class="sy0">;</span>
<span class="kw2">import</span> <span class="co2">javax.mail.*</span><span class="sy0">;</span>
<span class="kw2">import</span> <span class="co2">javax.mail.internet.*</span><span class="sy0">;</span>
<span class="kw2">import</span> <span class="co2">javax.activation.*</span><span class="sy0">;</span>
<span class="co1">// Send a simple, single part, text/plain e-mail</span>
<span class="kw2">public</span> <span class="kw2">class</span> TestEmail <span class="br0">{</span>
<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">void</span> main<span class="br0">(</span><span class="kw21">String</span><span class="br0">[</span><span class="br0">]</span> args<span class="br0">)</span> <span class="br0">{</span>
<span class="co1">// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!</span>
<span class="kw21">String</span> to = <span class="st0">"sendToMailAddress"</span><span class="sy0">;</span>
<span class="kw21">String</span> from = <span class="st0">"sendFromMailAddress"</span><span class="sy0">;</span>
<span class="co1">// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!</span>
<span class="kw21">String</span> host = <span class="st0">"smtp.yourisp.invalid"</span><span class="sy0">;</span>
<span class="co1">// Create properties, get Session</span>
<span class="kw46">Properties</span> props = <span class="kw2">new</span> <span class="kw46">Properties</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
<span class="co1">// If using static Transport.send(),</span>
<span class="co1">// need to specify which host to send it to</span>
props.<span class="me1">put</span><span class="br0">(</span><span class="st0">"mail.smtp.host"</span>, host<span class="br0">)</span><span class="sy0">;</span>
<span class="co1">// To see what is going on behind the scene</span>
props.<span class="me1">put</span><span class="br0">(</span><span class="st0">"mail.debug"</span>, <span class="st0">"true"</span><span class="br0">)</span><span class="sy0">;</span>
Session session = Session.<span class="me1">getInstance</span><span class="br0">(</span>props<span class="br0">)</span><span class="sy0">;</span>
<span class="kw2">try</span> <span class="br0">{</span>
<span class="co1">// Instantiate a message</span>
Message msg = <span class="kw2">new</span> MimeMessage<span class="br0">(</span>session<span class="br0">)</span><span class="sy0">;</span>
<span class="co1">//Set message attributes</span>
msg.<span class="me1">setFrom</span><span class="br0">(</span><span class="kw2">new</span> InternetAddress<span class="br0">(</span>from<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span>
InternetAddress<span class="br0">[</span><span class="br0">]</span> address = <span class="br0">{</span><span class="kw2">new</span> InternetAddress<span class="br0">(</span>to<span class="br0">)</span><span class="br0">}</span><span class="sy0">;</span>
msg.<span class="me1">setRecipients</span><span class="br0">(</span>Message.<span class="me1">RecipientType</span>.<span class="me1">TO</span>, address<span class="br0">)</span><span class="sy0">;</span>
msg.<span class="me1">setSubject</span><span class="br0">(</span><span class="st0">"Test E-Mail through Java"</span><span class="br0">)</span><span class="sy0">;</span>
msg.<span class="me1">setSentDate</span><span class="br0">(</span><span class="kw2">new</span> <span class="kw166">Date</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span>
<span class="co1">// Set message content</span>
msg.<span class="me1">setText</span><span class="br0">(</span><span class="st0">"This is a test of sending a "</span> +
<span class="st0">"plain text e-mail through Java.<span class="es0">\n</span>"</span> +
<span class="st0">"Here is line 2."</span><span class="br0">)</span><span class="sy0">;</span>
<span class="co1">//Send the message</span>
Transport.<span class="me1">send</span><span class="br0">(</span>msg<span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>
<span class="kw2">catch</span> <span class="br0">(</span>MessagingException mex<span class="br0">)</span> <span class="br0">{</span>
<span class="co1">// Prints all nested (chained) exceptions as well</span>
mex.<span class="me1">printStackTrace</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>
<span class="br0">}</span>
<span class="br0">}</span><span class="co1">//End of class</span></pre>