使用阿里云邮箱发送邮件

代码部分

/**
     * 发送邮件 (阿里云邮箱企业版)
     *
     * @param fromEmail
     *            发送邮箱
     * @param toEmail
     *            接收邮箱
     * @param emailName
     *            阿里云邮箱登录名
     * @param emailPassword
     *            密码
     * @param title
     *            发送主题
     * @param centent
     *            发送内容
     * @throws Exception
     */
    public static void sendMail(String fromEmail, String toEmail, String emailName, String emailPassword, String title,
                                String centent) throws Exception{
        Properties prop=new Properties();
        prop.put("mail.host","smtp.mxhichina.com" );
        prop.put("mail.transport.protocol", "smtp");
        prop.put("mail.smtp.auth", "true");
        Session session=Session.getInstance(prop);
        session.setDebug(true);
        Transport ts=session.getTransport();
        ts.connect(emailName, emailPassword);
        Message message=new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
        message.setSubject(title);
        message.setContent(centent, "text/html;charset=utf-8");
        ts.sendMessage(message, message.getAllRecipients());
    }

所需依赖

   <dependency>  
            <groupId>javax.mail</groupId>  
            <artifactId>mail</artifactId>  
            <version>1.4</version>  
     </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中使用阿里云邮箱发送信息,你可以使用JavaMail API和阿里云的SMTP服务器。以下是使用Java代码发送邮件的基本步骤: 1. 首先,确保你已经在阿里云邮箱中创建了一个发件人账户。登录阿里云邮箱,进入“设置”-“POP3/SMTP服务”页面,启用SMTP服务并记下SMTP服务器地址。 2. 在Java应用程序中,使用JavaMail API连接到阿里云的SMTP服务器。 ```java Properties properties = new Properties(); properties.setProperty("mail.smtp.host", "smtp.mxhichina.com"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.starttls.enable", "true"); Session session = Session.getInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your-email@example.com", "your-password"); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your-email@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email@example.com")); message.setSubject("邮件主题"); message.setText("邮件内容"); Transport.send(message); System.out.println("邮件发送成功"); } catch (Exception e) { System.out.println("邮件发送失败:" + e.getMessage()); } ``` 3. 将你的发件人邮箱地址和密码替换到代码中的`getPasswordAuthentication()`方法中的`your-email@example.com`和`your-password`。 4. 将收件人的邮箱地址替换到`setRecipients()`方法中的`recipient-email@example.com`。 5. 设置邮件的主题和内容。 6. 使用`Transport.send(message)`方法发送邮件。 7. 检查控制台输出,如果邮件发送成功,会打印"邮件发送成功",否则会打印发送失败的原因。 请注意,这只是一个基本的示例代码,实际使用时可能需要根据你的具体情况进行适当的修改。还要确保你的Java应用程序能够访问到阿里云的SMTP服务器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值