如下所示:
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标签内容的邮件实例代码全部内容了,希望大家多多支持武林网〜