0

这是我发送邮件的 java 代码,但是当我使用此代码发送时,除了正文之外的所有内容都被发送。

此正文未在 Outlook 中显示,这是我的代码。

包装样品;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
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 SendMail {
    public void sendMail(String m_from,String m_to,String m_subject,String m_body){
      try {
            Session m_Session;
            Message m_simpleMessage;
            InternetAddress m_fromAddress;
            InternetAddress m_toAddress;
            Properties m_properties;

            m_properties     = new Properties();
            m_properties.put("mail.smtp.host", "fffffff"); 
            m_properties.put("mail.smtp.auth", "true");
            m_properties.put("mail.smtp.starttls.enable", "true");
            m_properties.put("mail.smtp.port", "587");

            m_Session=Session.getDefaultInstance(m_properties,new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("anthony.savarimut","1wr345d@1"); // username and the password
                }
            });
            m_Session.setDebug(true);
            m_simpleMessage  =   new MimeMessage(m_Session);
            m_fromAddress    =   new InternetAddress(m_from);
            m_toAddress      =   new InternetAddress(m_to);

            m_simpleMessage.setFrom(m_fromAddress);
            m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
            m_simpleMessage.setSubject(m_subject);

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

            //m_simpleMessage.setContent(m_body,"text/plain");

            Transport.send(m_simpleMessage);
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }
    public static void main(String[] args) {
      SendMail send_mail    =   new SendMail();
      String empName = "Antony Raj S";
      String title ="<b>Hi !"+empName+"</b>";
      StringBuffer mailBody = new StringBuffer();
        mailBody.append("<b><Hi  "+empName+">");
        mailBody.append("<br>");
        mailBody.append("<Please Apply Leave for the following date(s  )"+empName+">");
        mailBody.append("<br>");
        mailBody.append("<Regards>");
        mailBody.append("<br>");
        mailBody.append("<HR Team>");
        mailBody.append("<br>");
        mailBody.append("<Slingmedia>");


      send_mail.sendMail("anthony.savarimut@xxxxxx.com", "anthony.savarimut@yyyyy.com", "Please apply for leave for the following dates", mailBody.toString());
    }
}

请帮我解决这个问题。

问候托尼

4

1 回答 1

1

尝试像这样设置身体:

MimeBodyPart messageBodyPart = new MimeBodyPart();
//fill message
messageBodyPart.setContent(bodytext, type.type);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
于 2012-06-18T10:40:33.163 回答