位置:首頁 > Java技術 > JavaMail > JavaMail API 發送簡單的電子郵件

JavaMail API 發送簡單的電子郵件

下麵是一個例子發送一個簡單的電子郵件。在這裡,我們使用JangoSMPT服務器通過該電子郵件被發送到我們的目標電子郵件地址。 The setup is explained in the Environment Setup chapter.

要發送一個簡單的電子郵件的步驟依次是:

  • 獲取一個Session

  • 創建一個默認 MimeMessage 對象,並設置發件人,收件人,主題(From, To, Subject)在消息中。 

  • 將實際的消息為:

    message.setText("your text goes here");
  • 發送使用傳輸對象的消息。

創建Java類

創建一個Java類文件SendEmail,是其內容如下:

package com.yiibai;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
   public static void main(String[] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "destinationemail@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "fromemail@gmail.com";
      final String username = "manishaspatil";//change accordingly
      final String password = "******";//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = "relay.jangosmtp.net";

      Properties props = new Properties();
      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.
      Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
	   }
         });

      try {
	   // Create a default MimeMessage object.
	   Message message = new MimeMessage(session);
	
	   // Set From: header field of the header.
	   message.setFrom(new InternetAddress(from));
	
	   // Set To: header field of the header.
	   message.setRecipients(Message.RecipientType.TO,
               InternetAddress.parse(to));
	
	   // Set Subject: header field
	   message.setSubject("Testing Subject");
	
	   // Now set the actual message
	   message.setText("Hello, this is sample for to check send " +
		"email using JavaMailAPI ");

	   // Send message
	   Transport.send(message);

	   System.out.println("Sent message successfully....");

      } catch (MessagingException e) {
         throw new RuntimeException(e);
      }
   }
}

由於我們使用的是由主機提供商JangoSMTP 提供 SMTP服務器,我們需要驗證用戶名和密碼。javax.mail.PasswordAuthentication類用於驗證的密碼。

編譯並運行

現在,我們類是準備好了,讓我們編譯上麵的類。已經保存了類SendEmail.java 到目錄:/home/manisha/JavaMailAPIExercise。我們需要jar文件 javax.mail.jar andactivation.jar 在 classpath中。執行下麵的命令從命令提示符編譯類(jars被放置在/home/manisha/ 目錄下):

javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendEmail.java

現在,這個類被編譯,執行下麵的命令來運行:

java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendEmail

驗證輸出

你應該看到下麵的消息命令控製台上:

Sent message successfully....

因為我通過JangoSMTP發送郵件到我的Gmail地址,下麵的郵件會在我的Gmail帳戶的收件箱中接收:

JavaMail API Send Email