<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nepherte (dot) be &#187; smtps</title>
	<atom:link href="http://www.nepherte.be/tag/smtps/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nepherte.be</link>
	<description>About Nepherte, Mosiah and the person behind</description>
	<lastBuildDate>Mon, 30 Aug 2010 18:45:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Howto: Send Mail Over SMTPS In Java</title>
		<link>http://www.nepherte.be/send-mail-over-smtps-in-java/</link>
		<comments>http://www.nepherte.be/send-mail-over-smtps-in-java/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 16:00:28 +0000</pubDate>
		<dc:creator>Nepherte</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[smtps]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.nepherte.be/?p=72</guid>
		<description><![CDATA[Introduction This tutorial will show you how to send mail in Java over SMTPS. SMTP, or Simple Mail Transfer Protocol, is the de facto standard for email transmissions across the internet. The SSL protocol, or Secure Socket Layer, provides an encrypted, secure connection. Many providers try to use this secure version of smtp and I [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction</strong><br />
This tutorial will show you how to send mail in Java over SMTPS. SMTP, or Simple Mail Transfer Protocol, is the de facto standard for email transmissions across the internet. The SSL protocol, or Secure Socket Layer, provides an encrypted, secure connection. Many providers try to use this secure version of smtp and I encourage everyone to make use of it. </p>
<p><strong>Example code</strong><br />
In the example given, we will use the smtps server of Google. To use this server, you will need a Gmail account. The example makes use of the fictive email account &#8216;mail@gmail.com&#8217; with &#8216;password&#8217; and the <a href="http://java.sun.com/products/javamail/">JavaMail</a> library.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Properties</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.mail.Message</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.mail.Multipart</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.mail.Session</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.mail.internet.InternetAddress</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.mail.internet.MimeBodyPart</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.mail.internet.MimeMessage</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.mail.internet.MimeMultipart</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.mail.smtp.SMTPSSLTransport</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> sendMail<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> to, <span style="color: #003399;">String</span> from, <span style="color: #003399;">String</span> subject, Multipart content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// create properties</span>
<span style="color: #003399;">Properties</span> props <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">getProperties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// &lt; -- authorization, i.e. account &amp;amp; password is required --&gt;</span>
props.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mail.smtps.auth&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// &lt; -- it is important you use the correct port. smtp uses 25, smtps 465 --&gt;</span>
props.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mail.smtps.port&quot;</span>, <span style="color: #0000ff;">&quot;465&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// &lt; -- put the smtps server host address here --&gt;</span>
props.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mail.smtps.host&quot;</span>, <span style="color: #0000ff;">&quot;smtp.gmail.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create session</span>
Session session <span style="color: #339933;">=</span> Session.<span style="color: #006633;">getDefaultInstance</span><span style="color: #009900;">&#40;</span>props<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
session.<span style="color: #006633;">setDebug</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create content</span>
MimeMultipart msgContent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MimeMultipart<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;alternative&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Text Mail</span>
MimeBodyPart html <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MimeBodyPart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// &lt; -- we will send plain text, &quot;text/html&quot; is possible as well --&gt;</span>
html.<span style="color: #006633;">setContent</span><span style="color: #009900;">&#40;</span>content, <span style="color: #0000ff;">&quot;text/plain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
html.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MIME-Version&quot;</span>, <span style="color: #0000ff;">&quot;1.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
html.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span>, <span style="color: #0000ff;">&quot;text/plain; charset=iso-8859-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
msgContent.<span style="color: #006633;">addBodyPart</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
MimeMessage msg <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MimeMessage<span style="color: #009900;">&#40;</span>session<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
msg.<span style="color: #006633;">setContent</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// set sender and recipient</span>
msg.<span style="color: #006633;">setFrom</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> InternetAddress<span style="color: #009900;">&#40;</span>from<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
msg.<span style="color: #006633;">addRecipient</span><span style="color: #009900;">&#40;</span>Message.<span style="color: #006633;">RecipientType</span>.<span style="color: #006633;">TO</span>, <span style="color: #000000; font-weight: bold;">new</span> InternetAddress<span style="color: #009900;">&#40;</span>to<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
msg.<span style="color: #006633;">setSubject</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// transport the message</span>
<span style="color: #666666; font-style: italic;">// &lt; -- we will send the message over smtps --&gt;</span>
SMTPSSLTransport transport <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SMTPSSLTransport<span style="color: #009900;">&#41;</span>session.<span style="color: #006633;">getTransport</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;smtps&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// connect to server</span>
<span style="color: #666666; font-style: italic;">// &lt; -- fill in gmail email address and password --&gt;</span>
transport.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;smtp.gmail.com&quot;</span>, <span style="color: #0000ff;">&quot;mail@gmail.com&quot;</span>, <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// send the message</span>
transport.<span style="color: #006633;">sendMessage</span><span style="color: #009900;">&#40;</span>msg, msg.<span style="color: #006633;">getAllRecipients</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// close the connection</span>
transport.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
e.<span style="color: #006633;">getStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nepherte.be/send-mail-over-smtps-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
