java制作简易邮箱代码 java制作简易邮箱代码怎么写( 三 )


// 设置HTML内容
messageBodyPart.setContent(mailInfo.getContent(),
"text/html; charset=" + encode);
mainPart.addBodyPart(messageBodyPart);
// 存在附件
String[] filePaths = mailInfo.getAttachFileNames();
if (filePaths != nullfilePaths.length0) {
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
for (String filePath : filePaths) {
messageBodyPart = new MimeBodyPart();
File file = new File(filePath);
if (file.exists()) {// 附件存在磁盘中
FileDataSource fds = new FileDataSource(file);// 得到数据源
messageBodyPart
.setDataHandler(new DataHandler(fds));// 得到附件本身并至入BodyPart
messageBodyPart.setFileName("=?" + encode + "?B?"
+ enc.encode(EmailFileNameConvert.changeFileName(file.getName()).getBytes())
+ "?=");// 得到文件名同样至入BodyPart
mainPart.addBodyPart(messageBodyPart);
}
}
}
// 将MimeMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
Transport.send(mailMessage);// 发送邮件
return true;
} catch (Exception e) {
e.printStackTrace();
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(5);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return false;
}
}
/**
* 封装邮件的基本信息
*
* @author Sglee
*
*/
public class MailInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = -3937199642590071261L;
private String mailServerHost;// 服务器ip
private String mailServerPort;// 端口
private long timeout;// 超时时间
private String fromAddress;// 发送者的邮件地址
private String[] toAddress;// 邮件接收者地址
private String[] msAddress;// 密送地址
private String username;// 登录邮件发送服务器的用户名
private String password;// 登录邮件发送服务器的密码
private boolean validate = false;// 是否需要身份验证
private String subject;// 邮件主题
private String content;// 邮件内容
private String[] attachFileNames;// 附件的文件地址
private boolean debug;// 调试模式
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
p.put("mail.smtp.timeout", this.timeout);
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String[] getToAddress() {
return toAddress;
}
public void setToAddress(String[] toAddress) {
this.toAddress = toAddress;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {

推荐阅读