如何将 HTML 链接放入电子邮件正文中?
我有一个可以发送邮件的应用程序,用 Java 实现。我想在邮件中放置一个 HTML 链接,但该链接显示为普通字母,而不是 HTML 链接... 我怎样才能将 HTML 链接放入字符串中?我需要特殊字符吗?非常感谢
更新: 大家好!感谢oyu的解答!这是我的代码:
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;
}
}
这里是正文字符串:
String link = "<a href=\"WWW.google.es\">ACTIVAR CUENTA</a>";
但是在收到的消息中,链接显示为链接字符串,而不是 HTML 超链接!我不明白发生了什么...
有什么解决办法吗?
I have an application that 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
添加链接就像在字符串中添加
text
一样简单。您应该将您的电子邮件设置为支持html(这取决于您使用的库),并且在发送之前不应该转义您的电子邮件内容。更新:由于您使用的是
java.mail
,因此您应该这样设置文本:html
是 mime 子类型(这将导致text/html)。
setText(string)
方法使用的默认值为plain
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:html
is the mime subtype (this will result intext/html
). The default value that is used by thesetText(string)
method isplain
我只是要回答,以防这对其他人不起作用。
我尝试了 Bozho 的方法,但由于某种原因,当我对整个邮件执行
setText
时,电子邮件无法发送。我尝试过
,但这是 Outlook 中的附件,而不是通常的文本。为了解决这个问题,在 Outlook 中,我没有执行 mbp.setContent 和 message.setText,而是在邮件正文部分执行了一个 setText。即:
我的消息代码如下所示:
I'm just going to answer in case this didn't work for someone else.
I tried Bozho's method and for some reason the email wouldn't send when I did the
setText
on the message as a whole.I tried
but this came as an attachment in Outlook instead of in the usual text. To fix this for me, and in Outlook, instead of doing the mbp.setContent and message.setText, I just did a single setText on the message body part. ie:
With my code for the message looking like this:
在 URL 之前添加“http://”对我有用。
Appending "http://" before the URL worked for me.
我们可以使用 java 代码在电子邮件正文中创建 html 链接。在我的例子中,我正在开发重置密码,我应该在其中创建链接并通过邮件发送给用户。您将创建一个字符串。在字符串中输入所有url。如果将 http 添加到 .it 的行为就像邮件中的链接一样。
Ex:String mailBody ="http://localhost:8080/Mail/verifytoken?token="+ token ;
您可以通过添加查询字符串来发送带有 url 的值。她的令牌具有一些加密值。
将 mailBody 放入邮件正文参数中。
ex": "嗨 "+userdata.getFirstname()+
“\n\n 您已向 X 应用程序请求新密码。请使用下面的链接登录。”+
"\n\n Click on Link: "+mailBody);
上面是您必须传递给邮件服务的参数字符串。电子邮件服务接受诸如 from、to、subject、body 之类的参数。这里我已经给了正文应该如何。你根据你的演员表传递 from 、to、subject 值
We can create html link in the email body by using java code.In my case I am developing reset password where I should create link and send to the user through the mail.you will create one string.With in a string you type all the url.If you add the http to the that .it behaves like link with in the mail.
Ex:String mailBody ="http://localhost:8080/Mail/verifytoken?token="+ token ;
you can send some value with url by adding query string.Her token has some encrypted value.
put mailBody in your mail body parameter.
ex": "Hi "+userdata.getFirstname()+
"\n\n You have requested for a new password from the X application. Please use the below Link to log in."+
"\n\n Click on Link: "+mailBody);
The above is the string that is parameter that you have to pass to your mail service.Email service takes parameters like from,to,subject,body.Here I have given body how it should be.you pass the from ,to,subject values according to your cast
你可以采取对我有用的正确方式。
you can do right way it is working for me.