用java写一个美观的邮件界面
发布网友
发布时间:2022-04-28 11:47
我来回答
共1个回答
热心网友
时间:2023-10-08 15:56
06年的sun科技日的时候,sun的演讲团,讲师沈卓立拿这个做了一个例子。主要的美观处理还是多层的贴图效果,如果谁有源码也可以送我一份。这个个人觉得有点难度……
至于简单邮件收发,例子到处都是,我这里现在有个正用的发生例子。接收就是处理pop3,然后拆分邮件体和邮件标题,附件等一些功能。
private MimeMessage mimeMsg; // MIME邮件对象
private Session sessions; // 邮件会话对象
private Properties props; // 系统属性
private boolean needAuth = false; // smtp是否需要认证
private String username = ""; // smtp认证用户名和密码
private String password = "";
private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
public SendMail() {
// setSmtpHost(getConfig.mailHost);//如果没有指定邮件服务器,就从getConfig类中获取
createMimeMessage();
}
public SendMail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
public void setSmtpHost(String hostName) {
if (props == null)
props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
}
public boolean createMimeMessage() {
try {
sessions = Session.getDefaultInstance(props, null); // 获得邮件会话对象
} catch (Exception e) {
e.printStackTrace();
return false;
}
try {
mimeMsg = new MimeMessage(sessions); // 创建MIME邮件对象
mp = new MimeMultipart();
return true;
} catch (Exception e) {
System.err.println("创建MIME邮件对象失败!" + e);
return false;
}
}
public void setNeedAuth(boolean need) {
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
public void setNamePass(String name, String pass) {
username = name;
password = pass;
}
public boolean setSubject(String mailSubject) {
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
}
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 setFrom(String from) {
try {
mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
return true;
} catch (Exception e) {
return false;
}
}
public boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,password);
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
// transport.send(mimeMsg);
transport.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}