如何将 HTML 链接放入电子邮件正文中?

发布于 2024-10-27 21:40:41 字数 1515 浏览 1 评论 0原文

我有一个可以发送邮件的应用程序,用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

韶华倾负 2024-11-03 21:40:42

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

更新:由于您使用的是 java.mail,因此您应该这样设置文本:

message.setText(body, "UTF-8", "html");

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:

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

墨洒年华 2024-11-03 21:40:42

我只是要回答,以防这对其他人不起作用。
我尝试了 Bozho 的方法,但由于某种原因,当我对整个邮件执行 setText 时,电子邮件无法发送。

我尝试过

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

,但这是 Outlook 中的附件,而不是通常的文本。为了解决这个问题,在 Outlook 中,我没有执行 mbp.setContent 和 message.setText,而是在邮件正文部分执行了一个 setText。即:

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

我的消息代码如下所示:

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

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

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:

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);
拥抱我好吗 2024-11-03 21:40:42

在 URL 之前添加“http://”对我有用。

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

明媚如初 2024-11-03 21:40:42

我们可以使用 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

晚雾 2024-11-03 21:40:42

你可以采取对我有用的正确方式。

 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 = "[email protected]";      
           String from = "[email protected]";
           final String username = "[email protected]";
           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);
     }
  }

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 = "[email protected]";      
           String from = "[email protected]";
           final String username = "[email protected]";
           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);
     }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文