试图发送电子邮件,收到错误说“此导入java.mail”无法解决

时间:2014-10-27 22:04:55

标签: java

我正在尝试编写一个简单的程序,使用eclipse ide发送邮件,买我无法导入图书馆的邮件。 就行了 import javax.mail。*;

我收到错误

"this import java.mail" cannot be resolved"

代码来自 http://www.tutorialspoint.com/java/java_sending_email.htm

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

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

      // Sender's email ID needs to be mentioned
      String from = "web@gmail.com";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }

1 个答案:

答案 0 :(得分:1)

JavaMail不附带Java SE。您可以从Oracle here下载它。