【发布时间】:2014-04-09 11:25:42
【问题描述】:
我目前有一个程序可以根据用户在我的数据库中的信息向他们发送电子邮件。电子邮件以 html 构建,并作为内容类型 text/html 发送给用户电子邮件。我想试试看是否可以使用电子邮件格式##########@domain.com 以某种方式将此消息发送到他们的手机。
显然手机无法接收 HTML 消息,所以我尝试了这个:
-删除了 html 并发送了纯文本,这确实有效,但是对于 Verizon(我测试过的唯一服务提供商),文本被截断并且完整的消息从未发送。我只收到了消息的第一部分。
然后我想知道是否有可能以某种方式“截屏”html 消息,然后简单地将 html 显示的图片发送到手机。
这是我当前发送电子邮件的代码:
public static void email(String content, String address) {
final String username = "email";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
Address[] a = InternetAddress.parse("myemail");
message.setReplyTo(a);
message.setHeader("From: ", "Movie Alert");
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(address));
if (ShowFinder.showsFound > 1) message.setSubject("Movie Alert: " + ShowFinder.showsFound + " New Shows Found!");
else if (ShowFinder.showsFound == 1) message.setSubject("Movie Alert: " + ShowFinder.showsFound + " New Show Found!");
else message.setSubject("Unsubscribed");
StringBuilder sb = new StringBuilder();
sb.append(content);
message.setContent(sb.toString(), "text/html");
Transport.send(message);
System.out.println("Sent Email");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
总之,我有以下问题:
-文字被截断的原因是我没有正确发送电子邮件,还是因为服务提供商?
-是否可以通过短信发送基于html代码的html显示截图?
谢谢!
【问题讨论】:
-
只是猜测,但您是否尝试过 text/plain 而不是 text/html?或者只使用
setText(sb.toString()) -
我尝试使用 setText 和 text/plain 得到相同的结果
标签: java html email jakarta-mail