如何在邮件正文中添加HTML链接?

时间:2022-02-13 08:04:39

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

我有一个可以发送邮件的应用,用Java实现。我想在de mail里面放一个HTML链接,但是这个链接是普通的字母,而不是HTML链接……如何在HTML链接中插入字符串?我需要特殊字符吗?非常感谢你

Update: HI evereybody! thanks for oyu answers! Here is my code:

更新:嗨evereybody !感谢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;
    }
}

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...

但是在接收到的消息中,链接显示为链接字符串,而不是HTML超链接!我不明白发生了什么……

Any solution?

有解决方案吗?

5 个解决方案

#1


16  

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.

添加链接就像添加 < / >在字符串文本。您应该将电子邮件设置为支持html(这取决于您使用的库),并且在发送电子邮件之前不应该转义电子邮件内容。

Update: since you are using java.mail, you should set the text this way:

更新:因为您正在使用java。发邮件时,应将文本设置为:

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是mime子类型(这会导致文本/html)。setText(string)方法使用的默认值是plain

#2


7  

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.

我要回答的是,这对其他人没用。我尝试了Bozho的方法,出于某种原因,当我把消息作为一个整体来处理时,邮件不会发送。

I tried

我试着

MimeBodyPart mbp = new MimeBodyPart(); 
mbp.setContent(body, "text/html"); 

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:

但这只是Outlook中的附件,而不是通常的文本。为我和Outlook解决这个问题,而不是做mbp。setContent和消息。setText,我只是在消息主体部分做了一个setText。即:

MimeBodyPart mbp = new MimeBodyPart(); 
mbp.setText(messageBody,"UTF-8", "html");

With my code for the message looking like this:

我的消息代码是这样的:

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
for(String str : to){
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(str));
}        
message.setSubject(subject);
// Create the message part 
MimeBodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText(messageBody,"UTF-8","html");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send(message);

#3


2  

Appending "http://" before the URL worked for me.

在URL对我起作用之前加上“http://”。

#4


1  

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.

我们可以使用java代码在邮件主体中创建html链接。在我的情况下,我正在开发重置密码,我应该创建链接并通过邮件发送给用户。您将创建一个字符串。在字符串中输入所有url。如果您将http添加到它,它的行为就像邮件中的链接。

Ex:String mailBody ="http://localhost:8080/Mail/verifytoken?token="+ token ;

例:字符串mailBody = " http://localhost:8080 /邮件/ verifytoken吗?令牌= " +令牌;

you can send some value with url by adding query string.Her token has some encrypted value.

您可以通过添加查询字符串的url发送一些值。她的令牌有一些加密的值。

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);

将mailBody放到邮件主体参数中。例:“Hi”+userdata.getFirstname()+“\n\n”您已经向X应用程序请求了一个新的密码。请使用以下链接登录。“+”\n\n单击链接:“+邮件正文”;

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

上面的字符串是您必须传递给邮件服务的参数。电子邮件服务接受从主体到主体的参数。在这里,我已经告诉了肉体它应该是什么样子。你可以根据你的演员阵容来传递你的主题。

#5


0  

you can do right way it is working for me.

