package cn.com.xinli.mail;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class Mail {
/**
* 通常email都使用的是smtp来发送邮件,使用pop3来接受邮件
* @throws Exception
*/
public void sendMail() throws Exception
{
String host = "192.168.0.181"; //
String from = "
[email protected]"; //
String psw = "xinlilipf";
String to1 = "
[email protected]"; //
String to2 = "
[email protected]"; //
String to3 = "
[email protected]"; //
String cto1 = "
[email protected]"; //
Properties props = System.getProperties();
//host的问题
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Email_Autherticator myauth = new Email_Autherticator(from, psw);
Session session = Session.getDefaultInstance(props, myauth);
MimeMessage message = new MimeMessage(session);
message.setContent("444", "text/plain");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to1));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to2));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to3));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cto1));
message.setSubject("系统使用情况");
System.out.println("--------------");
/** 邮件附件 */
MimeBodyPart messageBodyPart =new MimeBodyPart();
Date date = new Date();
StringBuffer str = new StringBuffer();
str.append("大家好:")
.append("\r\n")
.append(" 附件是系统的使用情况\r\n")
.append(" \t 李鹏飞\r\n")
.append("\t\t\t\t"+date);
DataSource fds = new javax.mail.util.ByteArrayDataSource(str.toString().getBytes(),"text/html");
messageBodyPart.setDataHandler(new DataHandler(fds));
String a = "D:\\data\\登陆情况按时间.csv";
String b = "D:\\data\\登陆情况按地市.csv";
String c = "D:\\data\\登陆情况明细.csv";
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//发送附件
messageBodyPart = new MimeBodyPart();
DataSource source_a =new FileDataSource(a);
messageBodyPart.setDataHandler(new DataHandler(source_a));
messageBodyPart.setFileName(MimeUtility.encodeText("登陆情况按时间.csv"));
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source_b =new FileDataSource(b);
messageBodyPart.setDataHandler(new DataHandler(source_b));
messageBodyPart.setFileName(MimeUtility.encodeText("登陆情况按地市.csv"));
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source_c =new FileDataSource(c);
messageBodyPart.setDataHandler(new DataHandler(source_c));
messageBodyPart.setFileName(MimeUtility.encodeText("登陆情况明细.csv"));
multipart.addBodyPart(messageBodyPart);
message.setText("java");
message.setContent(multipart);
message.saveChanges();
Transport.send(message);
}
}
package cn.com.xinli.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticator extends Authenticator
{
String username = "";
String password = "";
public Email_Autherticator()
{
super();
}
public Email_Autherticator(String user,String pwd)
{
super();
username = user;
password = pwd;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,password);
}
}
1.乱码:
MimeUtility.encodeText("登陆情况明细.csv") 搞定。
2. jar :
activation.jar和mail.jar