Announcement Announcement Module
Collapse
No announcement yet.
impossible to send a mail in HTML format Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • flamant
    started a topic impossible to send a mail in HTML format

    impossible to send a mail in HTML format

    Hi,
    I am not sure that this thread is posted in the right forum, anyway here is my problem :
    I try to send a message in HTML format. Here is the code :

    Code:
    public void sendMessage(String subject, String message, String to, String cc, String bcc) throws MessagingException  {
        	
        	MimeMessage mimeMessage = mailSender.createMimeMessage();
        	MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            if(to!=null && !to.equals("")){
            	mimeMessageHelper.setTo(to);
            }
            if(cc!=null && !cc.equals("")){
            	mimeMessageHelper.setCc(cc);
            }
            if(bcc!=null && !bcc.equals("")){
            	mimeMessageHelper.setBcc(bcc);
            }
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(message,true);
            mimeMessageHelper.setFrom(from);
            logger.info("host = "+mailSender.getHost());
            logger.info("Port = "+mailSender.getPort());
            //logger.info("UserName = "+mailSender.getUsername());
            //logger.info("Password = "+mailSender.getPassword());
           	mailSender.send(mimeMessage);
        }

    and the message is the following :

    Code:
    String message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"<URL>">"+ "<htmlxmlns=\"<URL>\"><head><head/><body>"
    +messageSource.getMessage("messagetosend.part1", null, Locale.ENGLISH)+ user.getFirstName()+" "+ user.getLastName()+"<br/>"
    + messageSource.getMessage("messagetosend.part2", null, Locale.ENGLISH)+<br/>"
    + messageSource.getMessage("messagetosend.part3", null, Locale.ENGLISH)+ newPassword+<br/>"
    + messageSource.getMessage("messagetosend.part4", null, Locale.ENGLISH)+<br/>"
    + messageSource.getMessage("messagetosend.part5", null, Locale.ENGLISH)+<br/>"
    + messageSource.getMessage("homepage.internet.address", null, Locale.ENGLISH)+"</body></html>";

    according to what I found on internet, the method MimeMessageHelper.setText(message,true); specifies that the message is sent in HTML format ( second argument is boolean "true" saying it is a HTML message ).
    But I don't get the message in HTML format. Anyone can help me ? thank you in advance
    Last edited by flamant; Oct 28th, 2008, 01:51 AM.

  • flamant
    replied
    thank you shahzad992 for your answer,
    I just realized that my internet client didn't receive HTML format message. Actually my method to send messages in HTML format works. Sorry for the inconvenience.

    Leave a comment:


  • shahzad992
    replied
    Hi,

    I think following code should work:


    public void sendEmail(final String fromEmailAddress, final String fromEmailPassword, String emailToAddress, String emailBccAddress){

    File tempFile = null;

    try {

    String temp = null;
    FileChannel srcChannel = null;
    FileChannel dstChannel = null;

    String fileToSend= tempFilePath + System.currentTimeMillis() + ".html";
    tempFile = new File(fileToSend);

    RandomAccessFile randomAccessFile = new RandomAccessFile(fileToSend, "rws");

    if( tempFile!=null && tempFile.exists()) {

    dstChannel = new FileOutputStream(tempFile).getChannel();
    dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
    dstChannel.close();
    }



    String messageToSend = "";

    messageToSend = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"<URL>">" + "<htmlxmlns=\"<URL>\"><head><head/><body>"
    + messageSource.getMessage("messagetosend.part1", null, Locale.ENGLISH)+ user.getFirstName()+" "+ user.getLastName()+"<br/>"
    + messageSource.getMessage("messagetosend.part2", null, Locale.ENGLISH)+<br/>"
    + messageSource.getMessage("messagetosend.part3", null, Locale.ENGLISH)+ newPassword+<br/>"
    + messageSource.getMessage("messagetosend.part4", null, Locale.ENGLISH)+<br/>"
    + messageSource.getMessage("messagetosend.part5", null, Locale.ENGLISH)+<br/>"
    + messageSource.getMessage("homepage.internet.addres s", null, Locale.ENGLISH)+"</body></html>";
    while((temp = randomAccessFile.readLine()) != null) {


    randomAccessFile.seek(randomAccessFile.getFilePoin ter());
    randomAccessFile.writeBytes(messageToSend);


    break;
    }

    if(randomAccessFile!=null){
    randomAccessFile.close();
    }


    String emailSubject = "emailSubject";


    MimeMessage message = null;
    Session session = null;

    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    session = Session.getDefaultInstance(properties, new javax.mail.Authenticator(){
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(fromEmailAddress,fromEmailP assword);
    }});

    try {
    BodyPart bodyPart = getFileBodyPart(fileToSend, "text/html");
    Multipart multiPart = new MimeMultipart("alternative");
    multiPart.addBodyPart(bodyPart);

    message = new MimeMessage(session);
    message.setSender(new InternetAddress(fromEmailAddress));
    message.setSubject(emailSubject);
    message.setContent(multiPart);
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailToAddress));
    if(emailBccAddress!=null && emailBccAddress.length()>0){
    message.setRecipient(Message.RecipientType.BCC, new InternetAddress(emailBccAddress));
    }

    logger.info("\n \n Sending email through Transport.... \n "+emailToAddress+" \n "+emailBccAddress);
    Transport.send(message);

    } catch (AddressException e) {

    e.printStackTrace();
    } catch (MessagingException e) {

    e.printStackTrace();
    }


    if(tempFile!=null && tempFile.exists()) {
    tempFile.delete();
    tempFile = null;
    }

    } catch(IOException e) {

    e.printStackTrace();
    }

    }



    Regards,
    shahzad

    Leave a comment:


  • flamant
    replied
    Hi,
    somebody told me that the following method

    Code:
    mailSender.send(new MimeMessagePreparator() {
       public void prepare(MimeMessage mimeMessage) throws MessagingException {
         MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
         message.setFrom("[email protected]");
         message.setTo("[email protected]");
         message.setSubject("my subject");
         message.setText("my text <img src='cid:myLogo'>", true);
         message.addInline("myLogo", new ClassPathResource("img/mylogo.gif"));
         message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf"));
       }
     });
    works, but it is not convenient for my case. Anyone can help me ? Thank you

    Leave a comment:

Working...
X