- package com.syj;
-
- import java.io.bytearrayoutputstream;
- import java.io.fileinputstream;
- import java.io.ioexception;
- import java.util.arrays;
- import java.util.date;
- import java.util.properties;
-
- import javax.activation.datahandler;
- import javax.activation.filedatasource;
- import javax.mail.authenticator;
- import javax.mail.message;
- import javax.mail.passwordauthentication;
- import javax.mail.session;
- import javax.mail.transport;
- import javax.mail.internet.internetaddress;
- import javax.mail.internet.mimemessage;
-
- import javax.mail.bodypart;
- import javax.mail.multipart;
- import javax.mail.internet.mimebodypart;
- import javax.mail.internet.mimemultipart;
-
- import com.sun.istack.internal.bytearraydatasource;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class sendmail {
- private static string username = "xxxx";
- private static string password = "xxxx";
- private static string smtpserver = "smtp.163.com";
- private static string frommailaddress = "xxxx@163.com";
- private static string tomailaddress = "sunyujia@yahoo.cn";
-
- public static void main(string[] args) throws exception {
- properties props = new properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.host", smtpserver);
-
- session session = session.getdefaultinstance(props,
- new smtpauthenticator(username, password));
-
-
- mimemessage mimemessage = new mimemessage(session);
- mimemessage.setfrom(new internetaddress(frommailaddress));
- mimemessage.setrecipient(message.recipienttype.to, new internetaddress(
- tomailaddress));
- mimemessage.setsubject("主题");
- mimemessage.setsentdate(new date());
- multipart mp = new mimemultipart("related");
-
- bodypart bodypart = new mimebodypart();
- bodypart.setdatahandler(new datahandler("测<img src="cid:img1" />试",
- "text/html;charset=gbk"));
-
- bodypart attachbodypart = new mimebodypart();
- filedatasource fds = new filedatasource("c:/boot.ini");
- attachbodypart.setdatahandler(new datahandler(fds));
- attachbodypart.setfilename("=?gbk?b?"
- + new sun.misc.base64encoder().encode(fds.getname().getbytes())
- + "?=");
- mp.addbodypart(attachbodypart);
-
- mimebodypart imgbodypart = new mimebodypart();
- byte[] bytes = readfile("c:/button.gif");
- bytearraydatasource fileds = new bytearraydatasource(bytes,
- "application/octet-stream");
- imgbodypart.setdatahandler(new datahandler(fileds));
- imgbodypart.setfilename("button.gif");
- imgbodypart.setheader("content-id", "<img1></img1>");
- mp.addbodypart(imgbodypart);
-
- mp.addbodypart(bodypart);
- mimemessage.setcontent(mp);
- transport.send(mimemessage);
-
- }
-
-
-
-
-
-
-
-
- public static byte[] readfile(string file) {
- fileinputstream fis = null;
- bytearrayoutputstream bos = null;
- try {
- fis = new fileinputstream(file);
- bos = new bytearrayoutputstream();
- int bytesread;
- byte buffer[] = new byte[1024 * 1024];
- while ((bytesread = fis.read(buffer)) != -1) {
- bos.write(buffer, 0, bytesread);
- arrays.fill(buffer, (byte) 0);
- }
- } catch (ioexception e1) {
- e1.printstacktrace();
- } finally {
- try {
- if (bos != null)
- bos.close();
- } catch (ioexception e) {
- e.printstacktrace();
- }
- }
- return bos.tobytearray();
- }
- }
-
-
-
-
- class smtpauthenticator extends authenticator {
- string username = null;
- string password = null;
-
-
- public smtpauthenticator(string username, string password) {
- this.username = username;
- this.password = password;
- }
-
- public passwordauthentication getpasswordauthentication() {
- return new passwordauthentication(this.username, this.password);
- }
-
- }
|