使用Java Mail API发送带有星号的Gmail邮件可以通过以下步骤实现:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-email@gmail.com", "your-password");
}
});
请注意替换上述代码中的"your-email@gmail.com"和"your-password"为您自己的Gmail邮箱地址和密码。
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email@gmail.com"));
message.setSubject("Subject with asterisk *");
message.setText("This is the message content.");
请注意替换上述代码中的"your-email@gmail.com"为您自己的Gmail邮箱地址,"recipient-email@gmail.com"为收件人的邮箱地址。
Transport.send(message);
完整的Java代码示例:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class GmailSender {
public static void main(String[] args) {
// 邮件配置
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
// 创建邮件会话
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-email@gmail.com", "your-password");
}
});
try {
// 创建邮件消息
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email@gmail.com"));
message.setSubject("Subject with asterisk *");
message.setText("This is the message content.");
// 发送邮件
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
请注意替换上述代码中的"your-email@gmail.com"和"your-password"为您自己的Gmail邮箱地址和密码,"recipient-email@gmail.com"为收件人的邮箱地址。
这是一个使用Java Mail API发送带有星号的Gmail邮件的基本示例。您可以根据实际需求进行进一步的定制和扩展。
领取专属 10元无门槛券
手把手带您无忧上云