兄弟,下面是我在一个商务网站的一段代码看一下吧
try
{
Properties props = new Properties();
props = new Properties();
props.put("mail.smtp.host", mail_smtp_host);
props.setProperty("mail.transport.protocol","smtp");
props.setProperty("mail.smtp.auth","true");
Session s = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress(mail_from);
message.setFrom(from);
InternetAddress to = new InternetAddress(email);
message.addRecipient(Message.RecipientType.TO,to);
message.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(new String(fds.getName().getBytes("GBK"),"iso8859-1"));
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new java.util.Date());
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(new String(fds.getName().getBytes("GBK"),"iso8859-1"));
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new java.util.Date());
public boolean setBody(String mailBody) {
try{
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody,"text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
}
catch(Exception e){
System.err.println("设置邮件正文时发生错误!"+e);
return false;
}
}
public boolean addFileAffix(String filename) {
System.out.println("增加邮件附件:"+filename);
try{
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());