下面是一个简单的用java编写生日祝福 代码示例,用于向老板发送生日祝福:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class BirthdayGreetings {
public static void main(String[] args) {
// 邮件信息设置
String senderEmail = "your_email@example.com";
String senderPassword = "your_password";
String recipientEmail = "boss_email@example.com";
String subject = "生日快乐";
String message = "亲爱的老板,祝您生日快乐!";
// 配置邮件服务器属性
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.example.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
// 创建会话对象
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(senderEmail, senderPassword);
}
});
try {
// 创建邮件消息对象
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(senderEmail));
mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
mimeMessage.setSubject(subject);
mimeMessage.setText(message);
// 发送邮件
Transport.send(mimeMessage);
System.out.println("生日祝福已成功发送给老板!");
} catch (MessagingException e) {
System.out.println("发送生日祝福时出现错误:" + e.getMessage());
}
}
}
在上述代码中,你需要按照注释提供相关的邮件信息,包括发件人邮箱、发件人密码、收件人邮箱等。确保将 `smtp.example.com` 替换为你所使用的 SMTP 服务器地址,并选择正确的端口号。代码会通过 SMTP 服务器发送一封包含生日祝福内容的电子邮件给老板。
请注意,在运行代码之前,你需要确保已配置 JavaMail 相关依赖库,并设置正确的类路径。你可以下载 JavaMail API 并将其添加到项目中。另外,你可能还需要下载相应的 Java Activation Framework (JAF)。
希望这个示例对你有所帮助!如果还有其他问题,请随时提问。