如何用Java代码实现发送邮箱

所需要的jar架包在这里插入图片描述

你的密码

在这里插入图片描述

java代码

在这里插入代码片
```public class ss {
    //发件人地址
    public static String senderAddress = "你自己的邮箱地址";
    //收件人地址
    public static String recipientAddress = "对方的邮箱地址";
    //发件人账户名
    public static String senderAccount = "你自己的邮箱地址";
    //发件人账户密码
    public static String senderPassword = "这里根据上方QQ邮箱的操作进行,,拿到你的密码f";
     
    public static void main(String[] args) throws Exception {
        //1、连接邮件服务器的参数配置
        Properties props = new Properties();
        //设置用户的认证方式
        props.setProperty("mail.smtp.auth", "true");
        //设置传输协议
        props.setProperty("mail.transport.protocol", "smtp");
        //设置发件人的SMTP服务器地址
        props.setProperty("mail.smtp.host", "smtp.qq.com");
        //2、创建定义整个应用程序所需的环境信息的 Session 对象
        Session session = Session.getInstance(props);
        //设置调试信息在控制台打印出来
        session.setDebug(true);
        //3、创建邮件的实例对象
        Message msg = getMimeMessage(session);
        //4、根据session对象获取邮件传输对象Transport
        Transport transport = session.getTransport();
        //设置发件人的账户名和密码
        transport.connect(senderAccount, senderPassword);
        //发送邮件,并发送到所有收件人地址,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
        transport.sendMessage(msg,msg.getAllRecipients());
         
        //如果只想发送给指定的人,可以如下写法
        //transport.sendMessage(msg, new Address[]{new InternetAddress("xxx@qq.com")});
         
        //5、关闭邮件连接
        transport.close();
    }
     /**
     * 获得创建一封邮件的实例对象
     * @param session
     * @return
     * @throws MessagingException
     * @throws AddressException
     */
    public static MimeMessage getMimeMessage(Session session) throws Exception{
        //创建一封邮件的实例对象
        MimeMessage msg = new MimeMessage(session);
        //设置发件人地址
        msg.setFrom(new InternetAddress(senderAddress));
        /**
         * 设置收件人地址(可以增加多个收件人、抄送、密送),即下面这一行代码书写多行
         * MimeMessage.RecipientType.TO:发送
         * MimeMessage.RecipientType.CC:抄送
         * MimeMessage.RecipientType.BCC:密送
         */
        msg.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(recipientAddress));
        //设置邮件主题
        msg.setSubject("你的邮件标题","UTF-8");
        //设置邮件正文
        msg.setContent("你的邮件内容!", "text/html;charset=UTF-8");
        //设置邮件的发送时间,默认立即发送
        msg.setSentDate(new Date());
         
        return msg;
    }
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用JavaMail API来实现邮件发送功能。以下是一个简单的代码示例: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class MailSender { public static void main(String[] args) { final String username = "your_email@example.com"; final String password = "your_email_password"; String recipient = "recipient_email@example.com"; String subject = "Test Email"; String body = "This is a test email sent using JavaMail API."; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.example.com"); // Replace with your SMTP server address props.put("mail.smtp.port", "587"); // Replace with your SMTP server port number Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); message.setSubject(subject); message.setText(body); Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 在上面的代码中,你需要将 `username` 和 `password` 替换为你的发件人邮箱地址和密码。你还需要将 `recipient` 替换为收件人邮箱地址,`subject` 替换为邮件主题,`body` 替换为邮件正文。同时,你还需要将 `props.put("mail.smtp.host", "smtp.example.com");` 替换为你的 SMTP 服务器地址。 如果你已经配置好了 JavaMail API 的环境,那么你可以直接运行上面的代码发送邮件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值