你可以用正确的方式让它对我有效。

 public  class SendEmail
 {
   public void getEmail(String to,String from, String userName,String password,Properties props,String subject,String messageBody)
  {
      MimeBodyPart mimeBodyPart=new MimeBodyPart();
      mimeBodyPart.setContent(messageBody,"text/html");
      MimeMultipart multipart=new MimeMultipart();
      multipart.addBodyPart(mimeBodyPart);
      Session session=Session.getInstance(props,new Authenticator()
        {
          protected PasswordAuthentication getPasswordAuthentication()
           {
              return new PasswordAuthentication(userName,password);
           }
       });
         try{
              MimeMessage message=new MimeMessage(session);
              message.setFrom(new InternetAddress(from));
              message.setContent(multipart);
              message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
             message.setSubject("Have You got Mail!");
             message.setText(messageBody,"UTF-8","html");
             Transport.send(message);
           }
           catch(MessagingException ex){System.out.println(ex)}
      public static void main(String arg[]){
         SendEmail sendEmail=new SendEmail();
           String to = "XXXXXXX@gmail.com";      
           String from = "XXXXXXXX@gmail.com";
           final String username = "XXXXX@gmail.com";
           final String password = "XXXX";
            String subject="Html Template";

          String body = "<i> Congratulations!</i><br>";
          body += "<b>Your Email is working!</b><br>";
          body += "<font color=red>Thank </font>";
          String host = "smtp.gmail.com";
          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", "587");
          sendEmail.getEmail(to,from,username,password,props,subject,body);
     }
  }

#1


16  

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.

添加链接就像添加 < / >在字符串文本。您应该将电子邮件设置为支持html(这取决于您使用的库),并且在发送电子邮件之前不应该转义电子邮件内容。

Update: since you are using java.mail, you should set the text this way:

更新:因为您正在使用java。发邮件时,应将文本设置为:

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是mime子类型(这会导致文本/html)。setText(string)方法使用的默认值是plain

#2


7  

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.

我要回答的是,这对其他人没用。我尝试了Bozho的方法,出于某种原因,当我把消息作为一个整体来处理时,邮件不会发送。

I tried

我试着

MimeBodyPart mbp = new MimeBodyPart(); 
mbp.setContent(body, "text/html"); 

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:

但这只是Outlook中的附件,而不是通常的文本。为我和Outlook解决这个问题,而不是做mbp。setContent和消息。setText,我只是在消息主体部分做了一个setText。即:

MimeBodyPart mbp = new MimeBodyPart(); 
mbp.setText(messageBody,"UTF-8", "html");

With my code for the message looking like this:

我的消息代码是这样的:

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
for(String str : to){
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(str));
}        
message.setSubject(subject);
// Create the message part 
MimeBodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText(messageBody,"UTF-8","html");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send(message);

#3


2  

Appending "http://" before the URL worked for me.

在URL对我起作用之前加上“http://”。

#4


1  

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.

我们可以使用java代码在邮件主体中创建html链接。在我的情况下,我正在开发重置密码,我应该创建链接并通过邮件发送给用户。您将创建一个字符串。在字符串中输入所有url。如果您将http添加到它,它的行为就像邮件中的链接。

Ex:String mailBody ="http://localhost:8080/Mail/verifytoken?token="+ token ;

例:字符串mailBody = " http://localhost:8080 /邮件/ verifytoken吗?令牌= " +令牌;

you can send some value with url by adding query string.Her token has some encrypted value.

您可以通过添加查询字符串的url发送一些值。她的令牌有一些加密的值。

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);

将mailBody放到邮件主体参数中。例:“Hi”+userdata.getFirstname()+“\n\n”您已经向X应用程序请求了一个新的密码。请使用以下链接登录。“+”\n\n单击链接:“+邮件正文”;

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

上面的字符串是您必须传递给邮件服务的参数。电子邮件服务接受从主体到主体的参数。在这里,我已经告诉了肉体它应该是什么样子。你可以根据你的演员阵容来传递你的主题。

#5


0  

you can do right way it is working for me.

你可以用正确的方式让它对我有效。

 public  class SendEmail
 {
   public void getEmail(String to,String from, String userName,String password,Properties props,String subject,String messageBody)
  {
      MimeBodyPart mimeBodyPart=new MimeBodyPart();
      mimeBodyPart.setContent(messageBody,"text/html");
      MimeMultipart multipart=new MimeMultipart();
      multipart.addBodyPart(mimeBodyPart);
      Session session=Session.getInstance(props,new Authenticator()
        {
          protected PasswordAuthentication getPasswordAuthentication()
           {
              return new PasswordAuthentication(userName,password);
           }
       });
         try{
              MimeMessage message=new MimeMessage(session);
              message.setFrom(new InternetAddress(from));
              message.setContent(multipart);
              message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
             message.setSubject("Have You got Mail!");
             message.setText(messageBody,"UTF-8","html");
             Transport.send(message);
           }
           catch(MessagingException ex){System.out.println(ex)}
      public static void main(String arg[]){
         SendEmail sendEmail=new SendEmail();
           String to = "XXXXXXX@gmail.com";      
           String from = "XXXXXXXX@gmail.com";
           final String username = "XXXXX@gmail.com";
           final String password = "XXXX";
            String subject="Html Template";

          String body = "<i> Congratulations!</i><br>";
          body += "<b>Your Email is working!</b><br>";
          body += "<font color=red>Thank </font>";
          String host = "smtp.gmail.com";
          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", "587");
          sendEmail.getEmail(to,from,username,password,props,subject,body);
     }
  }