使用JavaMail发送邮件(Gmail)


以Gmail服务器为例

输入正确的用户名和密码,然后输入有效的邮箱地址即可实现发送

代码已经验证通过。



下面是源码

package com.suifeng.mail;

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
{

    Properties emailProperties;
    Session mailSession;
    MimeMessage emailMessage;

    public static void main(String args[])
    {

        JavaEmail javaEmail = new JavaEmail();

        javaEmail.setMailServerProperties();
        try
        {
            javaEmail.createEmailMessage();
            javaEmail.sendEmail();
        }
        catch (AddressException e)
        {
            System.out.println("Address Exception:" + e.getMessage());
            e.printStackTrace();
        }
        catch (MessagingException e)
        {
            System.out.println("Message Exception:" + e.getMessage());
            e.printStackTrace();
        }
    }

    public void setMailServerProperties()
    {

        String emailPort = "587";// gmail's smtp port

        emailProperties = System.getProperties();
        emailProperties.put("mail.smtp.port", emailPort);
        emailProperties.put("mail.smtp.auth", "true");
        emailProperties.put("mail.smtp.starttls.enable", "true");

    }

    public void createEmailMessage() throws AddressException,
            MessagingException
    {
        String[] toEmails =
        { "要发送的邮箱" };
        String emailSubject = "Java Email";
        String emailBody = "This is an email sent by <b>JavaMail</b> api.";

        mailSession = Session.getDefaultInstance(emailProperties, null);
        emailMessage = new MimeMessage(mailSession);

        for (int i = 0; i < toEmails.length; i++)
        {
            emailMessage.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(toEmails[i]));
        }

        emailMessage.setSubject(emailSubject);
        emailMessage.setContent(emailBody, "text/html");// for a html email
        // emailMessage.setText(emailBody);// for a text email

    }

    public void sendEmail() throws AddressException, MessagingException
    {

        String emailHost = "smtp.gmail.com";
        String fromUser = "这里使你的gmail邮箱地址";// just the id alone without
        // @gmail.com
        String fromUserEmailPassword = "这里使你的gmail密码";

        Transport transport = mailSession.getTransport("smtp");

        transport.connect(emailHost, fromUser, fromUserEmailPassword);
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        System.out.println("Email sent successfully.");
    }

}


评论 3 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值