Gmail Sender .sendMail中的Html.fromHtml(字符串,字符串,字符串,字符串)

时间:2013-07-09 11:17:32

标签: android html parameters gmail html-email

我有以下代码:

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);
}

我希望能够通过sendMail方法发送html,在eclipse中它一直要求我更改方法参数 - 将字符串html / body更改为跨区字符串。

有人可以建议解决这个问题吗?

提前致谢

克里斯

2 个答案:

答案 0 :(得分:2)

事实证明GMailSender是由stackoverflow用户在this post中发布的自定义类。

发送HTML而不是纯文本的最简单方法是从上面的链接更改sendMail方法,并将text/plain替换为text/html

或者您可以向方法添加参数并使其更具可自定义性:

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){

    }
}   

答案 1 :(得分:0)

如果您一直想要HTML消息,可以更改sendmail功能并将text/plain更改为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)
    }
}