Reputation: 236
I want to send a mail which has both calendar invite as well as message body which is a html content, I tried the following code but the mail that is sent has html content as file attachment , can someone please tell me where am I wrong
public class MailSender
{
public static void sendMail(String emailId,String subject,String mailBody)
{
PropertyWriter pw=new PropertyWriter();
try
{
pw.instantiateReadObjects();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
String smtpHost=pw.getHostNameMail();
String smtpPort=pw.getPortNumber();
final String fromAddress=pw.getEmailSource();
final String password=pw.getEmailPassword();
String SSL_FACTORY="javax.net.ssl.SSLSocketFactory";
Properties props=new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
props.put("mail.debug","true");
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.socketFactory.port",smtpPort);
props.put("mail.smtp.socketFactory.class",SSL_FACTORY);
props.put("mail.smtp.starttls.enable","ture");
props.put("mail.smtp.socketFactory.fallback","false");
Session ses=Session.getDefaultInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(fromAddress,password);
}
});
ses.setDebug(true);
Message msg=new MimeMessage(ses);
InternetAddress addressFrom=new InternetAddress(fromAddress);
msg.setFrom(addressFrom);
InternetAddress addressTo=new InternetAddress(emailId);
msg.setRecipient(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
StringBuffer sb = new StringBuffer();
System.out.println(mailBody);
StringBuffer buffer = sb.append("BEGIN:VCALENDAR\n" +
"PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n" +
"VERSION:2.0\n" +
"METHOD:REQUEST\n" +
"BEGIN:VEVENT\n" +
"ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:"+emailId+"\n" +
"ORGANIZER:MAILTO:"+pw.getEmailSource()+"\n" +
"DTSTART:20051208T053000Z\n" +
"DTEND:20051208T060000Z\n" +
"LOCATION:+TH-1\n" +
"TRANSP:OPAQUE\n" +
"SEQUENCE:0\n" +
"UID:040000008200E00074C5B7101A82E00800000000002FF466CE3AC5010000000000000000100\n" +
" 000004377FE5C37984842BF9440448399EB02\n" +
"DTSTAMP:20051206T120102Z\n" +
"CATEGORIES:Session\n" +
"DESCRIPTION:\n\n" +
"SUMMARY:Session for tomorrow\n" +
"PRIORITY:5\n" +
"CLASS:PUBLIC\n" +
"BEGIN:VALARM\n" +
"TRIGGER:PT1440M\n" +
"ACTION:DISPLAY\n" +
"DESCRIPTION:Reminder\n" +
"END:VALARM\n" +
"END:VEVENT\n" +
"END:VCALENDAR");
// Create the message part
BodyPart messageCalendar = new MimeBodyPart();
// Fill the message
messageCalendar.setHeader("Content-Class", "urn:content- classes:calendarmessage");
messageCalendar.setHeader("Content-ID", "calendar_message");
messageCalendar.setContent(mailBody, "BAKCHODI");
messageCalendar.setDataHandler(new DataHandler(
new ByteArrayDataSource(buffer.toString(), "text/calendar")));
MimeBodyPart bc = new MimeBodyPart();
bc.setContent(mailBody,"text/html");
BodyPart messageBody = bc;
Multipart multipart = new MimeMultipart();
// Add part one
multipart.addBodyPart(messageCalendar);
multipart.addBodyPart(messageBody);
//Put parts in message
msg.setContent(multipart);
Transport.send(msg);
}
catch(Exception e)
{
}
}
}
Upvotes: 2
Views: 1418
Reputation: 44965
Try simply to add the message body first like this:
multipart.addBodyPart(messageBody);
multipart.addBodyPart(messageCalendar);
Instead of this:
multipart.addBodyPart(messageCalendar);
multipart.addBodyPart(messageBody);
Upvotes: 2