如何将HTML链接放入电子邮件正文内? [英] How Can I put a HTML link Inside an email body?
问题描述
我该如何做到HTML链接到一个String?我需要特殊字符?非常感谢你
更新:
HI evereybody!谢谢你的答案!这是我的代码:
public static boolean sendMail(Properties props,String to,String from,
String password, String subject,String body)
{
try
{
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(body,text / html);
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(mbp);
// Preparamos la sesion
会话session = Session.getDefaultInstance(props);
// Construimos el mensaje
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setContent(multipart);
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
message.setText(body);
// Lo enviamos。
传输t = session.getTransport(smtp);
t.connect(from,password);
t.sendMessage(message,message.getAllRecipients());
// Cierre。
t.close();
返回true;
}
catch(异常e)
{
e.printStackTrace();
返回false;
}
}
这里的主体字符串:
String link =< a href = \WWW.google.es\> ACTIVAR CUENTA< / a>;
但是在收到的消息中,链接显示为链接字符串,而不是HTML超链接!我不明白会发生什么...
任何解决方案?
添加链接与在字符串中添加< a href =..> text< / a>
一样简单。您应该将您的电子邮件设置为支持html(这取决于您使用的库),您不应该在发送邮件之前转出您的电子邮件内容。
更新:自从你正在使用 java.mail
,您应该以这种方式设置文本:
message.setText(body,UTF-8,html);
html
是mime子类型导致 text / html
)。 setText(string)
方法使用的默认值为 plain
I have an application thats can send mails, implemented in Java. I want to put a HTML link inside de mail, but the link appears as normal letters, not as HTML link... How can i do to inside the HTML link into a String? I need special characters? thank you so much
Update: HI evereybody! thanks for oyu answers! Here is my code:
public static boolean sendMail(Properties props, String to, String from,
String password, String subject, String body)
{
try
{
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(body, "text/html");
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(mbp);
// Preparamos la sesion
Session session = Session.getDefaultInstance(props);
// Construimos el mensaje
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setContent(multipart);
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
message.setText(body);
// Lo enviamos.
Transport t = session.getTransport("smtp");
t.connect(from, password);
t.sendMessage(message, message.getAllRecipients());
// Cierre.
t.close();
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
And here the body String:
String link = "<a href=\"WWW.google.es\">ACTIVAR CUENTA</a>";
But in the received message the link appears as the link string, not as HTML hyperlink! I don't understand what happens...
Any solution?
Adding the link is as simple as adding the <a href="..">text</a>
inside the string. You should set your email to support html (it depends on the library you are using), and you should not escape your email content before sending it.
Update: since you are using java.mail
, you should set the text this way:
message.setText(body, "UTF-8", "html");
html
is the mime subtype (this will result in text/html
). The default value that is used by the setText(string)
method is plain
这篇关于如何将HTML链接放入电子邮件正文内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!