简体   繁体   English

如何发送 HTML email

[英]how to send HTML email

I have to send HTML file via email but not as attachment.我必须通过 email 发送 HTML 文件,但不能作为附件发送。

Message simpleMessage = new MimeMessage(mailSession);
try {
   fromAddress = new InternetAddress(from);
   toAddress = new InternetAddress(to);

} catch (AddressException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

try {
    simpleMessage.setFrom(fromAddress);
    simpleMessage.setRecipient(RecipientType.TO, toAddress);

    simpleMessage.setSubject(subject);
    simpleMessage.setText(text);

    Transport.send(simpleMessage);
} catch (MessagingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

It is sending email simply with text message.它只是通过短信发送 email。 I want to send HTML content which is stored in another file but not as attachment我想发送存储在另一个文件中但不作为附件的 HTML 内容

Don't upcast your MimeMessage to Message :不要将您的MimeMessage上传到Message

MimeMessage simpleMessage = new MimeMessage(mailSession);

Then, when you want to set the message body, either call然后,当你想设置消息正文时,要么调用

simpleMessage.setText(text, "utf-8", "html");

or call或打电话

simpleMessage.setContent(text, "text/html; charset=utf-8");

If you'd rather use a charset other than utf-8 , substitute it in the appropriate place.如果您更愿意使用utf-8以外的字符集,请将其替换在适当的位置。

JavaMail has an extra, useless layer of abstraction that often leaves you holding classes like Multipart , Message , and Address , which all have much less functionality than the real subclasses ( MimeMultipart , MimeMessage , and InternetAddress ) that are actually getting constructed... JavaMail 有一个额外的、无用的抽象层,它通常会让您持有MultipartMessageAddress ,这些类的功能都比实际MimeMultipart的真正子类( MimeMultipartMimeMessageInternetAddress )少得多......

Here is my sendEmail java program.这是我的 sendEmail java 程序。 It's good to use setContent method of Message class object.最好使用消息 class object 的 setContent 方法。

message.setSubject(message_sub);
message.setContent(message_text, "text/html; charset=utf-8");

sendEmail.java发送电子邮件.java

package employee;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class SendEmail {

    public static void sendEmail(String message_text, String send_to,String message_sub ) throws UnsupportedEncodingException {

        
        final String username = "hello@xyz.com";
        final String password = "password";

        Properties prop = new Properties();
        prop.put("mail.smtp.host", "us2.smtp.mailhostbox.com"); //replace your host address.
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true"); //TLS
        
        Session session = Session.getInstance(prop,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("sender@xyz.com", "Name from which mail has to be sent"));
            message.setRecipients(
                    Message.RecipientType.TO,
                    InternetAddress.parse(send_to)
            );
            message.setSubject(message_sub);
            message.setContent(message_text, "text/html; charset=utf-8");
            
          

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM