2013-07-09 148 views
0

我有以下代碼:Html.fromHtml Gmail發送的.sendMail(字符串,字符串,字符串,字符串)內

dev = "[email protected]"; 
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,在日食它不斷要求我將方法參數 - 字符串html/body更改爲spanned字符串。

任何人都可以提出解決這個問題的辦法嗎?

在此先感謝

克里斯

+0

第二個參數應該是String類型而不是Spanned。 – vorrtex

+0

我試圖將html添加到我的電子郵件中,在我發送的電子郵件中顯示html,是否需要更改方法參數或將標題添加到電子郵件? – cwiggo

+0

嘗試將此方法調用替換爲'sender.sendMail(subject,body,dev,emailadd)'。使用'body'變量而不是'html'。然後它將工作或有不同的錯誤。 – vorrtex

回答

2

它已經證明,GMailSender是由計算器用戶this post發佈的自定義類。

發送HTML而不是純文本的最簡單方法是從上面的鏈接更改sendMail方法,並用text/html替換text/plain

或者你可以添加一個參數的方法,使其更加個性化:

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

    } 
} 
0

如果你總是希望HTML的郵件,你可以改變sendmail的功能,改變text/plaintext/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) 
    } 
} 
+0

sendMail函數位於公共級GMailSender。 – sodasodamac