java邮件发送api文件,JavaMail API 发送一个HTML电子邮件
下面是一個例子,從你的機器發送HTML格式電子郵件。這里通過使用JangoSMPT服務器的郵件發送到我們的目標電子郵件地址。
這個例子非常相似,發送簡單的電子郵件,除非,這里我們使用的是使用setContent()方法來設置內容的第二個參數為“"text/html"指定的HTML內容被包含在消息中。通過這個例子,你可以發送喜歡HTML內容。
發送包含HTML內容的電子郵件,遵循的步驟是:
獲得一個Session
創建一個默認的MimeMessage對象,并設置發件人,收件人,主題(From,?To,?Subject)在消息中。
設置使用使用setContent(實際的消息),如下方法:
message.setContent("
This is actual message embedded in
HTML tags","text/html");
發送使用傳輸對象的消息。
創建Java類
創建一個Java類文件SendHTMLEmail,是其內容如下:
packagecom.yiibai;importjava.util.Properties;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;publicclassSendHTMLEmail{publicstaticvoidmain(String[]args){// Recipient's email ID needs to be mentioned.Stringto="destinationemail@gmail.com";// Sender's email ID needs to be mentionedStringfrom="fromemail@gmail.com";finalStringusername="manishaspatil";//change accordinglyfinalStringpassword="******";//change accordingly// Assuming you are sending email through relay.jangosmtp.netStringhost="relay.jangosmtp.net";Propertiesprops=newProperties();props.put("mail.smtp.auth","true");props.put("mail.smtp.starttls.enable","true");props.put("mail.smtp.host",host);props.put("mail.smtp.port","25");// Get the Session object.Sessionsession=Session.getInstance(props,newjavax.mail.Authenticator(){protectedPasswordAuthenticationgetPasswordAuthentication(){returnnewPasswordAuthentication(username,password);}});try{// Create a default MimeMessage object.Messagemessage=newMimeMessage(session);// Set From: header field of the header.message.setFrom(newInternetAddress(from));// Set To: header field of the header.message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));// Set Subject: header fieldmessage.setSubject("Testing Subject");// Send the actual HTML message, as big as you likemessage.setContent("
This is actual message embedded in HTML tags
","text/html");// Send messageTransport.send(message);System.out.println("Sent message successfully....");}catch(MessagingExceptione){e.printStackTrace();thrownewRuntimeException(e);}}}由于我們使用的是由主機提供商JangoSMTP提供 SMTP服務器,我們需要驗證用戶名和密碼。javax.mail.PasswordAuthentication類用于驗證的密碼。
編譯并運行
Now that our class is ready, let us compile the above class. I've saved the class SendHTMLEmail.java to directory :?/home/manisha/JavaMailAPIExercise. We would need the jars?javax.mail.jar?andactivation.jar?in the classpath. Execute the command below to compile the class (both the jars are placed in /home/manisha/ directory) from command prompt:
javac-cp/home/manisha/activation.jar:/home/manisha/javax.mail.jar:SendHTMLEmail.java
Now that the class is compiled, execute the below command to run:
java-cp/home/manisha/activation.jar:/home/manisha/javax.mail.jar:SendHTMLEmail
Verify Output
You should see the following message on the command console:
Sentmessage successfully....
As I'm sending an email to my gmail address through JangoSMTP, the following mail would be received in my gmail account inbox:
¥ 我要打賞
糾錯/補充
收藏
加QQ群啦,易百教程官方技術學習群
注意:建議每個人選自己的技術方向加群,同一個QQ最多限加 3 個群。
總結
以上是生活随笔為你收集整理的java邮件发送api文件,JavaMail API 发送一个HTML电子邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java mail 发件人昵称,Java
- 下一篇: php丢弃,在IIS 7.5中,PHP吓