21xrx.com
2024-11-08 21:10:26 Friday
登录
文章检索 我的文章 写文章
Java实现发送邮件并设置发件人图标
2023-06-14 16:22:08 深夜i     --     --
JavaMail API

在实际开发中,我们经常需要通过邮件进行通知或者调用第三方服务。Java提供了邮件发送的API,可以方便地实现发送邮件功能。本文将介绍如何使用Java发送邮件,并设置发件人图标。

首先,我们需要引入JavaMail邮件发送API和Java Activation Framework(JAF)相关包,具体可以在官网下载或者使用Maven来引入。

下面是一个使用JavaMail API和JAF进行发送邮件的示例代码:


public class EmailUtil {

  /**

   * 发送邮件

   * @param recipient 收件人邮箱

   * @param title 邮件标题

   * @param content 邮件内容

   * @param senderEmail 发件人邮箱

   * @param senderPwd 发件人邮箱密码

   * @throws Exception

   */

  public static void send(String recipient, String title, String content, String senderEmail, String senderPwd) throws Exception {

    Properties props = new Properties();

    props.setProperty("mail.smtp.host", "smtp.qq.com");

    props.setProperty("mail.smtp.auth", "true");

    props.setProperty("mail.smtp.port", "465");

    props.setProperty("mail.smtp.socketFactory.port", "465");

    props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    Session session = Session.getDefaultInstance(props, new Authenticator() {

      protected PasswordAuthentication getPasswordAuthentication() {

        return new PasswordAuthentication(senderEmail, senderPwd);

      }

    });

    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(senderEmail));

    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

    msg.setSubject(title);

    BodyPart messageBodyPart = new MimeBodyPart();

    messageBodyPart.setContent(content, "text/html;charset=UTF-8");

    Multipart multipart = new MimeMultipart();

    multipart.addBodyPart(messageBodyPart);

    msg.setContent(multipart);

    msg.setSentDate(new Date());

    Transport.send(msg);

  }

}

我们可以通过调用该方法来发送邮件,例如:


public class Main {

  public static void main(String[] args) {

    String recipient = "xxxxx@qq.com";

    String title = "邮件标题";

    String content = "邮件内容";

    String senderEmail = "abc@qq.com";

    String senderPwd = "abc123456";

    try {

      EmailUtil.send(recipient, title, content, senderEmail, senderPwd);

      System.out.println("邮件发送成功");

    } catch (Exception e) {

      e.printStackTrace();

      System.out.println("邮件发送失败:" + e.getMessage());

    }

  }

}

在设置发件人图标时,我们可以使用JavaMail的MimeMessage类的setContent方法来设置HTML内容。例如:


public class EmailUtil {

  /**

   * 发送邮件

   * @param recipient 收件人邮箱

   * @param title 邮件标题

   * @param content 邮件内容

   * @param iconUrl 发件人图标地址

   * @param senderEmail 发件人邮箱

   * @param senderPwd 发件人邮箱密码

   * @throws Exception

   */

  public static void send(String recipient, String title, String content, String iconUrl, String senderEmail, String senderPwd) throws Exception {

    Properties props = new Properties();

    props.setProperty("mail.smtp.host", "smtp.qq.com");

    props.setProperty("mail.smtp.auth", "true");

    props.setProperty("mail.smtp.port", "465");

    props.setProperty("mail.smtp.socketFactory.port", "465");

    props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    Session session = Session.getDefaultInstance(props, new Authenticator() {

      protected PasswordAuthentication getPasswordAuthentication() {

        return new PasswordAuthentication(senderEmail, senderPwd);

      }

    });

    MimeMessage msg = new MimeMessage(session);

    // 设置发件人

    msg.setFrom(new InternetAddress(senderEmail));

    // 设置收件人

    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

    // 邮件标题

    msg.setSubject(title, "UTF-8");

    // 设置带有发件人图标的HTML内容

    MimeMultipart allPart = new MimeMultipart("related");

    BodyPart messageBodyPart = new MimeBodyPart();

    messageBodyPart.setContent(content, "text/html;charset=UTF-8");

    // 设置发件人图标

    BodyPart image = new MimeBodyPart();

    URL url = new URL(iconUrl);

    URLConnection conn = url.openConnection();

    conn.connect();

    InputStream inputStream = conn.getInputStream();

    BufferedImage bufferedImage = ImageIO.read(inputStream);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    ImageIO.write(bufferedImage, "jpeg", out);

    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

    DataSource ds = new ByteArrayDataSource(in, "image/jpeg");

    image.setDataHandler(new DataHandler(ds));

    image.setHeader("Content-ID", "");

    // 图片和内容一起添加至Multipart

    allPart.addBodyPart(image);

    allPart.addBodyPart(messageBodyPart);

    msg.setContent(allPart);

    // 发送日期

    msg.setSentDate(new Date());

    // 发送邮件

    Transport.send(msg);

  }

}

我们也可以通过调用该方法来发送添加图标的邮件,例如:


public class Main {

  public static void main(String[] args) {

    String recipient = "xxxxx@qq.com";

    String title = "邮件标题";

    String content = "邮件内容";

    String iconUrl = "https://example.com/icon.jpg";

    String senderEmail = "abc@qq.com";

    String senderPwd = "abc123456";

    try {

      EmailUtil.send(recipient, title, content, iconUrl, senderEmail, senderPwd);

      System.out.println("邮件发送成功");

    } catch (Exception e) {

      e.printStackTrace();

      System.out.println("邮件发送失败:" + e.getMessage());

    }

  }

}

通过本文的介绍,我们可以利用JavaMail API和JAF来发送邮件,并且可以设置发件人图标,提高邮件的可读性和可视性。

、JAF、发送邮件、发件人图标、HTML内容

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复