请问谁用javamail作过发送中文html邮件???能否指教。。 msg.setHeader("Content-Type", "text/html;charset=gb2312"); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 能将你发送英文的html格式文件的实例,发给我参考一下呢?谢谢 能否也發一個給我[email protected] 也发一个给我吧,我现在做项目正好需要这个。谢谢![email protected] 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>爱你</font></p>"); sb.append("</BODY>\n"); sb.append("</HTML>\n"); msg.setDataHandler(new DataHandler( new ByteArrayDataSource(sb.toString(), "text/html;charset=gb2312"))); }} 我用了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>爱你</font></p>"); sb.append("</BODY>\n"); sb.append("</HTML>\n"); msg.setDataHandler(new DataHandler( new ByteArrayDataSource(sb.toString(), "text/html;charset=gb2312"))); }} JNA 调用dll时的参数问题 java的数据类型有基本类型和引用类型。数组属于什么类型? 要考scjp大家推荐点教材 关于SWING中的DefaultCellEditor类的讲解,请老师以实战通俗的角度,而不是使用API文档上的解释,谢谢 请问这段小程序里换掉一句后,为何小球就不动了。 关于Sring查找问题 我可以为大家免费地做些项目 高分:一个关于日蚀Eclipse的问题!! 我们公司不让上QQ,不知道用代理服务器什么的可以上吗???? JBuilder5 的在线注册方法!!!!(献给没有注册机的Java爱好者) javac 为什么不能编译? 游戏最好用什么软件做呀?
[email protected]
[email protected]
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>爱你</font></p>"); sb.append("</BODY>\n");
sb.append("</HTML>\n"); msg.setDataHandler(new DataHandler(
new ByteArrayDataSource(sb.toString(), "text/html;charset=gb2312")));
}
}
可是发送的邮件似乎对此没有任何反映,仍然是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>爱你</font></p>"); sb.append("</BODY>\n");
sb.append("</HTML>\n"); msg.setDataHandler(new DataHandler(
new ByteArrayDataSource(sb.toString(), "text/html;charset=gb2312")));
}
}