用java编写代码发送到qq邮箱

首先的话我们打开QQ邮箱(地址:登录QQ邮箱);

然后点击设置

 再然后找到账户点击开启因为我已经开启了所以这里初始化时要开启的

 

 

 

 然后我们得开启授权码,注意这个授权码我们得记下来因为这个授权码我们是要在代码里面使用得

然后我们得导入相关得依赖

  <!--QQ邮箱验证码所需jar包-->
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.4</version>
        </dependency>

 然后我们可以写一个随机验证码得工具类来进行验证

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class QqRandom {
    public static String achieveCode() {
        String[] beforeShuffle = new String[]{"2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
                "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a",
                "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
                "w", "x", "y", "z"};
        List list = Arrays.asList(beforeShuffle);//将数组转换为集合
        Collections.shuffle(list);//打乱顺序
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<list.size();i++){
            String s=(String) list.get(i);
            //进行追加
            stringBuilder.append(s);
        }
        //对字符串进行截取
       return stringBuilder.toString().substring(3,8);
    }
}

然后我们可以开始写入代码了

import com.atguigu.util.QqRandom;
        import org.apache.commons.mail.EmailException;
        import org.apache.commons.mail.SimpleEmail;

public class QqEmailTest {
    //发送邮件代码
    public static void sendAuthCodeEmail() {
        try {
            String authCode= QqRandom.achieveCode();
            SimpleEmail mail = new SimpleEmail();
            mail.setHostName("smtp.qq.com");//发送邮件的服务器
            mail.setAuthentication("你的qq邮箱","你的授权码");//刚刚记录的授权码,是开启SMTP的密码
            mail.setFrom("你的qq邮箱","发件人的昵称");  //发送邮件的邮箱和发件人
            //mail.setSSLOnConnect(true); //使用安全链接
            mail.setSmtpPort(587);
            mail.addTo("收信人邮箱");//接收的邮箱
            //System.out.println("email"+email);
            mail.setSubject("注册验证码");//设置邮件的主题
            mail.setMsg("尊敬的用户:你好!\n 注册验证码为:" + authCode+"\n"+"     (有效期为一分钟)");//设置邮件的内容
            mail.send();//发送
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        QqEmailTest.sendAuthCodeEmail();
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用Java调用API发送企业邮箱,需要按照以下步骤操作: 1. 配置企业邮箱SMTP服务 首先需要在企业邮箱后台配置SMTP服务,获取SMTP服务器地址、端口号、用户名和密码等信息。一般情况下,SMTP服务器地址为smtp.exmail.qq.com,端口号为465或587。 2. 导入JavaMail库 JavaMail是一款Java邮件发送库,可以通过Maven或手动导入jar包的方式引入到项目中。 3. 编写Java代码 使用JavaMail库发送邮件的代码示例: ``` public static void sendMail(String smtpServer, String smtpPort, String username, String password, String fromAddress, String toAddress, String subject, String content) throws MessagingException { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", smtpServer); props.setProperty("mail.smtp.port", smtpPort); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.ssl.enable", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromAddress)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); message.setSubject(subject); message.setContent(content, "text/html;charset=UTF-8"); Transport.send(message); } ``` 其中,smtpServer、smtpPort、username和password为企业邮箱SMTP服务的配置信息;fromAddress和toAddress为发件人和收件人的邮箱地址;subject为邮件主题;content为邮件正文。 4. 调用Java代码Java程序中调用sendMail()方法即可发送邮件。 示例代码: ``` public static void main(String[] args) throws Exception { String smtpServer = "smtp.exmail.qq.com"; String smtpPort = "465"; String username = "your_email@your_company.com"; String password = "your_password"; String fromAddress = "your_email@your_company.com"; String toAddress = "recipient_email@recipient_company.com"; String subject = "Test Email"; String content = "<h1>Hello, World!</h1>"; sendMail(smtpServer, smtpPort, username, password, fromAddress, toAddress, subject, content); } ``` 以上就是使用Java调用API发送企业邮箱的步骤和代码示例。

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_58664047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值