如下所示:
package test;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeUtility;import javax.mail.Session;import javax.mail.MessagingException;import javax.mail.Transport;public class SendHtmlMail { public static void sendMessage(String smtphost,字符串從,字符串到,字符串主題,字符串messageText)拋出MessagingException,Java.io.unsupportedEncodingException {//步驟1:配置Mail Session System.Out.println(“配置郵件會話: java.util.properties props = new Java.util.properties(); props.setProperty(“ mail.smtp.auth”,“ true”); //指定是否需要smtp驗證props.setproperty(“ mail.smtp.host”,smtphost); //指定smtpsmtp服務器propsept(“ mail.transport.prot.prot.prot.protcol.prot.protcol.protcol.prot color) session mailsession = session.getDefaultInstance(props); mailsession.setDebug(false); // debug信息//步驟2:構造消息system.out.println(“構造消息 - from =” +“ +” +從 +“ to =” + to); InternetAddress fromAddress = new InternetAddress(來自); InternetAddress Toaddress =新的InternetAddress(to); mimemessage testMessage = new Mimemessage(MailSession); testMessage.setfrom(摘要); testMessage.Addrecipient(Javax.mail.message.recipientType.to,Toaddress); testMessage.setSentDate(new Java.util.date()); testMessage.Setsubject(mimeutility.encodeText(主題,“ gb2312”,“ b”)); testMessage.setContent(MessageText,“ text/html; charset = gb2312”); system.out.println(“構造消息”); //步驟3:現在發送消息傳輸= mailSession.getTransport(“ SMTP”); transport.connect(smtphost,“ riteng_mes”,“ ri-teng1234”); transport.SendMessage(testMessage,testMessage.getAllRecipients()); transport.close(); system.out.println(“發送消息! ”); } public static void main(string [] args){string smtphost =“ 10.131.119.36”;字符串來自=“ [email protected]”;字符串為=“ [email protected]”;字符串主題=“ html郵件測試”; //主題javamail自動轉碼stringbuffer theSessage = new StringBuffer(); theSessage.Append(“ <h2> <font color = red> red> </font> </h2>”); theSessage.Append(“ <hr>”); theSessage.Append(“ <i>年年失望年年望</i>”); theSessage.append(“ <table border ='1'> <tr> <td> aaa </td> <td> bbb </td> </td> </tr> </tr> <tr> <tr> <td> ccc </td> ccc <td> <td> ddd> ddd </td> </td> </td> </tr> </tr> </table> </table>> </table>>”);嘗試{sendhtmlmail.sendmessage(smtphost,從,到主題,theSessage.toString()); } catch(javax.mail.messagingException exc){exc.printstacktrace(); } catch(java.io.unsupportedEncodingException exc){exc.printstacktrace(); }}}}以上就是小編為大家帶來的java發送帶html標籤內容的郵件實例代碼全部內容了,希望大家多多支持武林網〜