利用javaMail创建一个简单邮件
代码如下:
public static void main(String[] args) throws Exception{
//1、创建邮件
//设置属性
Properties props = new Properties();
//创建session
Session session = Session.getInstance(props);
//创建message邮件
MimeMessage message = new MimeMessage(session);
//2、设置邮件属性
//邮件来自哪
message.setFrom(new InternetAddress("[email protected]"));
//邮件发到哪
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
//邮件的主题
message.setSubject("Hi");
//3、正文
MimeBodyPart part = new MimeBodyPart();
part.setContent("你知道我是谁不?", "text/html;charset=gb2312");
//4、描述关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(part);
//设置
message.setContent(mm);
message.saveChanges();
message.writeTo(new FileOutputStream("D://1.eml"));
}
利用JavaMail创建一个包含图片的邮件
代码如下:
public class MimeImageMail {
public static void main(String[] args)throws Exception {
//1、创建邮件
//MimeMessage message = new MimeMessage(Session.getInstance(new Properties()));
//设置属性
Properties props=new Properties();
//创建session对象
Session session=Session.getInstance(props);
//创建邮件对象
MimeMessage message=new MimeMessage(session);
//2、设置邮件属性
//邮件来自哪message.setFrom(new InternetAddress(""));
message.setFrom(new InternetAddress("[email protected]"));
//邮件发去哪message.setRecipient(Message.RecipientType.TO, new InternetAddress(""));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
//主题message.setSubject("");
message.setSubject("看看这是谁啊!");
//3、创建正文MimeBodyPart text=new MimeBodyPart();
MimeBodyPart text = new MimeBodyPart();
text.setContent("呵呵,这小妞长的真俊啊,谁家的啊!<br/><br/><img src='cid:xx.jpg'>是不错吧", "text/html;charset=utf-8");
//4、创建一个图片MimeBodyPart image=new MimeBodyPart();
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src//f.jpg")));
image.setContentID("xx.jpg");
//创建描述关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
//设置关系
mm.setSubType("related");//表示消息体中的内容是关联(依赖)组合类型,表示消息体中的某些内容,依赖另外的资源。
message.setContent(mm);
message.saveChanges();
message.writeTo(new FileOutputStream("D://2.eml"));
}
}
利用JavaMail创建一个包含附件的邮件
代码如下:
public class AttchMime {
public static void main(String[] args)throws Exception {
//1、创建邮件
MimeMessage message = new MimeMessage(Session.getInstance(new Properties()));
//2、设置邮件属性
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("听听这首音乐");
//3、正文
MimeBodyPart text= new MimeBodyPart();
//文本的设置
text.setContent("我这有一首很好听的音乐,一起分享一下吧<br/><br/><br/>如果感觉不错就回复一下吧", "text/html;charset=utf-8");
//附件
MimeBodyPart attch= new MimeBodyPart();
//附件的设置
attch.setDataHandler(new DataHandler(new FileDataSource("src//1.mp3")));
attch.setFileName("1.mp3");
//4、描述关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(attch);
//设置关系
mm.setSubType("mixed");//表示消息体中的内容是混合组合类型,可以是文本,声音和附件等不同邮件内容的混合体。
message.setContent(mm);
message.saveChanges();
message.writeTo(new FileOutputStream("d://3.eml"));
}
}
利用javaMail创建一个包含两张图片的邮件
代码如下:
public class MimeImage {
public static void main(String[] args)throws Exception {
//创建邮件
MimeMessage message=new MimeMessage(Session.getInstance(new Properties()));
//设置属性
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, "[email protected]");
message.setSubject("好多图片啊");
//设置正文
MimeBodyPart text=new MimeBodyPart();
text.setContent("这有好多图片<br/><img src='cid:xx.jpg'><br/><img src='cid:yy.jpg'><br/>选一张吧", "text/html;charset=utf-8");
//设置图片
MimeBodyPart image=new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src//f.jpg")));
image.setContentID("xx.jpg");
MimeBodyPart image1=new MimeBodyPart();
image1.setDataHandler(new DataHandler(new FileDataSource("src//x.jpg")));
image1.setContentID("yy.jpg");
//描述关系
//图片与正文的描述关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.addBodyPart(image1);
mm.setSubType("related");
message.setContent(mm);
message.saveChanges();
message.writeTo(new FileOutputStream("d://4.eml"));
}
}
利用JavaMail创建一个既包含图片又包含附件的邮件
代码如下:
public class MimeImageAttch {
public static void main(String[] args)throws Exception {
//1、创建邮件
MimeMessage message=new MimeMessage(Session.getInstance(new Properties()));
//2、设置属性
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, "[email protected]");
message.setSubject("既有图片又有附件");
//3、设置正文
MimeBodyPart text=new MimeBodyPart();
text.setContent("这有好多图片<br/><img src='cid:xx.jpg'><br/><img src='cid:yy.jpg'><br/>选一张吧", "text/html;charset=utf-8");
//4、设置图片
MimeBodyPart image=new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src//f.jpg")));
image.setContentID("xx.jpg");
MimeBodyPart image1=new MimeBodyPart();
image1.setDataHandler(new DataHandler(new FileDataSource("src//x.jpg")));
image1.setContentID("yy.jpg");
//5、设置附件
MimeBodyPart attch = new MimeBodyPart();
DataHandler dh1 = new DataHandler(new FileDataSource("src//大笑.mp3"));
attch.setDataHandler(dh1);
String filename = dh1.getName();
attch.setFileName(MimeUtility.encodeText(filename));
//6、描述关系
//图片与正文的描述关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.addBodyPart(image1);
mm.setSubType("related");
//图片与正文的 bodypart
MimeBodyPart part = new MimeBodyPart();
part.setContent(mm);
//附件与以上的bodypart关系
MimeMultipart m = new MimeMultipart();
m.addBodyPart(part);
m.addBodyPart(attch);
m.setSubType("mixed");
//把MIME消息设置到message中
message.setContent(m);
message.saveChanges();
message.writeTo(new FileOutputStream("d://5.eml"));
}
}
//附件
MimeBodyPart attch= new MimeBodyPart();
//附件的设置
attch.setDataHandler(new DataHandler(new FileDataSource("src//1.mp3")));
attch.setFileName("1.mp3");
如果上边代码中的附件标题是中文的就会出现乱码问题,下面的代码就解决了乱码问题
//附件
MimeBodyPart attch = new MimeBodyPart();
DataHandler dh1 = new DataHandler(new FileDataSource("src//大笑.mp3"));
attch.setDataHandler(dh1);
String filename = dh1.getName();
attch.setFileName(MimeUtility.encodeText(filename));