为什么用javamail向163发送邮件,每封信都带有一个附件的符号???
当你没有发送附件时,163也会显示一个大头针的符号,这表明有附件,可是打开这封信,信里是没有附件的,这是对的,现在我的问题就是,不管你有没有发送带附件的邮件,163都会显示一个大头针的符号 (就是有附件的符号),这个怎么解决,下面是我的代码,大家帮我看看哪写错了:
[code]public HashMap send(){
HashMap map=new HashMap();
map.put("state", "success");
String message="邮件发送成功!";
Session session=null;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
if(ifAuth){ //服务器需要身份认证
props.put("mail.smtp.auth","true");
SmtpAuth smtpAuth=new SmtpAuth(username,password);
session=Session.getDefaultInstance(props, smtpAuth);
}else{
props.put("mail.smtp.auth","false");
session=Session.getDefaultInstance(props, null);
}
session.setDebug(true);
Transport trans = null;
try {
MimeMessage msg = new MimeMessage(session);
try{
Address from_address = new InternetAddress(from, displayName);
msg.setFrom(from_address);
}catch(java.io.UnsupportedEncodingException e){
e.printStackTrace();
}
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content.toString(), "text/html;charset=gb2312");
mp.addBodyPart(mbp);
if(!file.isEmpty()){//有附件
Enumeration efile=file.elements();
while(efile.hasMoreElements()){
mbp=new MimeBodyPart();
filename=efile.nextElement().toString(); //选择出每一个附件名
FileDataSource fds=new FileDataSource(filename); //得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart
mp.addBodyPart(mbp);
}
file.removeAllElements();
}
msg.setContent(mp); //Multipart加入到信件
msg.setSentDate(new Date()); //设置信件头的发送日期
//发送信件
msg.saveChanges();
trans = session.getTransport("smtp");
trans.connect(smtpServer, username, password);
trans.sendMessage(msg, msg.getAllRecipients());
trans.close();
}catch(AuthenticationFailedException e){
map.put("state", "failed");
message="邮件发送失败!错误原因:\n"+"身份验证错误!";
e.printStackTrace();
}catch (MessagingException e) {
message="邮件发送失败!错误原因:\n"+e.getMessage();
map.put("state", "failed");
e.printStackTrace();
Exception ex = null;
if ((ex = e.getNextException()) != null) {
System.out.println(ex.toString());
ex.printStackTrace();
}
}
//System.out.println("\n提示信息:"+message);
map.put("message", message);
return map;
}
[/code]
当你没有发送附件时,163也会显示一个大头针的符号,这表明有附件,可是打开这封信,信里是没有附件的,这是对的,现在我的问题就是,不管你有没有发送带附件的邮件,163都会显示一个大头针的符号 (就是有附件的符号),这个怎么解决,下面是我的代码,大家帮我看看哪写错了:
[code]public HashMap send(){
HashMap map=new HashMap();
map.put("state", "success");
String message="邮件发送成功!";
Session session=null;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
if(ifAuth){ //服务器需要身份认证
props.put("mail.smtp.auth","true");
SmtpAuth smtpAuth=new SmtpAuth(username,password);
session=Session.getDefaultInstance(props, smtpAuth);
}else{
props.put("mail.smtp.auth","false");
session=Session.getDefaultInstance(props, null);
}
session.setDebug(true);
Transport trans = null;
try {
MimeMessage msg = new MimeMessage(session);
try{
Address from_address = new InternetAddress(from, displayName);
msg.setFrom(from_address);
}catch(java.io.UnsupportedEncodingException e){
e.printStackTrace();
}
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content.toString(), "text/html;charset=gb2312");
mp.addBodyPart(mbp);
if(!file.isEmpty()){//有附件
Enumeration efile=file.elements();
while(efile.hasMoreElements()){
mbp=new MimeBodyPart();
filename=efile.nextElement().toString(); //选择出每一个附件名
FileDataSource fds=new FileDataSource(filename); //得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart
mp.addBodyPart(mbp);
}
file.removeAllElements();
}
msg.setContent(mp); //Multipart加入到信件
msg.setSentDate(new Date()); //设置信件头的发送日期
//发送信件
msg.saveChanges();
trans = session.getTransport("smtp");
trans.connect(smtpServer, username, password);
trans.sendMessage(msg, msg.getAllRecipients());
trans.close();
}catch(AuthenticationFailedException e){
map.put("state", "failed");
message="邮件发送失败!错误原因:\n"+"身份验证错误!";
e.printStackTrace();
}catch (MessagingException e) {
message="邮件发送失败!错误原因:\n"+e.getMessage();
map.put("state", "failed");
e.printStackTrace();
Exception ex = null;
if ((ex = e.getNextException()) != null) {
System.out.println(ex.toString());
ex.printStackTrace();
}
}
//System.out.println("\n提示信息:"+message);
map.put("message", message);
return map;
}
[/code]