我正在使用java邮件向发件人发送邮件。但我的问题是我无法在一个邮件正文中发送多个信息,只有一个信息即将到来。但我想在一封邮件中发送多个信息,这里我到目前为止
Properties props = System.getProperties();
props.put("mail.smtp.user", senderEmail);
props.put("mail.smtp.host", gmail);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
// Required to avoid security exception.
MyAuthenticator authentication = new MyAuthenticator(senderEmail,senderMailPassword);
Session session = Session.getInstance(props,authentication);
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(senderEmail));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipeintEmail));
/*message.setRecipients(Message.RecipientType.CC,InternetAddress.parse(WebConstants.AdminMail));*/
message.setSubject("Password Recovery mail");
message.setText(resetLink);
message.setText("1236547");
message.setText("Here is another text");
message.setContent(messageText, "text/html; charset=utf-8");
//message.setText(messageText);
Transport transport = session.getTransport("smtps");
transport.connect(gmail,port, senderEmail, senderMailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
我的问题是只有第一个setText工作,而不是其他两个。任何人都可以帮忙?
答案 0 :(得分:1)
每次致电
message.setText("whatever")
它会覆盖您之前设置的文字。
然后,你最后打电话给
message.setContent(messageText, "text/html; charset=utf-8");
将再次使用messageText
中存储的内容覆盖所有内容(在您的情况下显然与resetLink
的值相同)。
首先使用StringBuilder
制作邮件内容,然后将其插入邮件中,试试这样:
StringBuilder sb = new StringBuilder();
sb.append(resetLink).append(System.lineSeparator());
sb.append("1236547").append(System.lineSeparator());
sb.append("Here is another text");
message.setText(sb.toString());