JSP中傳送郵件【Z】
Jakarta釋出了Commons Emails 1.0 released 版本,目的是為了簡化JavaMail。
知道有它幾個class嗎?你一定想不到,只有8個!
好了,開始我們的jakarta commons emails 之旅:)
一:Quick Start
通過SimpleEmail傳送郵件
1
java.lang.Object
2 org.apache.commons.mail.Email
3 org.apache.commons.mail.SimpleEmail
2 org.apache.commons.mail.Email
3 org.apache.commons.mail.SimpleEmail
1
SimpleEmail email
=
new
SimpleEmail();
2 email.setHostName( " mail.4ya.cn " );
3 email.setAuthentication( " <username> " , " <password> " )
4 email.addTo( " martin.xus@gmail.com " , " martin " );
5 email.setFrom( " martin@4ya.cn " , " martin " );
6 email.setSubject( " 測試主題 " );
7 email.setMsg( " 這裡是郵件內容 " );
8 email.send();
2 email.setHostName( " mail.4ya.cn " );
3 email.setAuthentication( " <username> " , " <password> " )
4 email.addTo( " martin.xus@gmail.com " , " martin " );
5 email.setFrom( " martin@4ya.cn " , " martin " );
6 email.setSubject( " 測試主題 " );
7 email.setMsg( " 這裡是郵件內容 " );
8 email.send();
就如程式碼裡字面上的意思一樣簡單:
1:建立以SimpleEmail物件
2:設定傳送信件的smtp伺服器,如果沒有設定,會尋找系統變數中mail.host值。
3:設定smtp的使用者和密碼
4:收件人
5:發件人
6:主題
7:內容
8:傳送
二:傳送帶附件的郵件
我們可以傳送本機的附件,當然我們也可以傳送非本機的附件,如果傳送的是一個存在網路上的附件的url,則郵件傳送的時候會自動下載,新增到附件中。
1:)傳送本地附件:
1
EmailAttachment attachment
=
new
EmailAttachment();
2 attachment.setPath( " test/test.rar " );
3 attachment.setDisposition(EmailAttachment.ATTACHMENT);
4 attachment.setDescription( " python resource " );
5 attachment.setName( " resource " );
2 attachment.setPath( " test/test.rar " );
3 attachment.setDisposition(EmailAttachment.ATTACHMENT);
4 attachment.setDescription( " python resource " );
5 attachment.setName( " resource " );
2:)傳送不存在本地的附件
1
EmailAttachment attachment
=
new
EmailAttachment();
2 attachment.setURL( new URL( " http://www.smilinglibrary.org/sldoc/pics/index03.jpg " ));
3 attachment.setDisposition(EmailAttachment.ATTACHMENT);
4 attachment.setDescription( " 微笑圖書館 " );
5 attachment.setName( " 微笑圖書館 " );
2 attachment.setURL( new URL( " http://www.smilinglibrary.org/sldoc/pics/index03.jpg " ));
3 attachment.setDisposition(EmailAttachment.ATTACHMENT);
4 attachment.setDescription( " 微笑圖書館 " );
5 attachment.setName( " 微笑圖書館 " );
next,新增附件到我們的郵件中
1
MultiPartEmail email
=
new
MultiPartEmail();
2 email.setHostName( " mail.4ya.cn " );
3 email.setAuthentication(" <username> " , " <password> " )
4 email.addTo( " martin.xus@gmail.com " , " martin " );
5 email.setFrom( " martin@4ya.cn " , " martin " );
6 email.setSubject( " 郵件主題 " );
7 email.setMsg( " 郵件內容 " );
8 // 新增附件
9 email.attach(attachment);
10
11 // 傳送郵件
12 email.send();
2 email.setHostName( " mail.4ya.cn " );
3 email.setAuthentication(" <username> " , " <password> " )
4 email.addTo( " martin.xus@gmail.com " , " martin " );
5 email.setFrom( " martin@4ya.cn " , " martin " );
6 email.setSubject( " 郵件主題 " );
7 email.setMsg( " 郵件內容 " );
8 // 新增附件
9 email.attach(attachment);
10
11 // 傳送郵件
12 email.send();
如果需要傳送多個附件,只需建立多個EmailAttachement,即可
1
email.attach(attachment1)
2 email.attach(attachment2)
2 email.attach(attachment2)
三:傳送html格式的郵件
通過HtmlEmail我們可以傳送Html格式的郵件:
1
java.lang.Object
2 org.apache.commons.mail.Email
3 org.apache.commons.mail.MultiPartEmail
4 org.apache.commons.mail.HtmlEmail
5
2 org.apache.commons.mail.Email
3 org.apache.commons.mail.MultiPartEmail
4 org.apache.commons.mail.HtmlEmail
5
如下:
1
//
HtmlEmail!
2 HtmlEmail email = new HtmlEmail();
3 email.setHostName( " mail.4ya.cn " );
3 email.setAuthentication(" <username> " , " <password> " )
5 email.addTo("martin@4ya.cn " martin " );
6 email.setFrom("martin.xus@gmail.com " martin " );
7 email.setSubject( "主題:該郵件包括html格式內容 " );
8 // embed the image and get the content id
9 // 注意這裡:embed 將幫助我們建立標籤如:cid:xxx url
10 URL url = new URL( " http://www.apache.org/images/asf_logo_wide.gif " );
11 String cid = email.embed(url, " Apache logo " );
12
13 /**
14 set the html message
15 我們看到HtmlEmail extends Email的,它依然有setMsg(),但是這裡傳送的郵件包括了插入在郵件內容中的圖片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg程式碼
16 * */
17 email.setHtmlMsg( " <html>The apache logo - <img src=\ " cid: " +cid+ " \ " ></html> " );
18
19 // set the alternative message
20 email.setTextMsg( " Your email client does not support HTML messages " );
21
22 // set mail
23 email.send();
24
2 HtmlEmail email = new HtmlEmail();
3 email.setHostName( " mail.4ya.cn " );
3 email.setAuthentication(" <username> " , " <password> " )
5 email.addTo("martin@4ya.cn " martin " );
6 email.setFrom("martin.xus@gmail.com " martin " );
7 email.setSubject( "主題:該郵件包括html格式內容 " );
8 // embed the image and get the content id
9 // 注意這裡:embed 將幫助我們建立標籤如:cid:xxx url
10 URL url = new URL( " http://www.apache.org/images/asf_logo_wide.gif " );
11 String cid = email.embed(url, " Apache logo " );
12
13 /**
14 set the html message
15 我們看到HtmlEmail extends Email的,它依然有setMsg(),但是這裡傳送的郵件包括了插入在郵件內容中的圖片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg程式碼
16 * */
17 email.setHtmlMsg( " <html>The apache logo - <img src=\ " cid: " +cid+ " \ " ></html> " );
18
19 // set the alternative message
20 email.setTextMsg( " Your email client does not support HTML messages " );
21
22 // set mail
23 email.send();
24
四:最後一步
如果需要實現更復雜authenticator 你可以extends javax.mail.Authenticator
,實現你自己的東西,然後呼叫Email.setAuthenticator(javax.mail.Authenticator newAuthenticator)即可
這一點jakarta也做了,給我們提供了一個defaultAuthenticator
1
java.lang.Object
2 javax.mail.Authenticator
3 org.apache.commons.mail.DefaultAuthenticator
2 javax.mail.Authenticator
3 org.apache.commons.mail.DefaultAuthenticator
覆蓋掉該方法,實現你自己的東東 o_o
1
protected
javax.mail.PasswordAuthentication getPasswordAuthentication()
相關文章
- JSP筆記-傳送郵件JS筆記
- 郵件傳送
- 傳送郵件
- Android中呼叫傳送郵件Android
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- Laravel 傳送郵件Laravel
- PHP傳送郵件PHP
- Django——郵件傳送Django
- java郵件傳送Java
- Laravel傳送郵件Laravel
- gmail傳送郵件AI
- Oracle郵件傳送Oracle
- java傳送郵件Java
- Powershell郵件傳送
- thinkphp 郵件傳送PHP
- centos 傳送郵件CentOS
- phpcms傳送郵件PHP
- 郵件的傳送
- JSP與JavaMail(4)---傳送HTML格式的郵件 (轉)JSJavaAIHTML
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- Linux配置msmtp+mutt傳送郵件(可放在指令碼中定時傳送郵件)Linux指令碼
- golang傳送郵件(qq郵箱)Golang
- 郵件傳送API整理API
- php windows 傳送郵件PHPWindows
- 定時傳送郵件
- thinkjs 傳送郵件JS
- 使用phpmailer傳送郵件PHPAI
- Ubuntu的郵件傳送Ubuntu
- python傳送郵件Python
- nodejs 傳送郵件NodeJS
- 傳送郵件那些事
- javamail郵件傳送例子JavaAI
- 使用JavaMail傳送郵件JavaAI
- 使用nodemailer傳送郵件AI
- javamail傳送郵件(轉)JavaAI
- 使用python傳送郵件和接收郵件Python
- 如何傳送電子郵件到別人郵箱?電子郵件傳送的方法
- 使用 smtplib 傳送郵件