JavaMailAPI发送简单的电子邮件

Git ww9992019com 发表于 1 周前

<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="1728">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

下面是一个例子发送一个简单的电子邮件。在这里,我们使用JangoSMPT服务器通过该电子邮件被发送到我们的目标电子邮件地址。 The setup is explained in the <a href="http://www.yiibai.com/javamail_api/javamail_api_environment_setup.html">Environment Setup</a> chapter.</p>

<p>

要发送一个简单的电子邮件的步骤依次是:</p>

<ul>

<li>

    <p>

        获取一个Session</p>

</li>

<li>

    <p>

        创建一个默认 MimeMessage 对象,并设置发件人,收件人,主题(From, To, Subject)在消息中。 </p>

</li>

<li>

    <p>

        将实际的消息为:</p>

    <pre class="prettyprint prettyprinted">

<span class="pln">message</span><span class="pun">.</span><span class="pln">setText</span><span class="pun">(</span><span class="str">"your text goes here"</span><span class="pun">);</span></pre>

</li>

<li>

    <p>

        发送使用传输对象的消息。</p>

</li>

</ul>

<h2>

创建Java类</h2>

<p>

创建一个Java类文件SendEmail,是其内容如下:</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">package</span><span class="pln"> com</span><span class="pun">.</span><span class="pln">yiibai</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="typ">Properties</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="typ">Message</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="typ">MessagingException</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="typ">PasswordAuthentication</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="typ">Session</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="typ">Transport</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="pln">internet</span><span class="pun">.</span><span class="typ">InternetAddress</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="pln">internet</span><span class="pun">.</span><span class="typ">MimeMessage</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">SendEmail</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

  </span><span class="com">// Recipient's email ID needs to be mentioned.</span><span class="pln">

  </span><span class="typ">String</span><span class="pln"> to </span><span class="pun">=</span><span class="pln"> </span><span class="str">"destinationemail@gmail .com"</span><span class="pun">;</span><span class="pln">



  </span><span class="com">// Sender's email ID needs to be mentioned</span><span class="pln">

  </span><span class="typ">String</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="str">"fromemail@gmail .com"</span><span class="pun">;</span><span class="pln">

  </span><span class="kwd">final</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> username </span><span class="pun">=</span><span class="pln"> </span><span class="str">"manishaspatil"</span><span class="pun">;</span><span class="com">//change accordingly</span><span class="pln">

  </span><span class="kwd">final</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> password </span><span class="pun">=</span><span class="pln"> </span><span class="str">"******"</span><span class="pun">;</span><span class="com">//change accordingly</span><span class="pln">



  </span><span class="com">// Assuming you are sending email through relay.jangosmtp.net</span><span class="pln">

  </span><span class="typ">String</span><span class="pln"> host </span><span class="pun">=</span><span class="pln"> </span><span class="str">"relay.jangosmtp.net"</span><span class="pun">;</span><span class="pln">



  </span><span class="typ">Properties</span><span class="pln"> props </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Properties</span><span class="pun">();</span><span class="pln">

  props</span><span class="pun">.</span><span class="pln">put</span><span class="pun">(</span><span class="str">"mail.smtp.auth"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"true"</span><span class="pun">);</span><span class="pln">

  props</span><span class="pun">.</span><span class="pln">put</span><span class="pun">(</span><span class="str">"mail.smtp.starttls.enable"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"true"</span><span class="pun">);</span><span class="pln">

  props</span><span class="pun">.</span><span class="pln">put</span><span class="pun">(</span><span class="str">"mail.smtp.host"</span><span class="pun">,</span><span class="pln"> host</span><span class="pun">);</span><span class="pln">

  props</span><span class="pun">.</span><span class="pln">put</span><span class="pun">(</span><span class="str">"mail.smtp.port"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"25"</span><span class="pun">);</span><span class="pln">



  </span><span class="com">// Get the Session object.</span><span class="pln">

  </span><span class="typ">Session</span><span class="pln"> session </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Session</span><span class="pun">.</span><span class="pln">getInstance</span><span class="pun">(</span><span class="pln">props</span><span class="pun">,</span><span class="pln">

     </span><span class="kwd">new</span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="typ">Authenticator</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

        </span><span class="kwd">protected</span><span class="pln"> </span><span class="typ">PasswordAuthentication</span><span class="pln"> getPasswordAuthentication</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

           </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">PasswordAuthentication</span><span class="pun">(</span><span class="pln">username</span><span class="pun">,</span><span class="pln"> password</span><span class="pun">);</span><span class="pln">

   </span><span class="pun">}</span><span class="pln">

     </span><span class="pun">});</span><span class="pln">



  </span><span class="kwd">try</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

   </span><span class="com">// Create a default MimeMessage object.</span><span class="pln">

   </span><span class="typ">Message</span><span class="pln"> message </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">MimeMessage</span><span class="pun">(</span><span class="pln">session</span><span class="pun">);</span><span class="pln">



   </span><span class="com">// Set From: header field of the header.</span><span class="pln">

   message</span><span class="pun">.</span><span class="pln">setFrom</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">InternetAddress</span><span class="pun">(</span><span class="kwd">from</span><span class="pun">));</span><span class="pln">



   </span><span class="com">// Set To: header field of the header.</span><span class="pln">

   message</span><span class="pun">.</span><span class="pln">setRecipients</span><span class="pun">(</span><span class="typ">Message</span><span class="pun">.</span><span class="typ">RecipientType</span><span class="pun">.</span><span class="pln">TO</span><span class="pun">,</span><span class="pln">

           </span><span class="typ">InternetAddress</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">to</span><span class="pun">));</span><span class="pln">



   </span><span class="com">// Set Subject: header field</span><span class="pln">

   message</span><span class="pun">.</span><span class="pln">setSubject</span><span class="pun">(</span><span class="str">"Testing Subject"</span><span class="pun">);</span><span class="pln">



   </span><span class="com">// Now set the actual message</span><span class="pln">

   message</span><span class="pun">.</span><span class="pln">setText</span><span class="pun">(</span><span class="str">"Hello, this is sample for to check send "</span><span class="pln"> </span><span class="pun">+</span><span class="pln">

    </span><span class="str">"email using JavaMailAPI "</span><span class="pun">);</span><span class="pln">



   </span><span class="com">// Send message</span><span class="pln">

   </span><span class="typ">Transport</span><span class="pun">.</span><span class="pln">send</span><span class="pun">(</span><span class="pln">message</span><span class="pun">);</span><span class="pln">



   </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Sent message successfully...."</span><span class="pun">);</span><span class="pln">



  </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">catch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">MessagingException</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

     </span><span class="kwd">throw</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RuntimeException</span><span class="pun">(</span><span class="pln">e</span><span class="pun">);</span><span class="pln">

  </span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span></pre>

<p>

由于我们使用的是由主机提供商JangoSMTP 提供 SMTP服务器,我们需要验证用户名和密码。javax.mail.PasswordAuthentication类用于验证的密码。</p>

<h2>

编译并运行</h2>

<p>

现在,我们类是准备好了,让我们编译上面的类。已经保存了类SendEmail.java 到目录:/home/manisha/JavaMailAPIExercise。我们需要jar文件 javax.mail.jar andactivation.jar 在 classpath中。执行下面的命令从命令提示符编译类(jars被放置在/home/manisha/ 目录下):</p>

<pre class="prettyprint prettyprinted">

<span class="pln">javac </span><span class="pun">-</span><span class="pln">cp </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">manisha</span><span class="pun">/</span><span class="pln">activation</span><span class="pun">.</span><span class="pln">jar</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">manisha</span><span class="pun">/</span><span class="pln">javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="pln">jar</span><span class="pun">:</span><span class="pln"> </span><span class="typ">SendEmail</span><span class="pun">.</span><span class="pln">java</span></pre>

<p>

现在,这个类被编译,执行下面的命令来运行:</p>

<pre class="prettyprint prettyprinted">

<span class="pln">java </span><span class="pun">-</span><span class="pln">cp </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">manisha</span><span class="pun">/</span><span class="pln">activation</span><span class="pun">.</span><span class="pln">jar</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">manisha</span><span class="pun">/</span><span class="pln">javax</span><span class="pun">.</span><span class="pln">mail</span><span class="pun">.</span><span class="pln">jar</span><span class="pun">:</span><span class="pln"> </span><span class="typ">SendEmail</span></pre>

<h2>

验证输出</h2>

<p>

你应该看到下面的消息命令控制台上:</p>

<pre class="prettyprint prettyprinted">

<span class="typ">Sent</span><span class="pln"> message successfully</span><span class="pun">....</span></pre>

<p>

因为我通过JangoSMTP发送邮件到我的Gmail地址,下面的邮件会在我的Gmail帐户的收件箱中接收:</p>

<img alt="JavaMail API Send Email" src="/uploads/allimg/131206/0A63a5T-0.jpg">

<div>

 </div>



                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="JavaMail API 发送电子邮件"><i class="layui-icon"></i>
                            JavaMail API 发送电子邮件</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:JavaMail API 发送电子邮件带有附件"><i class="layui-icon"></i>
                            JavaMail API 发送电子邮件带有附件</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复