gmail邮箱发送邮件 java

谷歌邮箱配置

首先登录邮箱 进入设置 如下

开启二步验证

进入二次验证,底部有个应用专用密码,进入设置

代码配置

首先导入依赖

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

配置项 yml

### email begin
  mail:
    host: smtp.gmail.com
    username: xxx@gmail.com  ### 邮箱地址
    password: xxxxx  ### 应用专用密码
    protocol: smtp
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 465
    properties.mail.display.sendmail: xxx@gmail.com
    properties.mail.display.sendname: xxx@gmail.com
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    from: xxx@gmail.com
    default-encoding: utf-8
#### email end

代码块

@Value("${spring.mail.from}")
private String from;

@Resource
private JavaMailSender mailSender;
/**
 * 发送文本邮件
 * @param to
 * @param subject
 */
@Override
public ResultBean sendSimpleMail(String to, String subject,String contentStr) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from); //配置邮箱
    message.setTo(to);  //收件邮箱
    message.setSubject(subject); //邮件标题
    message.setText(contentStr); //邮件内容
    try{
        mailSender.send(message);
    }catch (Exception e){
        e.printStackTrace();
    }
    return new ResultBean();
}

### 如何使用 JavaGmail SMTP 发送邮件 为了通过 Google 的 Gmail SMTP 服务发送电子邮件,可以利用 Spring 提供的 `JavaMailSender` 接口及其具体实现类 `JavaMailSenderImpl` 来完成操作。以下是详细的说明和示例代码。 #### 配置依赖项 首先,在 Maven 或 Gradle 中引入必要的依赖库来支持 JavaMail 功能: 对于 Maven 用户: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 对于 Gradle 用户: ```gradle implementation 'org.springframework.boot:spring-boot-starter-mail' ``` 这些依赖会自动导入所需的 JavaMail 库和其他必要组件[^1]。 --- #### 配置文件设置 (application.properties 或 application.yml) 在项目的配置文件中指定 Gmail SMTP 的相关参数: ##### **application.properties** ```properties spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=your-email@gmail.com spring.mail.password=your-app-password spring.mail.protocol=smtp spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=3000 spring.mail.properties.mail.smtp.writetimeout=5000 ``` 注意: - 替换 `your-email@gmail.com` 为您的实际 Gmail 地址。 - 将 `your-app-password` 设置为您创建的应用专用密码(如果启用了两步验证,则需要此密码)。如果没有启用两步验证,请输入账户密码[^4]。 ##### **application.yml** ```yaml spring: mail: host: smtp.gmail.com port: 587 username: your-email@gmail.com password: your-app-password protocol: smtp properties: mail: smtp: auth: true starttls: enable: true connectiontimeout: 5000 timeout: 3000 writetimeout: 5000 ``` --- #### 编写发送邮件的服务逻辑 下面是一个完整的示例代码片段,展示如何基于上述配置编写一个简单的邮件发送工具类。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender javaMailSender; public void sendSimpleEmail(String to, String subject, String text) { try { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); javaMailSender.send(message); // 实际执行发送动作 System.out.println("邮件已成功发送!"); } catch (Exception e) { System.err.println("邮件发送失败:" + e.getMessage()); } } } ``` 调用该方法时只需传入收件人地址、主题以及正文即可完成邮件发送任务[^3]。 --- #### 常见问题排查 当尝试连接到 Gmail SMTP 并发送消息时可能会遇到一些错误情况。例如身份验证失败或者网络超时等问题都可以借助开启调试模式查看更详尽的日志信息以便定位原因所在: ```properties spring.mail.properties.mail.debug=true ``` 启用后会在控制台打印出与远程服务器之间的通信细节帮助诊断潜在故障点[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值