import javax.mail.internet.MimeUtility包
// 解决文件名的中文问题
MimeUtility.decodeText(“attachment”)
// 解决标题的中文问题
MimeUtility.encodeText(”subject“)
这两个方法应该可以解决你的问题,我就是用的这两个方法
你的这行代码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("超越")+"\" <hjflbc1990@sina.com>"))
msg.setRecipients(RecipientType.TO,
InternetAddress.parse(MimeUtility.encodeText("毕老师")+" <bg@sina.com>,"+MimeUtility.encodeText("王老师")+" <wxz@sina.com>")
)
msg.setReplyTo(InternetAddress.parse("ss@sohu.com"))
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/htmlcharset=gbk")
msg.saveChanges()
//创建火箭
Transport transport = session.getTransport()
transport.connect("smtp.sina.com", 25, "hjflbc1990@sina.com", "*****")
//火箭发送卫星
transport.sendMessage(msg, InternetAddress.parse("aa,aaa"))
transport.close()
}
}
实现java发送邮件的过程大体有以下几步:
准备一个properties文件,该文件中存放SMTP服务器地址等参数。
利用properties创建一个Session对象
利用Session创建Message对象,然后设置邮件主题和正文
利用Transport对象发送邮件
需要的jar有2个:activation.jar和mail.jar发送附件,需要用到Multipart对象。
import java.io.Fileimport java.io.IOException
import java.io.InputStream
import java.util.Properties
import javax.activation.DataHandler
import javax.activation.DataSource
import javax.activation.FileDataSource
import javax.mail.BodyPart
import javax.mail.Message
import javax.mail.MessagingException
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
public class JavaMailWithAttachment {
private MimeMessage message
private Session session
private Transport transport
private String mailHost = ""
private String sender_username = ""
private String sender_password = ""
private Properties properties = new Properties()
/*
* 初始化方法
*/
public JavaMailWithAttachment(boolean debug) {
InputStream in = 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 (IOException e) {
e.printStackTrace()
}
session = Session.getInstance(properties)
session.setDebug(debug)// 开启后有调试信息
message = new MimeMessage(session)
}
/**
* 发送邮件
*
* @param subject
* 邮件主题
* @param sendHtml
* 邮件内容
* @param receiveUser
* 收件人地址
* @param attachment
* 附件
*/
public void doSendHtmlEmail(String subject, String sendHtml, String receiveUser, File attachment) {
try {
// 发件人
InternetAddress from = new InternetAddress(sender_username)
message.setFrom(from)
// 收件人
InternetAddress to = new InternetAddress(receiveUser)
message.setRecipient(Message.RecipientType.TO, to)
// 邮件主题
message.setSubject(subject)
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart()
// 添加邮件正文
BodyPart contentPart = new MimeBodyPart()
contentPart.setContent(sendHtml, "text/htmlcharset=UTF-8")
multipart.addBodyPart(contentPart)
// 添加附件的内容
if (attachment != null) {
BodyPart attachmentBodyPart = new MimeBodyPart()
DataSource source = new FileDataSource(attachment)
attachmentBodyPart.setDataHandler(new DataHandler(source))
// 网上流传的解决文件名乱码的方法,其实用MimeUtility.encodeWord就可以很方便的搞定
// 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
//sun.misc.BASE64Encoder enc = new sun.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("send success!")
} catch (Exception e) {
e.printStackTrace()
} finally {
if (transport != null) {
try {
transport.close()
} catch (MessagingException e) {
e.printStackTrace()
}
}
}
}
public static void main(String[] args) {
JavaMailWithAttachment se = new JavaMailWithAttachment(true)
File affix = new File("c:\\测试-test.txt")
se.doSendHtmlEmail("邮件主题", "邮件内容", "xxx@XXX.com", affix)//
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)