2012-08-16 156 views
2

你好我試圖通過Java發送短信我已經下載了mail.jar並設置以下的classpath 是SMTPSend.java和錯誤救了我的Java代碼PLZ指導我...發送短信

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

public class SMTPSend { 

    public SMTPSend() { 
    } 

    public void msgsend() { 

     String username = "MySMSUsername"; 
     String password = "MyPassword"; 
     String smtphost = "MySMSHost.com"; 
     String compression = "My SMS Compression Information"; 
     String from = "[email protected]"; 
     String to = "9762285104"; 
     String body = "Hello SMS World!"; 
     Transport myTransport = null; 

     try { 
      Properties props = System.getProperties(); 

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

      Session mailSession = Session.getDefaultInstance(props, null); 

      Message msg = new MimeMessage(mailSession); 

      msg.setFrom(new InternetAddress(from)); 

      InternetAddress[] address = {new InternetAddress(to)}; 

      msg.setRecipients(Message.RecipientType.TO, address); 

      msg.setSubject(compression); 

      msg.setText(body); 

      msg.setSentDate(new Date()); 

      myTransport = mailSession.getTransport("smtp"); 

      myTransport.connect(smtphost, username, password); 

      msg.saveChanges(); 

      myTransport.sendMessage(msg, msg.getAllRecipients()); 

      myTransport.close(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    public static void main(String[] argv) { 

SMTPSend smtpSend = new SMTPSend(); 

smtpSend.msgsend(); 

} 

} 

,當我運行的代碼

Exception in thread "main" java.lang.NoClassDefFounderror: SMTPSend 
+1

現在格式更糟...... – maba 2012-08-16 13:13:50

+1

請參閱http://stackoverflow.com/editing-help – 2012-08-16 13:15:13

+2

現在請將您的代碼縮進Java方式。 – 2012-08-16 13:15:46

回答

3
Properties props = new Properties(); 
props.put("mail.smtp.host", SMTP_HOST_NAME); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.debug", "true"); 
props.put("mail.smtp.port", SMTP_PORT); 
props.put("mail.smtp.socketFactory.port", SMTP_PORT); 
props.put("mail.smtp.socketFactory.class", SSL_FACTORY); 
props.put("mail.smtp.socketFactory.fallback", "false"); 

您還沒有提到使用哪個端口來發送消息我得到以下錯誤。
你必須做這樣的事情SMTP_PORT = "465"

+1

我是新來的這段代碼,你可以幫我一段代碼 – 2012-08-16 13:33:01