/*通过JAVAMail发送邮件步骤:
1 构建Session 实例
2 以Session 实例为参数构造MineMessage的空实例
3为 mineMessage 实例设置合适的属性和内容
4 使用抽象类Transprot 的send 或 sendMessage 方法发送邮件
在配置文件设置也行
MailSender 只能发送简单的,JavaMailSender 可以发送复杂的
*/
简单文本内容方式:
带图片的方式:
3 html 方式:
4 综合以上:
1 构建Session 实例
2 以Session 实例为参数构造MineMessage的空实例
3为 mineMessage 实例设置合适的属性和内容
4 使用抽象类Transprot 的send 或 sendMessage 方法发送邮件
在配置文件设置也行
MailSender 只能发送简单的,JavaMailSender 可以发送复杂的
*/
简单文本内容方式:
package com.mail.main;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author xxxx
* @version 創建時間:Sep 26, 2009 2:22:39 PM
* 類說明:
*/
public class TextMessage {
/**
* @param args
* @throws MessagingException
* @throws AddressException
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws AddressException, MessagingException, FileNotFoundException, IOException {
// TODO Auto-generated method stub
String from ="it315_test@sina.com";
String to = "it315_test@sina.com";
String subject ="test";
String body ="This is a test of text mail";
//创建Session 实例对象
Session session = Session.getDefaultInstance(new Properties());
//创建MimeMessage 实例对象
MimeMessage msg =new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress(from));
//to
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
//date
msg.setSentDate(new Date());
//subject
msg.setSubject(subject);
msg.setText(body);
//保存最终生成的邮件内容
msg.saveChanges();
//把MIMEMessage 对象中的内容写到文件中
msg.writeTo(new FileOutputStream("d:\\test.eml"));
}
}
带图片的方式:
package com.mail.main;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* @author xxxxxx
* @version 創建時間:Sep 26, 2009 2:22:39 PM
* 類說明:
*/
public class PictureMessage {
/**
* @param args
* @throws MessagingException
* @throws AddressException
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws AddressException, MessagingException, FileNotFoundException, IOException {
// TODO Auto-generated method stub
String from ="jsp_mail@sina.com";
String to = "jsp_mail@sina.com";
String subject ="test picture ";
// String body ="<h4>This is a test of HTML mail</h4><img src=\"http://www.it315.org/images/it315logo.gif\">";
// String body ="<h4>This is a test of HTML mail</h4><img src=\"88364.gif\">";
String body ="<h4>This is a test of HTML mail</h4><img src=\"cid:88364.gif\">";//为啥加cid 呢?表示引用Mime消息中Content-id属性值为88364.gif的消息体数据
//创建Session 实例对象
Session session = Session.getDefaultInstance(new Properties());
//创建MimeMessage 实例对象,并设置各种邮件头字段
MimeMessage msg =new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress(from));
//to
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
// 创建一个子类型为“related“的MimeMultipart 对象
MimeMultipart multipart = new MimeMultipart("related");
/*创建一个表示HTML正文的MimeBodyPart对象,并
* 将它加入到前面创建的MimeMultipart对象中
*/
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body,"text/html;charset=gb2312");
multipart.addBodyPart(htmlBodyPart);
/*创建一个表示图片内容的MimeBodyPart对象,并
* 将它加入到前面创建的MimeMultipart对象中
*/
MimeBodyPart gifBodyPart =new MimeBodyPart();
// gifBodyPart.setContent(arg0)
FileDataSource fds =new FileDataSource("d:\\88364.gif"); //JAF 中的datesouce
gifBodyPart.setDataHandler(new DataHandler(fds));
gifBodyPart.setContentID("88364.gif");
//下面这句也可以替换上句
// gifBodyPart.setHeader("Content-ID", "88364.GIF");
// gifBodyPart.setHeader("Content-Type", "image/gif'); //因为已经包含在JAF 中,不用包含此句
multipart.addBodyPart(gifBodyPart);
//date
msg.setSentDate(new Date());
//subject
msg.setSubject(subject);
// msg.setText(body);
//设置HTML 格式的邮件正文
// msg.setContent(body, "text/html;charset=gb2312");
/*
* 将MimeMultipart对象设置为整个邮件的内容
*/
msg.setContent(multipart);
//保存最终生成的邮件内容
msg.saveChanges();
//把MIMEMessage 对象中的内容写到文件中
msg.writeTo(new FileOutputStream("d:\\testHTMLPicture.eml"));
}
}
3 html 方式:
package com.mail.main;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* @author xxxx
* @version 創建時間:Sep 26, 2009 2:22:39 PM
* 類說明:邮件发送程序实例
*/
public class HtmlMessageSender {
String protocol ="smtp";
String from ="it315_test@sina.com";
String to = "it315_test@sina.com";
String subject ="test picture welcome sender";
String body ="<h4>This is a test of HTML mail</h4><img src=\"../images/it315logo.gif\">";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String server = "smtp.sina.com.cn";
String user = "it315_test"; //jsp_mail@sina.com
String pass = "123456";
HtmlMessageSender sender = new HtmlMessageSender();
Session session =sender.createSession();
MimeMessage message = sender.createMessage(session);
//获取Transport 对象,并连接邮件服务器发送邮件
Transport transport = session.getTransport();
transport.connect(server, user, pass);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
public Session createSession() {
Properties props =new Properties();
props.setProperty("mail.transport.protocol", protocol);
props.setProperty("mail.smtp.auth", "true");
/*必须将mail.smtp.auth属性设置为true,SMTPTransport对象才会向
SMTP服务器提交用户认证信息,这个信息可以从JavaMail的javadocs文档
中的com.sun.mail.smtp包的帮助页面内查看到。
*/
Session session =Session.getDefaultInstance(props);
session.setDebug(true);
return session;
}
public MimeMessage createMessage(Session session) throws Exception, MessagingException{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
MimeMultipart multipart = new MimeMultipart("related");
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body,"text/html;charset=gb2312");
multipart.addBodyPart(htmlBodyPart);
MimeBodyPart gifBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("d:\\88364.gif");
gifBodyPart.setDataHandler(new DataHandler(fds));
gifBodyPart.setContentID("it315_logo_gif");
multipart.addBodyPart(gifBodyPart);
message.setContent(multipart);
message.saveChanges();
return message;
}
}
4 综合以上:
package com.mail.main;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* @author xxxx
* @version 創建時間:Sep 27, 2009 2:12:28 PM
* 類說明:内嵌图片和附件的mail
*/
public class ComplexMessage {
/**
* @param args
* @throws MessagingException
* @throws IOException
* @throws Exception
*/
public static void main(String[] args) throws Exception, IOException, MessagingException {
Session session = Session.getDefaultInstance(new Properties());
MimeMessage message = createMessage(session);
message.writeTo(new FileOutputStream("d:\\ComplexMessage.eml"));
}
public static MimeMessage createMessage(Session session) throws Exception
{
String from = "jsp_mail@sohu.com ";//发件人地址
String to = "jsp_mail@sohu.com "; //收件人地址
String subject = "HTML邮件"; //邮件主题
String body = "<a href=http://www.it315.org>" +
"欢迎大家访问我们的网站</a></br>" +
"<img src=\"cid:it315_logo_gif\">";
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject);
//创建代表邮件正文和附件的各个MimeBodyPart对象
MimeBodyPart contentPart = createContent(body,"d:\\attach\\logo.gif");
MimeBodyPart attachPart1 = createAttachment("d:\\attach\\daemon.exe");
MimeBodyPart attachPart2 = createAttachment("d:\\attach\\sndrec.wav");
//创建用于组合邮件正文和附件的MimeMultipart对象
MimeMultipart allMultipart = new MimeMultipart("mixed");
allMultipart.addBodyPart(contentPart);
allMultipart.addBodyPart(attachPart1);
allMultipart.addBodyPart(attachPart2);
//设置整个邮件内容为最终组合出的MimeMultipart对象
message.setContent(allMultipart);
message.saveChanges();
return message;
}
public static MimeBodyPart createContent(String body,String filename)
throws Exception
{
/* 创建代表组合MIME消息的MimeMultipart对象,
和将该MimeMultipart对象保存到的MimeBodyPart对象 */
MimeBodyPart contentPart = new MimeBodyPart();
MimeMultipart contentMultipart = new MimeMultipart("related");
/* 创建用于保存HTML正文的MimeBodyPart对象,
并将它保存到MimeMultipart中 */
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body,"text/html;charset=gb2312");
contentMultipart.addBodyPart(htmlBodyPart);
/* 创建用于保存图片的MimeBodyPart对象,
并将它保存到MimeMultipart中 */
MimeBodyPart gifBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filename);
gifBodyPart.setDataHandler(new DataHandler(fds));
gifBodyPart.setContentID("it315_logo_gif");
contentMultipart.addBodyPart(gifBodyPart);
//将MimeMultipart对象保存到的MimeBodyPart对象中
contentPart.setContent(contentMultipart);
return contentPart;
}
public static MimeBodyPart createAttachment(String filename) throws Exception
{
//创建保存附件的MimeBodyPart对象,并加入附件内容和相应信息
MimeBodyPart attachPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filename);
attachPart.setDataHandler(new DataHandler(fds));
attachPart.setFileName(fds.getName());
return attachPart;
}
}