在Java中使用Gmail发送邮件

以下Java代码可以实现使用SMTP登陆到Gmail中并使用Gmail发送邮件。
使用Gmail发送邮件的代码:

 String host = "smtp.gmail.com";
String from = "username";
String pass = "password";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // 在本行添加
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");

String[] to = {"to@gmail.com"}; // 在本行添加

Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));

InternetAddress[] toAddress = new InternetAddress[to.length];

// 获取地址的array
for( int i=0; i < to.length; i++ ) { // 从while循环更改而成
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);

for( int i=0; i < toAddress.length; i++) { // 从while循环更改而成
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

代码本身应该很清楚了。在第7和8行加入你的Google账号密码:

props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);



在12行加入收件人信息:

String[] to = {"to@gmail.com"};// 在本行添加 
### 使用Java通过Gmail SMTP服务器发送邮件 为了实现这一功能,需要配置Java应用程序以连接到Gmail的SMTP服务器并发送电子邮件。下面是一个完整的示例程序,展示了如何利用`javax.mail`库完成此操作。 首先,确保项目中包含了必要的依赖项。对于Maven项目,在pom.xml文件内加入如下依赖: ```xml <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> ``` 接着编写用于发送邮件的核心逻辑代码: ```java import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class SendEmail { public static void main(String[] args) { String to = "recipient@example.com"; String from = "your-email@gmail.com"; final String username = "your-email@gmail.com"; // 用户名即为发件人的电子邮箱地址 final String password = "app-specific-password"; // 应用专用密码而非账户登录密码 Properties prop = new Properties(); prop.put("mail.smtp.host", "smtp.gmail.com"); prop.put("mail.smtp.port", "465"); prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); Session session = Session.getInstance(prop, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(to) ); message.setSubject("Testing Gmail SMTP Server via Java Mail API"); message.setText("Dear User,\n\n This is a test email sent through the JavaMail API using Gmail's SMTP server."); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 上述代码片段创建了一个新的会话实例,并设置了相应的属性以便于与Gmail SMTP服务建立安全连接。注意这里使用的是SSL加密方式(端口465),并且启用了身份验证机制。此外,还指定了应用特定的密码而不是常规的Google帐户密码[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值