邮件正文具有标题和零件信息

时间:2018-10-17 07:27:48

标签: java email javamail

当数据库中的值更改时,我们必须将电子邮件发送到通讯组。我有适当的代码。与以下链接中的代码相同-

[https://www.tutorialspoint.com/javamail_api/javamail_api_send_html_in_email.htm][1]

为便于参考,我将在此处添加代码-

    import java.util.Properties;

    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;

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

public class SendHTMLEmail {
   public static void main(String[] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "destinationemail@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "fromemail@gmail.com";
      final String username = "manishaspatil";//change accordingly
      final String password = "******";//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = "relay.jangosmtp.net";

      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", "25");

      // Get the Session object.
      Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
            }
    });

      try {
            // Create a default MimeMessage object.
            Message message = new MimeMessage(session);

       // Set From: header field of the header.
       message.setFrom(new InternetAddress(from));

       // Set To: header field of the header.
       message.setRecipients(Message.RecipientType.TO,
              InternetAddress.parse(to));

       // Set Subject: header field
       message.setSubject("Testing Subject");

       // Send the actual HTML message, as big as you like
       message.setContent(
              "<h1>This is actual message embedded in HTML tags</h1>",
             "text/html");

       // Send message
       Transport.send(message);

       System.out.println("Sent message successfully....");

      } catch (MessagingException e) {
       e.printStackTrace();
       throw new RuntimeException(e);
      }
   }
}

从我的本地设置来看,这很好。但是,从服务器上,即使标题信息也会打印在电子邮件中,并且主题行也会被剥离。我搜索了互联网,寻找可能的修复方法。也没有太多代码可以调试。如果您能给我一些帮助,那将非常有帮助。这是电子邮件的外观-

> ----=_Part_1_12345.566 Content-Type:text/plain; charset=us-ascii Content-Transfer-Encoding:7bit
> 
> <<Actual Body>>
> ----=_Part_1_12345.566

0 个答案:

没有答案