Docs
Email

Email

You can know how to use resend/SMTP to send email.

Sending Emails with Resend

This guide will walk you through integrating the Resend email service to send emails from your Next.js project.

Prerequisites

Ensure you have a Resend account and an API key.

Set Up API Key

Add your Resend API key to your environment variables by adding it to the .env.local file:

RESEND_API_KEY=your_resend_api_key

Verify the from Address

You need to verify your sender email address in Resend before you can send emails:

Log in to Resend. Go to the Email Addresses or Domains section. Add and verify the email address you want to use as the from address. Resend will send a verification email to this address — follow the instructions in the email to complete the process.

Send Email

Now, you can use resend to send an email.

const response = await fetch('/api/resend', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      to: '[email protected]',
      subject: 'Hello from Next.js and Resend',
      text: 'This is a test email from Resend!',
    }),
  });
 
  const result = await response.json();
  if (result.success) {
    console.log('Email sent successfully:', result.email);
  } else {
    console.error('Failed to send email:', result.error);
  }
 

Sending Emails with SMTP

Prerequisites

Ensure you have a STMP server

Config Transporter

You can see this in ‘api/email/route.ts’

    const transporter = nodemailer.createTransport({
      host: "smtp server",
      port: 465,
      secure: true, // true for 465, false for other ports
      auth: {
        user: "user ",
        pass: "password",
      },
      tls: {
        rejectUnauthorized: false, // 在某些环境下,设置为 false 可以避免认证问题
      },
    })
 

Send Email

Now, you can use STMP to send an email.

      await fetch("/api/email", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            to: toArray,
            title: "Welcome to BuouNext",
            content: `content`,
            from: "from",
            head: "BuouNext",
          }),
        })