java mail 邮箱发送
引用自http://www.cnblogs.com/xdp-gacl/p/4220190.html
package com.longcai.simple;
import java.io.FileOutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
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 javax.mail.internet.MimePart;
import javax.swing.plaf.basic.BasicDirectoryModel;
public class SendImaMail {
public static void main(String[] args) throws Exception {
Properties properties=new Properties();
properties.setProperty("mail.host", "smtp.sohu.com");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth","true");
Session session=Session.getInstance(properties);
session.setDebug(true);
Transport ts=session.getTransport();
ts.connect("smtp.sohu.com", "sohu588168", "dzj8013168");
Message message = createMixedMail(session);
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
//图片和附件一起发送
private static Message createMixedMail(Session session) throws Exception {
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress("sohu588168@sohu.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("275002199@qq.com"));
message.setSubject("带图片的邮件");
MimeBodyPart text=new MimeBodyPart();
text.setContent("xxx<img src='cid:aaa.jpg'/>","text/html;charset=utf-8");
MimeBodyPart img=new MimeBodyPart();
img.setDataHandler(new DataHandler(new FileDataSource("C:\\Users\\Administrator\\Desktop\\444.jpg")));
img.setContentID("aaa.jpg");
MimeBodyPart attach=new MimeBodyPart();
DataHandler dh=new DataHandler(new FileDataSource("C:\\Users\\Administrator\\Desktop\\333.jpg"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
MimeMultipart mp1=new MimeMultipart();
mp1.addBodyPart(text);
mp1.addBodyPart(img);
mp1.setSubType("related");
MimeMultipart mp2=new MimeMultipart();
mp2.addBodyPart(attach);
mp2.setSubType("mixed");
MimeBodyPart content =new MimeBodyPart();
content.setContent(mp1);
mp2.addBodyPart(content);
message.setContent(mp2);
message.saveChanges();;
return message;
}
//发送简单文本
private static Message createSimpleMail(Session session) throws Exception {
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("sohu588168@sohu.com"));
//to 代表发送 还有cc抄送等等
message.setRecipient(Message.RecipientType.TO, new InternetAddress("275002199@qq.com"));
message.setSubject("简单的标题");
message.setContent("你好,第一封邮件", "text/html;charset=utf-8");
return message;
}
//创建带图片的文件
private static Message createImageMail(Session session) throws Exception {
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("sohu588168@sohu.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("275002199@qq.com"));
message.setSubject("带图片的邮件");
MimeBodyPart text=new MimeBodyPart();
//这里的img必须有,才可以在邮件中显示出来图片
text.setContent("这是一封带图片的<img src='cid:xxx.jpg'>邮件","text/html;charset=utf-8");
MimeBodyPart image =new MimeBodyPart();
//可以是src\\22.jpg 也可以是当前桌面路径
DataHandler dh=new DataHandler(new FileDataSource("C:\\Users\\Administrator\\Desktop\\333.jpg"));
image.setDataHandler(dh);
image.setContentID("xxx.jpg");
MimeMultipart mm=new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
message.setContent(mm);
message.saveChanges();
message.writeTo(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\222.eml"));
return message;
}
private static Message createAttachMail(Session session) throws Exception {
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("sohu588168@sohu.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("275002199@qq.com"));
message.setSubject("JavaMail邮件发送测试");
MimeBodyPart text=new MimeBodyPart();
text.setContent("使用JavaMail创建的带附件的邮件", "text/html;charset=UTF-8");
MimeBodyPart attach =new MimeBodyPart();
DataHandler dh=new DataHandler(new FileDataSource("C:\\Users\\Administrator\\Desktop\\555.zip"));
attach.setDataHandler(dh);
//图片是ContentID 附件是setFileName
attach.setFileName(dh.getName());
MimeMultipart mm=new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(attach);
//如果是附件需要改这个
mm.setSubType("mixed");
message.setContent(mm);
message.saveChanges();
message.writeTo(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\333.eml"));
return message;
}
}