位置:首页 > 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