最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Java实现文本、图片、附件复杂的邮件发送

互联网 admin 1浏览 0评论

Java实现文本、图片、附件复杂的邮件发送

public class SendComplexEmail {public static void main(String[] args) throws GeneralSecurityException, MessagingException {Properties prop = new Properties();prop.setProperty("mail.host", "smtp.qq.com");  设置QQ邮件服务器prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码// QQ邮箱设置SSL加密MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);prop.put("mail.smtp.ssl.enable", "true");prop.put("mail.smtp.ssl.socketFactory", sf);//1、创建定义整个应用程序所需的环境信息的 Session 对象Session session = Session.getDefaultInstance(prop, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {//传入发件人的姓名和授权码return new PasswordAuthentication("发送人的邮箱@qq.com","发送人的授权码");}});//2、通过session获取transport对象Transport transport = session.getTransport();//3、通过transport对象邮箱用户名和授权码连接邮箱服务器transport.connect("smtp.qq.com","发送人的邮箱@qq.com","发送人的授权码");//4、创建邮件,传入session对象MimeMessage mimeMessage = complexEmail(session);//5、发送邮件transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());//6、关闭连接transport.close();}public static MimeMessage complexEmail(Session session) throws MessagingException {//消息的固定信息MimeMessage mimeMessage = new MimeMessage(session);//发件人mimeMessage.setFrom(new InternetAddress("发送人的邮箱@qq.com"));//收件人mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("收件人的邮箱@qq.com"));//邮件标题mimeMessage.setSubject("带图片和附件的邮件");//邮件内容//准备图片数据MimeBodyPart image = new MimeBodyPart();DataHandler handler = new DataHandler(new FileDataSource("E:\\image\\01.jpg"));image.setDataHandler(handler);image.setContentID("test.png"); //设置图片id//准备文本MimeBodyPart text = new MimeBodyPart();text.setContent("这是一段文本<img src='cid:test.png'>","text/html;charset=utf-8");//附件MimeBodyPart appendix = new MimeBodyPart();appendix.setDataHandler(new DataHandler(new FileDataSource("E:\\image\\email.txt")));appendix.setFileName("test.txt");//拼装邮件正文MimeMultipart mimeMultipart = new MimeMultipart();mimeMultipart.addBodyPart(image);mimeMultipart.addBodyPart(text);mimeMultipart.setSubType("related");//文本和图片内嵌成功//将拼装好的正文内容设置为主体MimeBodyPart contentText = new MimeBodyPart();contentText.setContent(mimeMultipart);//拼接附件MimeMultipart allFile = new MimeMultipart();allFile.addBodyPart(appendix);//附件allFile.addBodyPart(contentText);//正文allFile.setSubType("mixed"); //正文和附件都存在邮件中,所有类型设置为mixed//放到Message消息中mimeMessage.setContent(allFile);mimeMessage.saveChanges();//保存修改return mimeMessage;}
}

 

Java实现文本、图片、附件复杂的邮件发送

public class SendComplexEmail {public static void main(String[] args) throws GeneralSecurityException, MessagingException {Properties prop = new Properties();prop.setProperty("mail.host", "smtp.qq.com");  设置QQ邮件服务器prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码// QQ邮箱设置SSL加密MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);prop.put("mail.smtp.ssl.enable", "true");prop.put("mail.smtp.ssl.socketFactory", sf);//1、创建定义整个应用程序所需的环境信息的 Session 对象Session session = Session.getDefaultInstance(prop, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {//传入发件人的姓名和授权码return new PasswordAuthentication("发送人的邮箱@qq.com","发送人的授权码");}});//2、通过session获取transport对象Transport transport = session.getTransport();//3、通过transport对象邮箱用户名和授权码连接邮箱服务器transport.connect("smtp.qq.com","发送人的邮箱@qq.com","发送人的授权码");//4、创建邮件,传入session对象MimeMessage mimeMessage = complexEmail(session);//5、发送邮件transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());//6、关闭连接transport.close();}public static MimeMessage complexEmail(Session session) throws MessagingException {//消息的固定信息MimeMessage mimeMessage = new MimeMessage(session);//发件人mimeMessage.setFrom(new InternetAddress("发送人的邮箱@qq.com"));//收件人mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("收件人的邮箱@qq.com"));//邮件标题mimeMessage.setSubject("带图片和附件的邮件");//邮件内容//准备图片数据MimeBodyPart image = new MimeBodyPart();DataHandler handler = new DataHandler(new FileDataSource("E:\\image\\01.jpg"));image.setDataHandler(handler);image.setContentID("test.png"); //设置图片id//准备文本MimeBodyPart text = new MimeBodyPart();text.setContent("这是一段文本<img src='cid:test.png'>","text/html;charset=utf-8");//附件MimeBodyPart appendix = new MimeBodyPart();appendix.setDataHandler(new DataHandler(new FileDataSource("E:\\image\\email.txt")));appendix.setFileName("test.txt");//拼装邮件正文MimeMultipart mimeMultipart = new MimeMultipart();mimeMultipart.addBodyPart(image);mimeMultipart.addBodyPart(text);mimeMultipart.setSubType("related");//文本和图片内嵌成功//将拼装好的正文内容设置为主体MimeBodyPart contentText = new MimeBodyPart();contentText.setContent(mimeMultipart);//拼接附件MimeMultipart allFile = new MimeMultipart();allFile.addBodyPart(appendix);//附件allFile.addBodyPart(contentText);//正文allFile.setSubType("mixed"); //正文和附件都存在邮件中,所有类型设置为mixed//放到Message消息中mimeMessage.setContent(allFile);mimeMessage.saveChanges();//保存修改return mimeMessage;}
}

 

11136
发布评论

评论列表 (0)

  1. 暂无评论