Java Mail

public class MailSenderUtil {
   
   public static void send(String title,String []to,String text,List<File> files)throws Exception {
      JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
      mailSender.setHost("smpt.qq.com");
      mailSender.setPort(25);
      mailSender.setUsername("username");//个人邮箱
      mailSender.setPassword("password");//邮箱密码
      mailSender.setDefaultEncoding("UTF-8");
      
      Properties javaMailProperties = new Properties();
      javaMailProperties.setProperty("mail.smtp.sendpartial", "true");
      javaMailProperties.setProperty("mail.smtp.auth", "true");
      javaMailProperties.setProperty("mail.smtp.socketFactory.port", "465");
      javaMailProperties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      
      mailSender.setJavaMailProperties(javaMailProperties);
      
       MimeMessage msg = mailSender.createMimeMessage();
       MimeMessageHelper helper = new MimeMessageHelper(msg, true);
       helper.setFrom(mailSender.getUsername());
       helper.setTo(to);
       helper.setSubject(title);
       helper.setText(text);
       
       if (!CollectionUtil.isEmpty(files)) {
          for (File file : files) {
             helper.addAttachment(file.getName(), file);
         }
      }
       
       mailSender.send(msg);
   }
   
   public static void main(String[] args) throws Exception{
      String []MAIL_CC_LIST={"kakun.hsh@alibaba-inc.com","746334153@qq.com"};
      File file1 = new File("D:\\test.csv");
      File file2 = new File("D:\\test.xls");
      List<File> files = new ArrayList<File>();
      files.add(file1);
      files.add(file2);
      
      
      send("test", MAIL_CC_LIST, "my test",files);
   }
Java Mail 是一组用于发送和接收电子邮件的 API。它是 Java 标准版(Java SE)的一部分,因此不需要外部库或包。使用 Java Mail API,您可以从 Java 应用程序中发送电子邮件,也可以接收来自邮件服务器的电子邮件。Java Mail API 支持多种邮件传输协议,例如 SMTP、IMAP 和 POP3。 要使用 Java Mail API,您需要导入以下两个包: - javax.mail:这是 Java Mail API 的主要包,包含各种类和接口,用于发送和接收电子邮件。 - javax.mail.internet:这个包包含各种类和接口,用于处理邮件消息中的各种 MIME 类型,例如文本、HTML、图像等。 以下是一个简单的 Java Mail 示例,用于从 Gmail 发送电子邮件: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { // 收件人电子邮件地址 String to = "recipient@example.com"; // 发件人电子邮件地址 String from = "sender@example.com"; // Gmail SMTP 服务器地址 String host = "smtp.gmail.com"; // Gmail SMTP 服务器端口 int port = 587; // 收件人邮箱密码(如果需要的话) String password = "recipient_password"; // 配置 Java Mail 属性 Properties properties = System.getProperties(); properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", port); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); // 创建一个新的 Session 对象 Session session = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { // 创建一个新的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // 设置发件人和收件人 message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置电子邮件主题和正文 message.setSubject("Java Mail Example"); message.setText("This is a Java Mail example message."); // 发送消息 Transport.send(message); System.out.println("Message sent successfully!"); } catch (MessagingException mex) { mex.printStackTrace(); } } } ``` 在上面的示例中,我们使用 Gmail SMTP 服务器发送电子邮件。要使用其他邮件服务器,您需要更改 host 和 port 属性以及身份验证信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值