0

I have the following code:

dev = "email@gmail.com";
dev_p = "password";
subject = "Thank you for signing up to the company auditing app";
body = "<!DOCTYPE html><body>To " + username + ",\nThank you for signing up to the company auditing application.\nYou can login to your account via our website <a href='http://www.companyauditingapp.com/'>here</a>.\n Regards,\nThe Team</body></html>";
Spanned html = Html.fromHtml(body);
try{
    GMailSender sender = new GMailSender(dev, dev_p);
    sender.sendMail(subject,html,dev,emailadd);
} catch (Exception e) {
    Log.e("SendMail", e.getMessage(), e);
}

I want to be able to send html via the sendMail method, in eclipse it keeps asking me to change the method parameter - strings html/body to a spanned string.

Can anyone suggest a solution to this problem?

Thanks in advance

Chris

cwiggo
  • 2,541
  • 9
  • 44
  • 87
  • The second parameter should be of String type instead of Spanned. – vortexwolf Jul 09 '13 at 11:39
  • Im trying to add html into my email, display html in the email that I am sending, do I need to change the method parameters or add headers to the email? – cwiggo Jul 09 '13 at 11:46
  • Try to replace the method call like this `sender.sendMail(subject, body, dev, emailadd)`. Use the `body` variable instead of `html`. Then it will either work or have a different error. – vortexwolf Jul 09 '13 at 13:09
  • yes but then that would throw the whole "String" of tags through to the sendMail method. All I am trying to do is send the string into the method, so that at the other end the email is viewed in HTML. – cwiggo Jul 09 '13 at 13:27
  • 1
    You should send a string and set some header which switches text/html. I don't know where you got your `GMailSender` class, but if it is taken from this answer http://stackoverflow.com/a/2033124/427225, you can replace `text/plain` by `text/html` in the `sendMail` method. – vortexwolf Jul 10 '13 at 09:45
  • i think that is exactly what i was looking for, yet to change and test, but i couldnt find it! ta – cwiggo Jul 10 '13 at 11:15
  • @vorrtex if you want to put that in an answer i'd be happy to accept it as the answer, so it was basically changing text/plain to text/html, ta once again – cwiggo Jul 19 '13 at 10:31

2 Answers2

2

It has turned out that GMailSender is a custom class published by a stackoverflow user in this post.

The easiest way to send HTML instead of plain text is to change the sendMail method from the link above and replace text/plain by text/html.

Or you can add a parameter to the method and make it more customizable:

public synchronized void sendMail(String subject, String body, String sender, String recipients, boolean isHtml) throws Exception {   
    try{
        MimeMessage message = new MimeMessage(session);
        String messageType = isHtml ? "text/html" : "text/plain";
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), messageType));   
        message.setSender(new InternetAddress(sender));   
        message.setSubject(subject);   
        message.setDataHandler(handler);   
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        Transport.send(message);   
    }catch(Exception e){

    }
}   
Community
  • 1
  • 1
vortexwolf
  • 13,967
  • 2
  • 54
  • 72
0

If you always want HTML-messages, you can change the sendmail function and change text/plain to text/html.

public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
    try{
    MimeMessage message = new MimeMessage(session);   
    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), **"text/plain"**));   
    message.setSender(new InternetAddress(sender));   
    message.setSubject(subject);   
    message.setDataHandler(handler);   
    if (recipients.indexOf(',') > 0)   
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
    else  
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
    Transport.send(message);   
    }catch(Exception e)
    }
} 
Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27