package mail_page;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
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;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
/**
* @author Administrator
*/
public class Mail_class {
String toUser= ""; // 收信人地址
String toName= ""; // 收信人姓名
String subject= "测试邮件...."; // 主题
String body= "这是第三题 带有附件的题目 "; // 内容
String password= ""; //密码
String fromUser= ""; //发件人的地址
String fromName= ""; //名字
String toFile1 = "f:\\xuwenjinmsn.doc";
String toFile3 = "f:\\taihe.jpg";
// 发送 文本
private void sendSimpleEmail() {
try {
SimpleEmail email = new SimpleEmail();
email.setHostName("mail.163.com");// 邮件服务器
email.setAuthentication(fromUser, password);// smtp认证的用户名和密码
email.addTo(toUser, toName);// 收信者
email.setFrom(fromUser, fromName);// 发信者
email.setSubject(subject);// 标题
email.setCharset("UTF-8");// 编码格式
email.setMsg(" 第一题 我是 纯 text 文本的形式 发送的 ");// 内容
email.send();// 发送
}
catch (EmailException e) {
e.printStackTrace();
}
}
/*
* 带 图片的 html 文件
* 第二题
* 自己的机子 上可以 看得到本地图片 可是不是自己的机子 就看不到了。
*
*/
private void sendHtml() throws MessagingException {
String from = ""; // 发件人地址
String to = ""; // 收件人地址
String subject = "测试发送第二道 题的邮件"; // 主题
String text = "<h1>this is a test 啊 </h1><img src=\"http://www.rgagnon.com/images/jht.gif\">"; //内容
String host = "mail.taiheinfo.net"; // 邮件服务器地址
int port = 25; // 邮件服务器地址端口,默认为25
String username = ""; // 用户名
String password = ""; // 密码
Properties props = new Properties(); //new 系统对象,设置系统属性
props.put("mail.transport.protocol", "smtp"); // 使用smtp协议
props.put("mail.smtp.auth", "true"); // 是否需要邮件认证
//props.put("mail.debug", "true"); 在控制面板中显示
Session mailSession = Session.getDefaultInstance(props); //获得邮件回话对象
mailSession.setDebug(true); //主要是利于调试,默认为false
//创建邮件对象
Message newMessage = new MimeMessage(mailSession);
newMessage.setFrom(new InternetAddress(username)); //设置发件人
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //type表示接受者类型,设置收件人
newMessage.setSubject(subject); //主题
newMessage.setSentDate(new Date()); //日期
//创建一个Multipart容器
MimeMultipart multipart = new MimeMultipart("related");
//实例化一个BodyPart来封装内容
BodyPart bodyPart1=new MimeBodyPart();
bodyPart1.setContent(text, "text/html;charset=utf-8");
//将BodyPart添加到MulTipart
multipart.addBodyPart(bodyPart1);
newMessage.setContent(multipart);
Transport transport=mailSession.getTransport(); //创建Transport
transport.connect(host, port, username, password);//连接邮件服务器
transport.sendMessage(newMessage,newMessage.getRecipients(Message.RecipientType.TO)); //发送邮件
transport.close();
}
//添加附件
private void sendHtmlEmails() throws AddressException, MessagingException {
File file=new File("f:/taihe.jpg");
String from = " "; // 发件人地址
String to = " "; // 收件人地址
String subject = "测试发送第三道 题的邮件"; // 主题
String text = "<h1>这是第三封邮件 带有附件的</h1>"; //内容
String host = ""; // 邮件服务器地址
int port = 25; // 邮件服务器地址端口,默认为25
String username = ""; // 用户名
String password = ""; // 密码
Properties props = new Properties(); //new 系统对象,设置系统属性
props.put("mail.transport.protocol", "smtp"); // 使用smtp协议
props.put("mail.smtp.auth", "true"); // 是否需要邮件认证
//props.put("mail.debug", "true"); 在控制面板中显示
Session mailSession = Session.getDefaultInstance(props); //获得邮件回话对象
//mailSession.setDebug(true); //主要是利于调试,默认为false
//创建邮件对象
Message newMessage = new MimeMessage(mailSession);
newMessage.setFrom(new InternetAddress(username)); //设置发件人
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //type表示接受者类型,设置收件人
newMessage.setSubject(subject); //主题
newMessage.setSentDate(new Date()); //日期
//创建一个Multipart容器
MimeMultipart multipart = new MimeMultipart("related");
//实例化一个BodyPart来封装内容
BodyPart bodyPart1=new MimeBodyPart();
bodyPart1.setContent(text, "text/html;charset=gbk");
//将BodyPart添加到MulTipart
//每个附件需要实例化一个bodyPart
BodyPart bodyPart2=new MimeBodyPart();
//实例dataSource(来自jaf),参数为文件的地址file.getAbsolutePath()//得到file文件的地址
DataSource dataSource = new FileDataSource(file.getAbsolutePath());
//使用datasource实例化datahandler
DataHandler datahandler=new DataHandler(dataSource);
bodyPart2.setDataHandler(datahandler);
bodyPart2.setHeader("Content-ID","<image>");
//设置附件标题,使用MimeUtility进行名字转码,否则接收到的是乱码
try {
bodyPart2.setFileName(javax.mail.internet.MimeUtility.encodeText("带图片附件的邮件"));
multipart.addBodyPart(bodyPart1);
multipart.addBodyPart(bodyPart2);
newMessage.setContent(multipart);
Transport transport=mailSession.getTransport(); //创建Transport
transport.connect(host, port, username, password);//连接邮件服务器
transport.sendMessage(newMessage,newMessage.getRecipients(Message.RecipientType.TO)); //发送邮件
transport.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
//最后一道题目
public void four() throws AddressException, MessagingException, IOException {
File file=new File("f:/taihe2.gif");
String from = ""; // 发件人地址
String to = ""; // 收件人地址
String subject = "测试发送第四道 题的邮件"; // 主题
//这个图片的地址必须是共享的 就是任何一台机子都能访问到的 可是 能不能把本地的图片 挂在服务器上让后收邮件的 可以从服务器上访问 并下载 然后服务器上的资源就销毁
String text = "<h1>测试发送第四道 题的邮件</h1><img src=\"http://images.cnblogs.com/cnblogs_com/Terrylee/WindowsLiveWriter/240jQuery_128F5/jquery_2.gif\">"; //内容
String host = ""; // 邮件服务器地址
int port = 25; // 邮件服务器地址端口,默认为25
String username = ""; // 用户名
String password = ""; // 密码
Properties props = new Properties(); //new 系统对象,设置系统属性
props.put("mail.transport.protocol", "smtp"); // 使用smtp协议
props.put("mail.smtp.auth", "true"); // 是否需要邮件认证
//props.put("mail.debug", "true"); 在控制面板中显示
Session mailSession = Session.getDefaultInstance(props); //获得邮件回话对象
mailSession.setDebug(true); //主要是利于调试,默认为false
//创建邮件对象
Message newMessage = new MimeMessage(mailSession);
newMessage.setFrom(new InternetAddress(username)); //设置发件人
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //type表示接受者类型,设置收件人
newMessage.setSubject(subject); //主题
newMessage.setSentDate(new Date()); //日期
//创建一个Multipart容器
MimeMultipart multipart = new MimeMultipart("related");
//实例化一个BodyPart来封装内容
BodyPart bodyPart1=new MimeBodyPart();
bodyPart1.setContent(text, "text/html;charset=gbk");
//将BodyPart添加到MulTipart
//每个附件需要实例化一个bodyPart
BodyPart bodyPart2=new MimeBodyPart();
//实例dataSource(来自ja