Look!
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.net.URL;
import java.util.*;public class SendMail { public SendMail() {
}
public void sendMail(){
String to ="[email protected]";
String from ="[email protected]";
String server="smtp.163.net";
String user="xxx";
String password="*******";
try{
Properties props=new Properties ();
props.put("mail.smtp.host",server);
props.put("mail.smtp.auth","true");
props.put("mail.bebug","true"); Session sendMailSession=Session.getDefaultInstance(props,null);
sendMailSession.setDebug(true); Transport transport=sendMailSession.getTransport("smtp");
Message msg = new MimeMessage(sendMailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Java Mail Test");
msg.setHeader("X-Mailer", "msgsend");
msg.setSentDate(new Date()); MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("Lalalala......."); // create the second message part
MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message
FileDataSource fds = new FileDataSource("c://msdos.sys");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName()); // create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); // add the Multipart to the message
msg.setContent(mp); transport.connect (server,user,password);
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.net.URL;
import java.util.*;public class SendMail { public SendMail() {
}
public void sendMail(){
String to ="[email protected]";
String from ="[email protected]";
String server="smtp.163.net";
String user="xxx";
String password="*******";
try{
Properties props=new Properties ();
props.put("mail.smtp.host",server);
props.put("mail.smtp.auth","true");
props.put("mail.bebug","true"); Session sendMailSession=Session.getDefaultInstance(props,null);
sendMailSession.setDebug(true); Transport transport=sendMailSession.getTransport("smtp");
Message msg = new MimeMessage(sendMailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Java Mail Test");
msg.setHeader("X-Mailer", "msgsend");
msg.setSentDate(new Date()); MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("Lalalala......."); // create the second message part
MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message
FileDataSource fds = new FileDataSource("c://msdos.sys");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName()); // create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); // add the Multipart to the message
msg.setContent(mp); transport.connect (server,user,password);
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
关注中......