当前位置:
首页 >
emil 的使用
发布时间:2024/1/1
50
豆豆
摘抄自别人
RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封简单的邮件包含邮件头和邮件体两个部分,邮件头和邮件体之间使用空行分隔。
邮件头包含的内容有:
一个简单的邮件
package me.gacl.main;import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;/** * @ClassName: Sendmail * @Description: 发送Email * @author: 孤傲苍狼 * @date: 2015-1-12 下午9:42:56 * */ public class Sendmail {/*** @param args* @throws Exception */public static void main(String[] args) throws Exception {Properties prop = new Properties();prop.setProperty("mail.host", "smtp.sohu.com");prop.setProperty("mail.transport.protocol", "smtp");prop.setProperty("mail.smtp.auth", "true");//使用JavaMail发送邮件的5个步骤//1、创建sessionSession session = Session.getInstance(prop);//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态session.setDebug(true);//2、通过session得到transport对象Transport ts = session.getTransport();//3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。ts.connect("smtp.sohu.com", "gacl", "邮箱密码");//4、创建邮件Message message = createSimpleMail(session);//5、发送邮件 ts.sendMessage(message, message.getAllRecipients());ts.close();}/*** @Method: createSimpleMail* @Description: 创建一封只包含文本的邮件* @Anthor:孤傲苍狼** @param session* @return* @throws Exception*/ public static MimeMessage createSimpleMail(Session session)throws Exception {//创建邮件对象MimeMessage message = new MimeMessage(session);//指明邮件的发件人message.setFrom(new InternetAddress("gacl@sohu.com"));//指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com"));//邮件的标题message.setSubject("只包含文本的简单邮件");//邮件的文本内容message.setContent("你好啊!", "text/html;charset=UTF-8");//返回创建好的邮件对象return message;} }
包含内镶图片的邮件
package me.gacl.main;import java.io.FileOutputStream; import java.util.Properties;import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart;/** * @ClassName: Sendmail * @Description: 发送Email * @author: 孤傲苍狼 * @date: 2015-1-12 下午9:42:56 * */ public class Sendmail {/*** @param args* @throws Exception */public static void main(String[] args) throws Exception {Properties prop = new Properties();prop.setProperty("mail.host", "smtp.sohu.com");prop.setProperty("mail.transport.protocol", "smtp");prop.setProperty("mail.smtp.auth", "true");//使用JavaMail发送邮件的5个步骤//1、创建sessionSession session = Session.getInstance(prop);//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态session.setDebug(true);//2、通过session得到transport对象Transport ts = session.getTransport();//3、连上邮件服务器,需要发件人提供邮箱的用户名和密码进行验证ts.connect("smtp.sohu.com", "gacl", "邮箱密码");//4、创建邮件Message message = createImageMail(session);//5、发送邮件 ts.sendMessage(message, message.getAllRecipients());ts.close();}/*** @Method: createImageMail* @Description: 生成一封邮件正文带图片的邮件* @Anthor:孤傲苍狼** @param session* @return* @throws Exception*/ public static MimeMessage createImageMail(Session session) throws Exception {//创建邮件MimeMessage message = new MimeMessage(session);// 设置邮件的基本信息//发件人message.setFrom(new InternetAddress("gacl@sohu.com"));//收件人message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));//邮件标题message.setSubject("带图片的邮件");// 准备邮件数据// 准备邮件正文数据MimeBodyPart text = new MimeBodyPart();text.setContent("这是一封邮件正文带图片<img src='cid:xxx.jpg'>的邮件", "text/html;charset=UTF-8");// 准备图片数据MimeBodyPart image = new MimeBodyPart();DataHandler dh = new DataHandler(new FileDataSource("src\\1.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();//将创建好的邮件写入到E盘以文件的形式进行保存message.writeTo(new FileOutputStream("E:\\ImageMail.eml"));//返回创建好的邮件return message;} }
包含附件的邮件
package me.gacl.main;import java.io.FileOutputStream; import java.util.Properties;import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart;/** * @ClassName: Sendmail * @Description: 发送Email * @author: 孤傲苍狼 * @date: 2015-1-12 下午9:42:56 * */ public class Sendmail {/*** @param args* @throws Exception */public static void main(String[] args) throws Exception {Properties prop = new Properties();prop.setProperty("mail.host", "smtp.sohu.com");prop.setProperty("mail.transport.protocol", "smtp");prop.setProperty("mail.smtp.auth", "true");//使用JavaMail发送邮件的5个步骤//1、创建sessionSession session = Session.getInstance(prop);//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态session.setDebug(true);//2、通过session得到transport对象Transport ts = session.getTransport();//3、连上邮件服务器ts.connect("smtp.sohu.com", "gacl", "邮箱密码");//4、创建邮件Message message = createAttachMail(session);//5、发送邮件 ts.sendMessage(message, message.getAllRecipients());ts.close();}/*** @Method: createAttachMail* @Description: 创建一封带附件的邮件* @Anthor:孤傲苍狼** @param session* @return* @throws Exception*/ public static MimeMessage createAttachMail(Session session) throws Exception{MimeMessage message = new MimeMessage(session);//设置邮件的基本信息//发件人message.setFrom(new InternetAddress("gacl@sohu.com"));//收件人message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));//邮件标题message.setSubject("JavaMail邮件发送测试");//创建邮件正文,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码MimeBodyPart text = new MimeBodyPart();text.setContent("使用JavaMail创建的带附件的邮件", "text/html;charset=UTF-8");//创建邮件附件MimeBodyPart attach = new MimeBodyPart();DataHandler dh = new DataHandler(new FileDataSource("src\\2.jpg"));attach.setDataHandler(dh);attach.setFileName(dh.getName()); ////创建容器描述数据关系MimeMultipart mp = new MimeMultipart();mp.addBodyPart(text);mp.addBodyPart(attach);mp.setSubType("mixed");message.setContent(mp);message.saveChanges();//将创建的Email写入到E盘存储message.writeTo(new FileOutputStream("E:\\attachMail.eml"));//返回生成的邮件return message;} }
一个复杂的邮件
package me.gacl.main;import java.io.FileOutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; 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;/** * @ClassName: Sendmail * @Description: 发送Email * @author: 孤傲苍狼 * @date: 2015-1-12 下午9:42:56 * */ public class Sendmail {/*** @param args* @throws Exception */public static void main(String[] args) throws Exception {Properties prop = new Properties();prop.setProperty("mail.host", "smtp.sohu.com");prop.setProperty("mail.transport.protocol", "smtp");prop.setProperty("mail.smtp.auth", "true");//使用JavaMail发送邮件的5个步骤//1、创建sessionSession session = Session.getInstance(prop);//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态session.setDebug(true);//2、通过session得到transport对象Transport ts = session.getTransport();//3、连上邮件服务器ts.connect("smtp.sohu.com", "gacl", "邮箱密码");//4、创建邮件Message message = createMixedMail(session);//5、发送邮件 ts.sendMessage(message, message.getAllRecipients());ts.close();}/*** @Method: createMixedMail* @Description: 生成一封带附件和带图片的邮件* @Anthor:孤傲苍狼** @param session* @return* @throws Exception*/ public static MimeMessage createMixedMail(Session session) throws Exception {//创建邮件MimeMessage message = new MimeMessage(session);//设置邮件的基本信息message.setFrom(new InternetAddress("gacl@sohu.com"));message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));message.setSubject("带附件和带图片的的邮件");//正文MimeBodyPart text = new MimeBodyPart();text.setContent("xxx这是女的xxxx<br/><img src='cid:aaa.jpg'>","text/html;charset=UTF-8");//图片MimeBodyPart image = new MimeBodyPart();image.setDataHandler(new DataHandler(new FileDataSource("src\\3.jpg")));image.setContentID("aaa.jpg");//附件1MimeBodyPart attach = new MimeBodyPart();DataHandler dh = new DataHandler(new FileDataSource("src\\4.zip"));attach.setDataHandler(dh);attach.setFileName(dh.getName());//附件2MimeBodyPart attach2 = new MimeBodyPart();DataHandler dh2 = new DataHandler(new FileDataSource("src\\波子.zip"));attach2.setDataHandler(dh2);attach2.setFileName(MimeUtility.encodeText(dh2.getName()));//描述关系:正文和图片MimeMultipart mp1 = new MimeMultipart();mp1.addBodyPart(text);mp1.addBodyPart(image);mp1.setSubType("related");//描述关系:正文和附件MimeMultipart mp2 = new MimeMultipart();mp2.addBodyPart(attach);mp2.addBodyPart(attach2);//代表正文的bodypartMimeBodyPart content = new MimeBodyPart();content.setContent(mp1);mp2.addBodyPart(content);mp2.setSubType("mixed");message.setContent(mp2);message.saveChanges();message.writeTo(new FileOutputStream("E:\\MixedMail.eml"));//返回创建好的的邮件return message;} }
转载于:https://www.cnblogs.com/chengyangyang/p/9816318.html
总结
- 上一篇: GBase 8a 数据导入导出
- 下一篇: 数字化住宅小区对计算机网络有需求,数字化