//发送一个简单的邮件
Properties props=new Properties();
props.put("mail.smtp.host", "smtp.163.com");//发送邮件的服务器地址
props.put("mail.smtp.auth", "true");//必须填写用于用户(发件人)验证
//邮件发送和接收都必须用到此对象
Session session=Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","123456");
}
});
//设置debug模式可以再控制台输出日志
session.setDebug(true);
//邮件对象通常都为MimeMessage类型
MimeMessage message=new MimeMessage(session);
//设置主题
message.setSubject("邮件主题");
message.setText("邮件内容");//message.setContent("邮件内容","text/plain;charset=utf-8");
//设置发件人地址必须和session构造方法中的第二个参数中的用户名一致
message.setFrom(new InternetAddress("[email protected]"));
//设置回复人如果收件人手动回复时自动显示要回复的邮件列表
message.setReplyTo(new Address[]{new InternetAddress("[email protected]")});
//设置接收人(接收人,抄送人,密送人)
message.addRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
message.addRecipient(RecipientType.CC,new InternetAddress("[email protected]"));
message.addRecipient(RecipientType.BCC,new InternetAddress("[email protected]"));
//实例化传输对象好处是发送多封邮件时效率高
Transport transport=session.getTransport("smtp");
transport.send(message);//Transport.send(message);
transport.close();
//发送一封html格式的邮件
Properties props=new Properties();
props.put("mail.smtp.host","smtp.163.com");
props.put("mail.smtp.auth", "true");
Session session=Session.getDefaultInstance(props,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","123456");
}
});
MimeMessage message=new MimeMessage(session);
message.setSubject("邮件注册");
StringBuffer content=new StringBuffer();
content.append("<img src='http://www.baidu.com/img/baidu_sylogo1.gif' alt=百度''/>");
content.append("<a href='http://www.baidu.com' target='_blank'>点击</a>");
message.setContent(content.toString(),"text/html;charset=utf-8");
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
Transport.send(message);
//发送一份自动回复的邮件
Properties props=new Properties();
props.put("mail.smtp.host","smtp.163.com");
props.put("mail.smtp.auth","true");
Session session=Session.getDefaultInstance(props,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","123456");
}
});
session.setDebug(true);
MimeMessage message=new MimeMessage(session);
//设置头信息才,第二个参数为需要回执的地址
message.setHeader("Disposition-Notification-To", "[email protected]");
message.setSubject("邮件主题");
Multipart multipart=new MimeMultipart();
BodyPart htmlPart=new MimeBodyPart();
htmlPart.setContent("邮件html<a href='http://www.baidu.com' target='_blank'>点击</a>","text/html;charset=utf-8");
multipart.addBodyPart(htmlPart);
message.setContent(multipart);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(RecipientType.TO,new InternetAddress("[email protected]"));
Transport.send(message);
//发送一封带附件的邮件
Properties props=new Properties();
props.put("mail.smtp.host","smtp.163.com");
props.put("mail.smtp.auth","true");
Session session=Session.getDefaultInstance(props,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","123456");
}
});
MimeMessage message=new MimeMessage(session);
message.setSubject("邮件主题");
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(RecipientType.TO,new InternetAddress("[email protected]"));
Multipart multipart=new MimeMultipart();
BodyPart dataPart=new MimeBodyPart();
dataPart.setDataHandler(new DataHandler(new FileDataSource("d:/man.jpg")));
dataPart.setFileName("man.jpg");//必须设定不然不会再收件人信箱中找到附件
BodyPart contentPart=new MimeBodyPart();
contentPart.setContent("邮件内容","text/plain;charset=utf-8");
multipart.addBodyPart(contentPart);
multipart.addBodyPart(dataPart);
message.setContent(multipart);
Transport.send(message);
//发送一封带文本附件并且附件名为中文,内容为中文邮件
Properties props=new Properties();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
Session session=Session.getDefaultInstance(props,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","123456");
}
});
MimeMessage message=new MimeMessage(session);
message.setSubject("邮件主题");
message.setRecipient(RecipientType.TO,new InternetAddress("[email protected]"));
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(RecipientType.TO,new InternetAddress("[email protected]"));
Multipart multipart=new MimeMultipart();
BodyPart dataPart=new MimeBodyPart();
dataPart.setDataHandler(new DataHandler(new FileDataSource("d:/new.docx")));
dataPart.setFileName(MimeUtility.encodeText("附件名字.txt","utf-8",null));
BodyPart contentPart=new MimeBodyPart();
contentPart.setContent("邮件内容","text/plain;charset=utf-8");
multipart.addBodyPart(contentPart);
multipart.addBodyPart(dataPart);
message.setContent(multipart);
Transport.send(message);
//接收邮件的一般步骤
Properties props=new Properties();
props.put("mail.smtp.host","smtp.163.com");
props.put("mail.pop3.host", "pop3.163.com");//必须写
props.put("mail.smtp.auth","true");
Session session=Session.getDefaultInstance(props,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","123456");
}
});
Store store=session.getStore("pop3");
store.connect();//必须连接
Folder folder=store.getFolder("INBOX");//pop3只有一个目录,imap可以有多个
folder.open(Folder.READ_ONLY);
Message[] messageArr=folder.getMessages();
for (int i = 0; i < messageArr.length; i++) {
System.out.println("-----------");
System.out.println("第 "+(i+1)+"封 subject:"+messageArr[i].getSubject());
System.out.println("第 "+(i+1)+"封 from:"+((InternetAddress)messageArr[i].getFrom()[0]).getAddress());
StringBuffer content=new StringBuffer();
Part part=(Part)messageArr[i];
if(part.isMimeType("text/plain")){
content.append(messageArr[i].getContent());
}else if(part.isMimeType("multipart/*")){
Multipart multipart=(Multipart)messageArr[i].getContent();
for (int j = 0; j < multipart.getCount(); j++) {
content.append(multipart.getBodyPart(j).getContent());
}
}
System.out.println("第 "+(i+1)+"封 content:"+content.toString());
System.out.println();
}
folder.close(false);
store.close();