JavaMail API 之 发送电子邮件的Java程序 之 Gmail SMTP服务器示例

我们知道,如果必须从Java代码向某人发送邮件,则需要访问某些邮件服务器凭据。如果我们无权访问这些凭据,则Baidu会通过我们的Gmail帐户提供对Gmail SMTP服务器的公共访问权限。

1. Gmail SMTP服务器详细信息

Baidu提供了对其中一台SMTP服务器的免费访问权限,我们可以使用它使用Java代码发送电子邮件。

  1. Gmail SMTP服务器 – smtp.gmail.com
  2. 端口 – 465(需要SSL)
  3. 端口 – 587(需要TLS)
  4. 使用身份验证 –是

2. Java程序通过Gmail服务器发送电子邮件

下面给出的程序可用于使用Gmail SMTP服务器发送电子邮件。

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaEmail
{
    Session mailSession;
    public static void main(String args[]) throws AddressException, MessagingException
    {
        JavaEmail javaEmail = new JavaEmail();
        javaEmail.setMailServerProperties();
        javaEmail.draftEmailMessage();
        javaEmail.sendEmail();
    }
    private void setMailServerProperties()
    {
        Properties emailProperties = System.getProperties();
        emailProperties.put("mail.smtp.port", "586");
        emailProperties.put("mail.smtp.auth", "true");
        emailProperties.put("mail.smtp.starttls.enable", "true");
        mailSession = Session.getDefaultInstance(emailProperties, null);
    }
    private MimeMessage draftEmailMessage() throws AddressException, MessagingException
    {
        String[] toEmails = { "admin@gmail.com" };
        String emailSubject = "Test email subject";
        String emailBody = "This is an email sent by <b>//how2codex.com</b>.";
        MimeMessage emailMessage = new MimeMessage(mailSession);
        /**
         * Set the mail recipients
         * */
        for (int i = 0; i < toEmails.length; i++)
        {
            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
        }
        emailMessage.setSubject(emailSubject);
        /**
         * If sending HTML mail
         * */
        emailMessage.setContent(emailBody, "text/html");
        /**
         * If sending only text mail
         * */
        //emailMessage.setText(emailBody);// for a text email
        return emailMessage;
    }
    private void sendEmail() throws AddressException, MessagingException
    {
        /**
         * Sender's credentials
         * */
        String fromUser = "user-email@gmail.com";
        String fromUserEmailPassword = "*******";
        String emailHost = "smtp.gmail.com";
        Transport transport = mailSession.getTransport("smtp");
        transport.connect(emailHost, fromUser, fromUserEmailPassword);
        /**
         * Draft the message
         * */
        MimeMessage emailMessage = draftEmailMessage();
        /**
         * Send the mail
         * */
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        System.out.println("Email sent successfully.");
    }
}

学习愉快!

如果出现“连接超时”错误,请确保您没有任何阻碍您访问Gmail服务器的代理。大多数公司代理将阻止您。尝试ping通URL:“ smtp.gmail.com”

在此示例中,我们看到了Java程序使用gmail smtp服务器向多个收件人发送电子邮件。将您的问题留在我的评论中。

学习愉快!

参考文献:

GMail SMTP详细信息

saigon has written 1440 articles

Leave a Reply