msg.setHeader("Content-Type", "text/html;charset=gb2312");

解决方案 »

  1.   

    能将你发送英文的html格式文件的实例,发给我参考一下呢?谢谢
      

  2.   

    能否也發一個給我
    [email protected]
      

  3.   

    也发一个给我吧,我现在做项目正好需要这个。谢谢!
    [email protected]
      

  4.   

    import java.io.*;
    import java.util.Properties;
    import java.util.Date;import javax.mail.*;
    import javax.activation.*;
    import javax.mail.internet.*;public class sendhtml {    public static void main(String[] argv) {
    new sendhtml();
        }    public sendhtml() { String  to = "[email protected]";
            String subject = "title";
            String from = "[email protected]";
            String cc = null;
            String bcc = null;
            String url = null;
    String mailhost = "smtp.263.net";
    String mailer = "sendhtml";
    String protocol = null, host = null, user = null, password = null;
    String record = null; // name of folder in which to record mail
    boolean debug = false;
    BufferedReader in =
    new BufferedReader(new InputStreamReader(System.in)); try {     Properties props = System.getProperties();
        // XXX - could use Session.getTransport() and Transport.connect()
        // XXX - assume we're using SMTP
        if (mailhost != null)
    props.put("mail.smtp.host", mailhost);     // Get a Session object
        Session session = Session.getDefaultInstance(props, null);
        if (debug)
    session.setDebug(true);     // construct the message
        Message msg = new MimeMessage(session);
        if (from != null)
    msg.setFrom(new InternetAddress(from));
        else
    msg.setFrom();     msg.setRecipients(Message.RecipientType.TO,
    InternetAddress.parse(to, false));
        if (cc != null)
    msg.setRecipients(Message.RecipientType.CC,
    InternetAddress.parse(cc, false));
        if (bcc != null)
    msg.setRecipients(Message.RecipientType.BCC,
    InternetAddress.parse(bcc, false));     msg.setSubject(subject);     collect(msg);     msg.setHeader("X-Mailer", mailer);
        msg.setSentDate(new Date());     // send the thing off
        Transport.send(msg);     System.out.println("\nMail was sent successfully.");     // Keep a copy, if requested.     if (record != null) {
    // Get a Store object
    Store store = null;
    if (url != null) {
        URLName urln = new URLName(url);
        store = session.getStore(urln);
        store.connect();
    } else {
        if (protocol != null)
    store = session.getStore(protocol);
        else
    store = session.getStore();     // Connect
        if (host != null || user != null || password != null)
    store.connect(host, user, password);
        else
    store.connect();
    } // Get record Folder.  Create if it does not exist.
    Folder folder = store.getFolder(record);
    if (folder == null) {
        System.err.println("Can't get record folder.");
        System.exit(1);
    }
    if (!folder.exists())
        folder.create(Folder.HOLDS_MESSAGES); Message[] msgs = new Message[1];
    msgs[0] = msg;
    folder.appendMessages(msgs); System.out.println("Mail was recorded successfully.");
        } } catch (Exception e) {
        e.printStackTrace();
    }
        }    public void collect(Message msg)
    throws MessagingException, IOException {
    String line;
    String subject = msg.getSubject();
    StringBuffer sb = new StringBuffer();
    sb.append("<HTML>\n");
    sb.append("<HEAD>\n");
    sb.append("<TITLE>\n");
    sb.append(subject + "\n");
    sb.append("</TITLE>\n");
    sb.append("</HEAD>\n"); sb.append("<BODY>\n");
    sb.append("<H1>" + subject + "</H1>" + "\n");
            
            sb.append("<p><font size=7 color=FF0000 face=MS Song>&#29233;&#20320;</font></p>"); sb.append("</BODY>\n");
    sb.append("</HTML>\n"); msg.setDataHandler(new DataHandler(
    new ByteArrayDataSource(sb.toString(), "text/html;charset=gb2312")));
        }
    }
      

  5.   

    我用了caffv的collect()
    可是发送的邮件似乎对此没有任何反映,仍然是PLAINTEXT格式的,
    请问是为什么呢?
    发送邮件的代码如下:package Mail;import java.io.*;
    import java.util.*;import javax.mail.*;
    import javax.activation.*;
    import javax.mail.internet.*;public class MessageData
    {   /** 
       * Send Mail with attachment.
       */
       protected void doSendMessage(Dictionary fields)
          throws IOException, MessagingException, javax.servlet.ServletException
       {   String smtpServer = (String) fields.get("smtpServer");
      String mailer = "msgsend";   Properties props = System.getProperties();
      props.put("mail.smtp.host", smtpServer);   Session session = Session.getDefaultInstance(props, null);
      
      Message msg = new MimeMessage(session);
          msg.setFrom(new InternetAddress((String)fields.get("from")));
          
      InternetAddress[] tos = InternetAddress.parse((String)fields.get("to"));
          msg.setRecipients(Message.RecipientType.TO, tos);      if(fields.get("cc") != null)
          {
             InternetAddress[] ccs = InternetAddress.parse((String)fields.get("cc"));
             msg.setRecipients(Message.RecipientType.CC,ccs);
          }
          msg.setSubject((String)fields.get("subject"));   collect(msg);   msg.setHeader("X-Mailer", mailer);
          msg.setSentDate(new Date());      if(null == fields.get("attachment"))
             msg.setText((String)fields.get("body"));
          else
          {
             BodyPart body = new MimeBodyPart(),
    attachment = (BodyPart)fields.get("attachment");         body.setText((String)fields.get("body"));
             MimeMultipart multipart = new MimeMultipart();
             multipart.addBodyPart(body);
             multipart.addBodyPart(attachment);
             msg.setContent(multipart);
      }     Transport.send(msg);   }    public void collect(Message msg)
                        throws MessagingException, IOException 
    {
    String line;
    String subject = msg.getSubject();
    StringBuffer sb = new StringBuffer();
    sb.append("<HTML>\n");
    sb.append("<HEAD>\n");
    sb.append("<TITLE>\n");
    sb.append(subject + "\n");
    sb.append("</TITLE>\n");
    sb.append("</HEAD>\n"); sb.append("<BODY>\n");
    sb.append("<H1>" + subject + "</H1>" + "\n");

    sb.append("<p><font size=7 color=FF0000 face=MS Song>&#29233;&#20320;</font></p>"); sb.append("</BODY>\n");
    sb.append("</HTML>\n"); msg.setDataHandler(new DataHandler(
            new ByteArrayDataSource(sb.toString(), "text/html;charset=gb2312")));
        }