1
import java.security.Security;  

 import java.util.Properties;  


 import javax.mail.Message;  

 import javax.mail.NoSuchProviderException;  

 import javax.mail.Session;  
 import javax.mail.Transport;  

 import javax.mail.PasswordAuthentication;  

 import javax.mail.internet.AddressException;  

 import javax.mail.internet.InternetAddress;  

 import javax.mail.internet.MimeMessage;  



 public class SendMail {  



 public String to;  

 public String subject;  

 public String text;  



 SendMail(String to, String subject, String text){  

      this.to = to;  
      this.subject = subject;  

      this.text = text;  

     }  



public void send() throws NoSuchProviderException, AddressException{  



  try 

        {  

           Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());  

           Properties props=new Properties();  

           props.setProperty("mail.transport.protocol","smtp");  

           props.setProperty("mail.host","mail.epro-tech.com");  

           props.put("mail.smtp.auth","true");  

           props.put("mail.smtp.port","465");  

           props.put("mail.debug","true");  

           props.put("mail.smtp.socketFactory.port","465");  

           props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");  

           props.put("mail.smtp.socketFactory.fallback","false");  

           Session session=Session.getDefaultInstance(props,new GJMailAuthenticator());  

           session.setDebug(true);  
           Transport transport=session.getTransport();  

           InternetAddress addressFrom=new InternetAddress("itopstest@epro-tech.com");  

           MimeMessage message=new MimeMessage(session);  

           message.setSender(addressFrom);  

           message.setSubject(subject);  

           message.setContent(text,"text/html");  

           InternetAddress addressTo=new InternetAddress(to);  

           message.setRecipient(Message.RecipientType.TO,addressTo);  

           transport.connect();  

           Transport.send(message);  

           transport.close();  
           System.out.println("DONE");  



         }  

         catch(Exception e)  

         {  



           e.printStackTrace();  

         }  

    }  

 }  

class GJMailAuthenticator extends javax.mail.Authenticator{  

     protected PasswordAuthentication getPasswordAuthentication()  

     {  

         return new PasswordAuthentication("itopstest@epro-tech.com","Ops@890T");  



     }  

 } 



 public class Mail extends SendMail {  



     public static void main(String[] args) {  



         String to = "noreply@eprocorp.com";  

         String subject = "Test";  

         String message = "A test message";  



         SendMail SendMail = new SendMail(to, subject, message);  

                 try 

                 {  

                     SendMail.send();  

                }  

                 catch (Exception e)  

                 {  

                     //  

                 }  

     }  

 } 

收到错误

Mail.java:18: error: cannot find symbol
         SendMail SendMail = new SendMail(to, subject, message);
         ^
  symbol:   class SendMail
  location: class Mail
Mail.java:18: error: cannot find symbol
         SendMail SendMail = new SendMail(to, subject, message);
                                 ^
  symbol:   class SendMail
  location: class Mail
2 errors

任何人都可以建议我如何纠正这个

4

2 回答 2

0

代码已经包含一个构造函数

SendMail(String to, String subject, String text) {
this.to = to;
this.subject = 主题;
this.text = 文本;
}

 even at extends in the class Mail i can see the errors
于 2013-02-08T07:16:18.850 回答
0

我可以看到有问题。

  1. Mail 类扩展了没有默认 cunstructor 的 SendMail 类。所以要么在 SendMail 类中创建一个默认的 cunstructor,要么在 Mail 类中创建一个参数化 cunstructor

  2. SendMail.send();在这里我猜编译器正在尝试访问 SendMail 类的静态方法。创建您的对象SendMail sendMail = new SendMail(to, subject, message);并访问发送方法sendMail.send();

  3. 为您的两个类提供正确的包名称,因为您的类具有可以在 jar 中找到的通用名称。

于 2013-02-08T07:06:42.767 回答