使用JavaSpring发送html电子邮件?

cfh9epnr  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(326)

我使用以下代码使用JavaSpring发送电子邮件。我可以发送一个正常的短信成功使用此代码。但是我添加了下面的部分(///////),以便发送html电子邮件。在那之后消息就不发送了(您可以看到我在下面添加了/////////来分隔代码。以上部分工作正常)

public void sendUserRegisterEmail(String receiver, String receiverEmailAddress){
            MimeMessagePreparator preparator = new MimeMessagePreparator() {
                public void prepare(MimeMessage mimeMessage) throws Exception {
                    MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                    message.setSubject(USER_REGISTER_MESSAGE_SUBJECT);
                    message.setTo(receiverEmailAddress);
                    message.setFrom(SENDER_EMAIL_ADDRESS);
                    message.setText(String.format(USER_REGISTER_MESSAGE_BODY, receiver));

                    //////////////////////////////////////////////////////////////////////
                    Properties properties= getProperties();
                    Session session = Session.getDefaultInstance(properties);
                    MimeMessage msg = new MimeMessage(session);
                    msg.setFrom(new InternetAddress(SENDER_EMAIL_ADDRESS, "Admin"));
                    msg.addRecipient(Message.RecipientType.TO,
                            new InternetAddress(receiverEmailAddress, "user"));

                    msg.setSubject(USER_REGISTER_MESSAGE_SUBJECT,"UTF-8");

                    Multipart mp = new MimeMultipart();
                    MimeBodyPart htmlPart = new MimeBodyPart();
                    htmlPart.setContent(message,"E:\\test.html");
                    mp.addBodyPart(htmlPart);
                    msg.setContent(mp);
                    Transport.send(msg);

                }
            };
            sendEmail(preparator);
        }

这是我运行代码后的控制台。但我也看不到任何错误。我使用itellijide和jetty作为服务器。

DEBUG: JavaMail version 1.5.6
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map

编辑:我也遵循这个例子,并试图发送一个html电子邮件。但我无法将其与我的代码集成。
这是我的html文件

<html>
this is s HTML text
</html>
k2arahey

k2arahey1#

您可以尝试在string或stringbuffer变量assume htmcontent中初始化html文件,并将其作为setcontent type方法中的第一个参数提供。 htmlPart.setContent(htmContent.toString(), "text/html");

8aqjt8rx

8aqjt8rx2#

好的,试试我的代码片段,它们正在工作。
你甚至可以把它放在一个地方,而不用spring ioc,f.e.into main()方法并运行。
配置(bean声明)

@Bean(name = "javaMailSender")
  public JavaMailSender javaMailSender(@Value("${mail.smtp.host}") String smtpHost,
                                       @Value("${mail.smtp.port}") int smtpPort,
                                       @Value("${mail.name}") String mailName,
                                       @Value("${mail.cred}") String mailCred) {
    Properties props = new Properties();
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");

    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost(smtpHost);
    javaMailSender.setPort(smtpPort);
    javaMailSender.setUsername(mailName);
    javaMailSender.setPassword(mailCred);
    javaMailSender.setJavaMailProperties(props);
    return javaMailSender;
  }

电子邮件服务

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@Service("mailNotificationService")
public class MailNotificationService {
  @Value("${mail.name}")
  String mailFrom;

  @Resource
  private JavaMailSender javaMailSender;

  public void notify(String to, String subject, String message) {
    try {
      InternetAddress[] parsed;
      try {
        parsed = InternetAddress.parse(to);
      } catch (AddressException e) {
        throw new IllegalArgumentException("Not valid email: " + to, e);
      }

      MimeMessage mailMessage = javaMailSender.createMimeMessage();
      mailMessage.setSubject(subject, "UTF-8");

      MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "UTF-8");
      helper.setFrom(mailFrom);
      helper.setTo(parsed);
      helper.setText(message, true);

      javaMailSender.send(mailMessage);
    } catch (MessagingException ex) {
      throw new RuntimeException(ex);
    }
  }
}

相关问题