我已经编写了必要的代码,以便将电子邮件发送到我的Exchange 2010服务器,该服务器将转发到SMS网关到手机。我确实更改了这篇文章的所有用户,主机和其他相关数据,但代码工作正常,我收到短信。 (所以Exchange端配置没问题)唯一的问题是我的短信内容在75个字符后被切断。
我已经读过MimeMessage标题不能超过那些75个字符,但我发送的不是标题(也就是主题,它是正文) (又名内容)?无论如何,我试图绕过其他限制,比如一行中的78个字符,试图使用我想到的可以做CR和/或LF的任何事情(\ n \ r,(char)10+ (char)13),短信内容在75个字符后仍然被截断。
我有一个解决方法,只需发送多个短信,将主体截断为需要的尽可能多的消息,但如果有另一种方法可以将更长的文本发送到一个单件中,我很高兴知道。 / p>
这是我的实际代码:
String host = "xxxxxxxxxxxxxxx";
String user = "xxxxxxxxxxxxxxxxx";
String passwd = "xxxxxxxxxxxxxxxx";
StringBuilder body = new StringBuilder("This is supposed to be a very long message to try how many characters I can send from Java as SMS through Exchange. Is it long enough? Cause it will cut at 75 characters. ");
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.from", "xxxxxxxxxxxxxx");
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.transport.protocol", "smtp");
Authenticator authenticator = new Authenticator();
props.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
Session session = Session.getInstance(props, authenticator);
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress("xxxxxxxxxxxxxxxxx"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxxxxxxxxxxxxxxxxx"));
message.setContent(body.toString(), "text/plain");
message.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
transport.connect(host, 25, user, passwd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}