Reputation: 4532
this is what is my java code to send mail but when i send using this code everything is sent except body text.
this body text is not getting displayed in outlook, here is my code .
package samples;
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("[email protected]", "[email protected]", "Please apply for leave for the following dates", mailBody.toString());
}
}
Please help me to resolve this issue.
Regards Tony
Upvotes: 0
Views: 881
Reputation: 5798
try setting body like this:
MimeBodyPart messageBodyPart = new MimeBodyPart();
//fill message
messageBodyPart.setContent(bodytext, type.type);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Upvotes: 1