你的这行代码attachName=new String(attachName.getBytes("gb2312"),"ISO8859-1");应该修改成attachName=new String(attachName.getBytes("ISO8859-1"),"gb2312");还有就是邮件中文乱码问题。在界面传递时,中文通过一定编码格式编码后在传给另外一个界面,接收界面如果要正确的显示中文,应该正确的解码。可以使用jdk1.6提供MimeUtility类。将 FileDataSource fds=new FileDataSource(filename);修改为:
FileDataSource fds=new FileDataSource(MimeUtility.encodeText(filename));这样的话应该能解决附件乱码问题。这只是自己肤浅的认识,可能有些地方还有漏洞,往高手看后指点!!
=====================================================================
下面是敝人一段中文处理的代码,可以做参考
public class Demo3
{
/**
* 复杂邮件含附件+中文附件名_回信地址_友好名称
* @param args
*/
public static void main(String[] args) throws Exception
{
//配置环境
Properties pros = new Properties();
pros.setProperty("mail.smtp.auth", "true");
pros.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(pros);
session.setDebug(true);
//创建卫星
Message msg = new MimeMessage(session);
//设置msg的一些信息--发件人、主题、内容..
msg.setFrom(new InternetAddress("\""+MimeUtility.encodeText("超越")+"\" <[email protected]>"));
msg.setRecipients(RecipientType.TO,
InternetAddress.parse(MimeUtility.encodeText("毕老师")+" <[email protected]>,"+MimeUtility.encodeText("王老师")+" <[email protected]>")
);
msg.setReplyTo(InternetAddress.parse("[email protected]"));
MimeMultipart bodyMultipart = new MimeMultipart("mixed");
msg.setContent(bodyMultipart);
MimeBodyPart appurt1 = new MimeBodyPart();
MimeBodyPart appurt2 = new MimeBodyPart();
MimeBodyPart contentPart = new MimeBodyPart();
bodyMultipart.addBodyPart(appurt1);
bodyMultipart.addBodyPart(appurt2);
bodyMultipart.addBodyPart(contentPart);
appurt1.setDataHandler(new DataHandler(new FileDataSource("")));
appurt1.setFileName("");//重要
appurt2.setDataHandler(new DataHandler(new FileDataSource("")));
appurt2.setFileName("");
MimeMultipart contentMultipart = new MimeMultipart("related");
contentPart.setContent(contentMultipart);
MimeBodyPart picPart = new MimeBodyPart();
MimeBodyPart htmlPart = new MimeBodyPart();
contentMultipart.addBodyPart(picPart);
contentMultipart.addBodyPart(htmlPart);
picPart.setDataHandler(new DataHandler(new FileDataSource("")));
picPart.setHeader("Content-Location", "www.sohu.com/log.jpg");
htmlPart.setText("图片<img src=www.sohu.com/log.jpg/>", "text/html;charset=gbk");
msg.saveChanges();
//创建火箭
Transport transport = session.getTransport();
transport.connect("smtp.sina.com", 25, "[email protected]", "*****");
//火箭发送卫星
transport.sendMessage(msg, InternetAddress.parse("aa,aaa"));
transport.close();
}
}
2. 有关javamail的邮件附件的路径和文件名中文乱码问题
你可以试试javamail包中自复带的编码解码制方法
import javax.mail.internet.MimeUtility; 包
// 解决文件名的中文问题
MimeUtility.decodeText(“attachment”);
// 解决标题的中文问题
MimeUtility.encodeText(”subject“);
这两个方法应该可以解决你的问题,我就是用的这两个方法
3. Java收发邮件过程中具体的功能是怎么实现的
以qq邮箱为例:
packagemain;
importjava.util.Properties;
importjavax.mail.Authenticator;
importjavax.mail.MessagingException;
importjavax.mail.PasswordAuthentication;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeMessage.RecipientType;
publicclassSendMessageService{
;
privateMimeMessagemessage;
privatePropertiesprops=newProperties();
publicstaticStringuserName;
publicstaticStringpassword;
;
;
SendMessageService(){
{
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.qq.com");
props.put("mail.smtp.port","587");
props.put("mail.user","[email protected]");//发送出去邮箱,
props.put("mail.password","在腾讯邮箱申请的SMTP服务码");
//用户名、密码
userName=props.getProperty("mail.user");
password=props.getProperty("mail.password");
//构建授权信息,用于进行SMTP进行身份验证
Authenticatorauthenticator=newAuthenticator(){
(){
(userName,password);
}
};
mailSession=Session.getInstance(props,authenticator);
message=newMimeMessage(mailSession);
InternetAddressform=newInternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
}catch(MessagingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**参数,发送的邮件的标题,邮件的内容,目标邮箱地址**/
publicvoidsendEm(Stringstr,Stringcont,Stringmailbox){
try{
//添加目标邮箱地址
to=newInternetAddress(mailbox);
message.setRecipient(RecipientType.TO,to);
//设置邮件标题
message.setSubject(str);
//设置邮件的内容体
message.setContent(cont,"text/html;charset=UTF-8");
Transport.send(message);
}catch(MessagingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
newSendMessageService().sendEm("hahhahahhaha","哈哈哈哈哈哈","[email protected]");
}
}
申请/SMTP服务 ,先进入邮箱,点击设置,账户设置,翻到下面的开启第二个IMAP/SMTP服务 ,会得到一个SMTP码,记住它,
4. java 代码发邮件怎么添加附件
实现java发送邮件的过程大体有以下几步:
准备一个properties文件,该文件中存放SMTP服务器地址等参数。
利用properties创建一个Session对象
利用Session创建Message对象,然后设置邮件主题和正文
利用Transport对象发送邮件
需要的jar有2个:activation.jar和mail.jar发送附件,需要用到Multipart对象。
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Properties;
importjavax.activation.DataHandler;
importjavax.activation.DataSource;
importjavax.activation.FileDataSource;
importjavax.mail.BodyPart;
importjavax.mail.Message;
importjavax.mail.MessagingException;
importjavax.mail.Multipart;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeBodyPart;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeMultipart;
importjavax.mail.internet.MimeUtility;
{
privateMimeMessagemessage;
privateSessionsession;
privateTransporttransport;
privateStringmailHost="";
privateStringsender_username="";
privateStringsender_password="";
privatePropertiesproperties=newProperties();
/*
*初始化方法
*/
publicJavaMailWithAttachment(booleandebug){
InputStreamin=JavaMailWithAttachment.class.getResourceAsStream("MailServer.properties");
try{
properties.load(in);
this.mailHost=properties.getProperty("mail.smtp.host");
this.sender_username=properties.getProperty("mail.sender.username");
this.sender_password=properties.getProperty("mail.sender.password");
}catch(IOExceptione){
e.printStackTrace();
}
session=Session.getInstance(properties);
session.setDebug(debug);//开启后有调试信息
message=newMimeMessage(session);
}
/**
*发送邮件
*
*@paramsubject
*邮件主题
*@paramsendHtml
*邮件内容
*@paramreceiveUser
*收件人地址
*@paramattachment
*附件
*/
publicvoiddoSendHtmlEmail(Stringsubject,StringsendHtml,StringreceiveUser,Fileattachment){
try{
//发件人
InternetAddressfrom=newInternetAddress(sender_username);
message.setFrom(from);
//收件人
InternetAddressto=newInternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO,to);
//邮件主题
message.setSubject(subject);
//向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipartmultipart=newMimeMultipart();
//添加邮件正文
BodyPartcontentPart=newMimeBodyPart();
contentPart.setContent(sendHtml,"text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
//添加附件的内容
if(attachment!=null){
BodyPartattachmentBodyPart=newMimeBodyPart();
DataSourcesource=newFileDataSource(attachment);
attachmentBodyPart.setDataHandler(newDataHandler(source));
//网上流传的解决文件名乱码的方法,其实用MimeUtility.encodeWord就可以很方便的搞定
//这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
//sun.misc.BASE64Encoderenc=newsun.misc.BASE64Encoder();
//messageBodyPart.setFileName("=?GBK?B?"+enc.encode(attachment.getName().getBytes())+"?=");
//MimeUtility.encodeWord可以避免文件名乱码
attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
multipart.addBodyPart(attachmentBodyPart);
}
//将multipart对象放到message中
message.setContent(multipart);
//保存邮件
message.saveChanges();
transport=session.getTransport("smtp");
//smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(mailHost,sender_username,sender_password);
//发送
transport.sendMessage(message,message.getAllRecipients());
System.out.println("sendsuccess!");
}catch(Exceptione){
e.printStackTrace();
}finally{
if(transport!=null){
try{
transport.close();
}catch(MessagingExceptione){
e.printStackTrace();
}
}
}
}
publicstaticvoidmain(String[]args){
JavaMailWithAttachmentse=newJavaMailWithAttachment(true);
Fileaffix=newFile("c:\测试-test.txt");
se.doSendHtmlEmail("邮件主题","邮件内容","[email protected]",affix);//
}
}
5. javamail使用IMAP协议接受QQ邮件不成功,大虾们帮忙解决下~
...IMAP8等邮件协议,支持OviShare、抄OviContacts、OviMail 输入法 英文输入法,中文输入法,笔划中文输入法,拼... 这两款不支持后台,但是你可以去别的手机店里找人帮刷机(修改系统),就可以后台QQ了,我的8881C现在就正...