JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 设定 Mail Server
senderImpl.setHost("mail.eastcom-sw.com"); // SMTP验证时,需要用户名和密码
senderImpl.setUsername("aaa");
senderImpl.setPassword("bbb");
// 不设这个是不能用用户名密码通过验证发的
Properties prop = new Properties();
prop.setProperty("mail.smtp.host", "true");
senderImpl.setJavaMailProperties(prop);
// 建立邮件讯息
MimeMessage mailMessage = senderImpl.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
true, "UTF-8");
// 设定收件人、寄件人、主题与正文内容
messageHelper.setTo("[email protected]");
messageHelper.setFrom("[email protected]");
messageHelper.setSubject("故障信息报告");
messageHelper.setText(" <html> <head> </head> <body> <h1>消息接收,请您查阅附件"
+ " </h1> </body> </html>", true);
// 发附件
File files = new File(path+"\\"+"信息报送"+"_"+date+".xls"); messageHelper.addAttachment(MimeUtility.encodeWord(files.getName()),files);
// 传送邮件
senderImpl.send(mailMessage); 现在的问题,接收到的邮件,收件人和主题都是空,正文内容是乱码,附件也看不到。但通过写main()方法测试都是正常的。乱码如下,有经验的朋友看看。
senderImpl.setHost("mail.eastcom-sw.com"); // SMTP验证时,需要用户名和密码
senderImpl.setUsername("aaa");
senderImpl.setPassword("bbb");
// 不设这个是不能用用户名密码通过验证发的
Properties prop = new Properties();
prop.setProperty("mail.smtp.host", "true");
senderImpl.setJavaMailProperties(prop);
// 建立邮件讯息
MimeMessage mailMessage = senderImpl.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
true, "UTF-8");
// 设定收件人、寄件人、主题与正文内容
messageHelper.setTo("[email protected]");
messageHelper.setFrom("[email protected]");
messageHelper.setSubject("故障信息报告");
messageHelper.setText(" <html> <head> </head> <body> <h1>消息接收,请您查阅附件"
+ " </h1> </body> </html>", true);
// 发附件
File files = new File(path+"\\"+"信息报送"+"_"+date+".xls"); messageHelper.addAttachment(MimeUtility.encodeWord(files.getName()),files);
// 传送邮件
senderImpl.send(mailMessage); 现在的问题,接收到的邮件,收件人和主题都是空,正文内容是乱码,附件也看不到。但通过写main()方法测试都是正常的。乱码如下,有经验的朋友看看。
http://www.javaeye.com/topic/32639
true, "UTF-8");
这段代码中的编码换一下看看,GBK,ISO8859-1......
------=_Part_74_22788607.1234347942031
Content-Type: multipart/related; boundary="----=_Part_75_17847461.1234347942031"------=_Part_75_17847461.1234347942031
Content-Type: text/html; charset=GBK
Content-Transfer-Encoding: 7bitdsgdsgsdg
------=_Part_75_17847461.1234347942031--------=_Part_74_22788607.1234347942031
Content-Type: application/vnd.ms-excel; name="tt_2009-02-11 182522.xls"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="tt_2009-02-11 182522.xls"
//下面还一对excel的码
你确定发布后,File的路径是正确的?
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;import com.tensynad.monitor.constants.MailInfo;
import com.tensynad.monitor.entity.SendMailInfo;/**
* <p>Title: 使用javamail发送邮件</p>
* <p>Description: 演示如何使用javamail包发送电子邮件。这个实例可发送多附件</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: Mail.java</p>
* @version 1.0
*/
public class Mail {
SendMailInfo smi;
String[] to = null;//收件人
String from = "";//发件人
String host = "";//smtp主机
String username = "" ;
String password = "" ;
String filename = "";//附件文件名
String subject = "";//邮件主题
String content = "";//邮件正文
Vector<String> file = new Vector<String>();//附件文件集合
/**
*<br>方法说明:默认构造器
*<br>输入参数:
*<br>返回类型:
*/
public Mail(){
this(null);
}
public Mail(SendMailInfo smi){
this.smi=smi;
this.setHost(MailInfo.MAIL_HOST);
this.setUserName(MailInfo.MAIL_USER);
this.setPassWord(MailInfo.MAIL_PASS);
this.setTo(MailInfo.MAIL_ADDRESS);
this.setFrom(MailInfo.MAIL_FROM);
this.setSubject(this.smi.getSubject());
}
/**
*<br>方法说明:构造器,提供直接的参数传入
*<br>输入参数:
*<br>返回类型:
*/
public Mail(String[] to,String from,String smtpServer,String username,String password,String subject,String content){
this.to = to;
this.from = from;
this.host = smtpServer;
this.username = username;
this.password = password;
this.subject = subject;
this.content = content;
}
/**
*<br>方法说明:设置邮件服务器地址
*<br>输入参数:String host 邮件服务器地址名称
*<br>返回类型:
*/
public void setHost(String host){
this.host = host;
}
/**
*<br>方法说明:设置登录服务器校验密码
*<br>输入参数:
*<br>返回类型:
*/
public void setPassWord(String pwd){
this.password = pwd;
}
/**
*<br>方法说明:设置登录服务器校验用户
*<br>输入参数:
*<br>返回类型:
*/
public void setUserName(String usn){
this.username = usn;
}
/**
*<br>方法说明:设置邮件发送目的邮箱
*<br>输入参数:
*<br>返回类型:
*/
public void setTo(String[] to){
this.to = to;
}
/**
*<br>方法说明:设置邮件发送源邮箱
*<br>输入参数:
*<br>返回类型:
*/
public void setFrom(String from){
this.from = from;
}
/**
*<br>方法说明:设置邮件主题
*<br>输入参数:
*<br>返回类型:
*/
public void setSubject(String subject){
this.subject = subject;
}
/**
*<br>方法说明:设置邮件内容
*<br>输入参数:
*<br>返回类型:
*/
public void setContent(String content){
this.content = content;
}
/**
*<br>方法说明:把主题转换为中文
*<br>输入参数:String strText
*<br>返回类型:
*/
public String transferChinese(String strText){
try{
strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");
}catch(Exception e){
e.printStackTrace();
}
return strText;
}
/**
*<br>方法说明:往附件组合中添加附件
*<br>输入参数:
*<br>返回类型:
*/
public void attachfile(String fname){
file.addElement(fname);
}
/**
*<br>方法说明:发送邮件
*<br>输入参数:
*<br>返回类型:boolean 成功为true,反之为false
*/
public boolean sendMail(){
boolean sessionDebug = false;
// 设定Mail所需要的服务器和所使用的传输协议
Properties props = System.getProperties();
props.put("mail.smtp.host",host);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth","true");
PopupAuthenticator pp = new PopupAuthenticator();
PasswordAuthentication pop = pp.performCheck(from,password);pop.getClass();
//产生新的Session服务 Session mailSession = Session.getInstance(props,pp);
mailSession.setDebug(sessionDebug);// //构造mail session
// Properties props = System.getProperties();
// props.put("mail.smtp.host",host);
// props.put("mail.smtp.auth","true");
// Session session=Session.getDefaultInstance(props, new Authenticator(){
// public PasswordAuthentication getPasswordAuthentication(){
// return new PasswordAuthentication(username,password);
// }
// });
try {
//构造MimeMessage 并设定基本的值
MimeMessage msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
if(to==null) return false;
InternetAddress[] as=new InternetAddress[to.length];
for(int i=0;i<to.length;i++){
as[i]=new InternetAddress(to[i]);
}
// InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,as);
subject = transferChinese(subject);
msg.setSubject(subject);
//构造Multipart
Multipart mp = new MimeMultipart();
//向Multipart添加正文
MimeBodyPart mbpContent = new MimeBodyPart();
mbpContent.setText(content);
//向MimeMessage添加(Multipart代表正文)
mp.addBodyPart(mbpContent);
//向Multipart添加附件
Enumeration efile=file.elements();
while(efile.hasMoreElements()){
MimeBodyPart mbpFile = new MimeBodyPart();
filename=efile.nextElement().toString();
FileDataSource fds = new FileDataSource(filename);
mbpFile.setDataHandler(new DataHandler(fds));
mbpFile.setFileName(fds.getName());
//向MimeMessage添加(Multipart代表附件)
mp.addBodyPart(mbpFile); }
file.removeAllElements();
//向Multipart添加MimeMessage
msg.setContent(mp);
msg.setSentDate(new Date());
//发送邮件
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null){
ex.printStackTrace();
}
return false;
}
return true;
}
/**
*<br>方法说明:主方法,用于测试
*<br>输入参数:
*<br>返回类型:
*/
public static void main(String[] args){
Mail sendmail = new Mail();
sendmail.setHost("smtp.263xmail.com");
sendmail.setUserName("abc");
sendmail.setPassWord("123456");
// sendmail.setTo("[email protected]");
// sendmail.setTo("[email protected]");
sendmail.setTo(new String[]{"[email protected]"});
sendmail.setFrom("[email protected]");
sendmail.setSubject("标题");
sendmail.setContent("内容!");
//Mail sendmail = new Mail("[email protected]","[email protected]","smtp.sohu.com","du_jiang","31415926","你好","胃,你好吗?");
// sendmail.attachfile("c: est.txt");
// sendmail.attachfile("DND.jar");
sendmail.sendMail(); }
}