栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现邮件发送QQ邮箱带附件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现邮件发送QQ邮箱带附件

本文实例为大家分享了Java实现邮件发送QQ邮箱带附件的具体代码,供大家参考,具体内容如下

添加依赖


 
  javax.mail
  mail
  1.4.7

关键代码

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
 
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
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;
 

public class SendMail {
 
 
 public static void sendQQMail(String formMail, String descMail, String subject, String content, File[] files,
   String contentType, String password) throws MessagingException, UnsupportedEncodingException {
  Properties properties = new Properties();
  properties.setProperty("mail.smtp.host", "smtp.qq.com");
  properties.setProperty("mail.smtp.port", "465");
  properties.setProperty("mail.smtp.auth", "true");
  properties.setProperty("mail.debug", "true");
  properties.setProperty("mail.transport.protocol", "smtp");
  properties.setProperty("mail.smtp.ssl.enable", "true");
  Session session = Session.getInstance(properties, new Authenticator() {
   @Override
   protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(formMail, password);
   }
  });
  Message message = new MimeMessage(session);
  try {
   message.setFrom(new InternetAddress(formMail));
   message.setRecipient(Message.RecipientType.TO, new InternetAddress(descMail));
   message.setSubject(subject);
 
   // 是否存在附件
   if (null != files && files.length > 0) {
    MimeMultipart multipart = new MimeMultipart();
 
    BodyPart contentPart = new MimeBodyPart();
    contentPart.setContent(content, contentType);
    multipart.addBodyPart(contentPart);
 
    for (File file : files) {
     MimeBodyPart attachment = new MimeBodyPart();
     DataHandler dh2 = new DataHandler(new FileDataSource(file));
     attachment.setDataHandler(dh2);
     attachment.setFileName(MimeUtility.encodeText(dh2.getName()));
     multipart.addBodyPart(attachment);
    }
    multipart.setSubType("mixed");
 
    message.setContent(multipart);
    message.saveChanges();
   }
   // 普通
   else {
    message.setContent(content, contentType);
   }
 
   Transport transport = session.getTransport();
   transport.connect(formMail, password);
   Transport.send(message);
  } catch (UnsupportedEncodingException e) {
   throw e;
  } catch (NoSuchProviderException e) {
   throw e;
  } catch (MessagingException e) {
   throw e;
  }
 
 }
 
 public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
  // 由哪个邮箱发送
  String formMail = "********@qq.com";
  // QQ邮箱>设置>账户 开启POP3/SMTP服务 查看smtp密码
  String smtpPassword = "****************";
 
  // 发送人邮箱地址
  String descMail = "470947852@qq.com";
  String contentType = "text/html;charset=UTF-8";
 
  String subject = "测试邮件发送,含附件";
  String content = "test send mail, 这里是中文";
  File[] files = new File[2];
  files[0] = new File("C:/test_1.xls");
  files[1] = new File("C:/test_2.xls");
 
  SendMail.sendQQMail(formMail, descMail, subject, content, files, contentType, smtpPassword);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134929.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号