import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) throws Exception { 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 Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username", "password"); } }); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("from@example.com")); message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com")); message.setSubject("Hello"); message.setText("This is a test email."); Transport.send(message); System.out.println("Email sent successfully!"); } }


上一篇:
下一篇:
切换中文