- 论坛徽章:
- 0
|
发送有中文文件名的附件的邮件用javamail通常会导致文件名出现乱码,这是因为MIME规范规定不允许encode文件名,但很多应用程序都亵渎这一规范,这已成事实的规范。
/*
* Created on 2005-7-19
*/
package com.jilaninfo.webmail;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* @author elgs
*/
public class Send
{
public static void main(String[] args) throws Exception
{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.cableplus.com.cn");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
Session mailSession = Session.getInstance(props, new PopAuthenticator(
"username", "password"));
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress("
elgs@cableplus.com.cn
"));
String to = "
elgs@cableplus.com.cn
";
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
msg.setSentDate(new Date());
msg.setSubject("中文标题");
Multipart mp = new MimeMultipart();
MimeBodyPart mbpFile = new MimeBodyPart();
MimeBodyPart mbpText = new MimeBodyPart();
FileDataSource fds = new FileDataSource("D:/工作流程.doc");
mbpFile.setDataHandler(new DataHandler(fds));
mbpFile.setFileName(MimeUtility.encodeText(fds.getName()));
mp.addBodyPart(mbpFile);
mbpText.setText("中文内容");
mp.addBodyPart(mbpText);
msg.setContent(mp);
Transport.send(msg);
System.out.println("Mail sent!");
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4960/showart_36645.html |
|