你的這行代碼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現在就正...