http://www.iteye.com/problems/17702
package cn.itcast.javamail2;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Demo1 {
/**
* @param args add by zxx ,Feb 5, 2009
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setText("你好吗?");
msg.setFrom(new InternetAddress("
[email protected]"));
Transport transport = session.getTransport();
transport.connect("smtp.sina.com", 25, "itcast_test", "123456");
transport.sendMessage(msg,
new Address[]{new InternetAddress("
[email protected]")});
//transport.send(msg,new Address[]{new InternetAddress("
[email protected]")});
transport.close();
}
}
package cn.itcast.javamail2;
import java.io.FileInputStream;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Demo2 {
/**
* @param args add by zxx ,Feb 5, 2009
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.sina.com");
Session session = Session.getInstance(props,
new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("itcast_test","123456");
}
}
);
session.setDebug(true);
/*Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("
[email protected]"));
msg.setSubject("中文主题");
msg.setRecipients(RecipientType.TO,
InternetAddress.parse("
[email protected],
[email protected]"));
msg.setContent("<span style='color:red'>中文呵呵呵</span>", "text/html;charset=gbk");
Transport.send(msg);*/
Message msg = new MimeMessage(session,new FileInputStream("resouce\\demo3.eml"));
Transport.send(msg,InternetAddress.parse("
[email protected]"));
}
}
package cn.itcast.javamail2;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
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 Demo3 {
/**
* @param args add by zxx ,Feb 5, 2009
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Session session = Session.getInstance(new Properties());
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("itcast") + "\" <
[email protected]>"));
msg.setSubject("你们的Java培训真的是最牛的吗?");
msg.setReplyTo(new Address[]{new InternetAddress("
[email protected]")});
msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("黎活明") + " <
[email protected]>," + MimeUtility.encodeText("张孝祥") + " <
[email protected]>"));
MimeMultipart msgMultipart = new MimeMultipart("mixed");
msg.setContent(msgMultipart);
MimeBodyPart attch1 = new MimeBodyPart();
MimeBodyPart attch2 = new MimeBodyPart();
MimeBodyPart content = new MimeBodyPart();
msgMultipart.addBodyPart(attch1);
msgMultipart.addBodyPart(attch2);
msgMultipart.addBodyPart(content);
DataSource ds1 = new FileDataSource(
"resource\\Java培训.txt"
);
DataHandler dh1 = new DataHandler(ds1 );
attch1.setDataHandler(dh1);
attch1.setFileName(
MimeUtility.encodeText("java培训.txt")
);
DataSource ds2 = new FileDataSource(
"resource\\slogo.gif"
);
DataHandler dh2 = new DataHandler(ds2 );
attch2.setDataHandler(dh2);
attch2.setFileName("slogo.gif");
MimeMultipart bodyMultipart = new MimeMultipart("related");
content.setContent(bodyMultipart);
MimeBodyPart htmlPart = new MimeBodyPart();
MimeBodyPart gifPart = new MimeBodyPart();
bodyMultipart.addBodyPart(htmlPart);
bodyMultipart.addBodyPart(gifPart);
DataSource gifds = new FileDataSource(
"resource\\logo.gif"
);
DataHandler gifdh = new DataHandler(gifds);
gifPart.setDataHandler(gifdh);
gifPart.setHeader("Content-Location", "http://www.itcast.cn/logo.gif");
htmlPart.setContent("你们的Java培训真的是最牛的吗?大家都这么说,我想跟你们比试一下!这可是我自己用程序生成和发送的邮件哦!<img src='http://www.itcast.cn/logo.gif'>"
, "text/html;charset=gbk");
msg.saveChanges();
OutputStream ips = new FileOutputStream("resource\\demo3.eml");
msg.writeTo(ips);
ips.close();
}
}