java - 通过java发送短信

标签 java sms

嗨,我正在尝试通过 java 发送短信,我已经下载了 mail.jar 并设置为类路径 下面是我的 SMTPSend.java 保存的 java 代码,错误请指导我...

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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7dacee4fae4e2c4d2c5d9d6dad2f7facee4fae4ffd8c4c399d4d8da" rel="noreferrer noopener nofollow">[email protected]</a>";
        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

最佳答案

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" 的操作。

关于java - 通过java发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11987877/

相关文章:

php - 在手机短信中添加换行符

java - 我需要一种方法来操作 JTextArea 中的最后一个字符(通过删除它)

Java Clock 模拟,不知道如何进行 do...while

javascript - 在 Meteor 中通过 Amazon SNS 向电话号码发送短信

ios - 如何防止 iOS 消息应用裁剪粘贴的图像?

android - 如何获取android中已读消息的数量?

java - 通过中兴通讯 GSM 调制解调器将短信立即转发到电脑

java - 实现 Map<MyObject,ArrayList<MyObject>> 的正确方法

java - 获取 { 和 } 之间的所有文本(源文本包含\n)

java - 将类型传递给方法并更改响应