packageemail;importjavax.activation.DataHandler;importjavax.activation.FileDataSource;importjavax.mail.Address;importjavax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;importjava.net.URL;importjava.util.Properties;/*** 读取本地文件为附件*/
public classEmailTest1 {private static final String MAIL_IMAP_STARTTLS_ENABLE = "mail.imap.starttls.enable";private static final String MAIL_SMTP_STARTTLS_ENABLE = "mail.smtp.starttls.enable";private static final String MAIL_SMTP_SSL_TRUST = "mail.smtp.ssl.trust";public static void main(String[] args) throwsException {
String host= "smtp.163.com"; //163的host,其他的自己去百度
String username = "test@163.com";//邮箱地址,就是你申请的邮箱账号
String password = "123456";//邮箱密码
Properties props =System.getProperties();//设置用户的认证方式
props.setProperty("mail.smtp.auth", "true");//设置传输协议
props.setProperty("mail.transport.protocol", "smtp");//设置发件人的SMTP服务器地址
props.setProperty("mail.smtp.host", host);//端口
props.setProperty("mail.smtp.port", "465");//debug,一般不配置这个参数,调试的时候用
props.setProperty("mail.debug", "true");//使用ssl需要加如下参数
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session= Session.getInstance(props, null);//附件处理
Multipart multipart = newMimeMultipart();
MimeBodyPart messageBodyPart1= newMimeBodyPart();
String content= "abcdefg";//正文,可以是html文档
messageBodyPart1.setContent(content, "text/html;charset=utf-8");
multipart.addBodyPart(messageBodyPart1);
MimeBodyPart messageBodyPart2= newMimeBodyPart();//邮件中包含本地附件
String path = "F:\\fff.docx";//1、得到数据源
FileDataSource fds = newFileDataSource(path);//2、得到附件本身并至入BodyPart
messageBodyPart2.setDataHandler(newDataHandler(fds));//3、设置附件名
messageBodyPart2.setFileName("fff.docx");
multipart.addBodyPart(messageBodyPart2);//邮件中包含网络附件
String path2 = "http://www.pptbz.com/pptpic/UploadFiles_6909/201203/2012031220134655.jpg";
URL url= newURL(path2);
DataHandler dataHandler= newDataHandler(url);
MimeBodyPart messageBodyPart3= newMimeBodyPart();
messageBodyPart3.setDataHandler(dataHandler);
messageBodyPart3.setFileName("2012031220134655.jpg");
multipart.addBodyPart(messageBodyPart3);
MimeMessage replyMessage= newMimeMessage(session);//发件箱,大部分与邮箱账号一致
InternetAddress mailFrom = new InternetAddress("test@163.com");//收件箱,有一段时间申请的QQ邮箱好像默认关闭了 pop3和smtp,所以qq邮箱可能会收不到邮件
InternetAddress mailTo = new InternetAddress("11111@qq.com");
replyMessage.setFrom(mailFrom);
replyMessage.setContent(multipart);//replyMessage.setContent("简单的纯文本邮件!", "text/html;charset=UTF-8");
replyMessage.setSubject("mail test test test");
replyMessage.setReplyTo(newAddress[]{mailFrom});
replyMessage.setRecipient(javax.mail.Message.RecipientType.TO, mailTo);
replyMessage.setSender(mailFrom);
Transport transport= session.getTransport("smtp");try{
transport.connect(host, username, password);//发送邮件
transport.sendMessage(replyMessage, newAddress[]{mailTo});
}catch(Exception e) {
}finally{
transport.close();
}
System.out.println("success");
}
}