[英]JavaMail: How to send a variable value as email content instead of just text?
I have the cut following code below, for emailing something to me. 我有下面的剪切代码,用于通过电子邮件发送给我。
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("emailfrom@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("emailto@gmail.com"));
message.setSubject("Email title");
message.setText("Email body");
Transport.send(message);
It works fine when i put the message.setText line to something in quotations "", eg. 当我将message.setText行放在引号“”中时,例如,它工作正常。 message.setText("Hello there");
message.setText(“ Hello there”);
But the setText part is underlined in red whenever I try to use a variable name, eg. 但是,每当我尝试使用变量名时,setText部分就会用红色加下划线。
String x = Hello;
String y = there;
and use the above code with the line: 并在行中使用以上代码:
message.setText(x + y);
With the desired message to say "Hello there", using the variables instead of the quoted text. 用所需的消息说“ Hello there”,使用变量而不是引用的文本。
How do i do this? 我该怎么做呢?
setText
should accept any String
instance. setText
应该接受任何String
实例。 It's possible you were getting an error because you didn't add quotes around "Hello" and "there"; 您可能会遇到错误,因为您没有在“ Hello”和“ there”周围添加引号;
String x = "Hello";
String y = "there";
message.setText(x + " " + y);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.