您好我正在尝试通过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 = "mySMSUsername@MySMSHost.com";
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
答案 0 :(得分: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"
。