import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.example.com"); properties.put("mail.smtp.port", "587"); Session session = Session.getDefaultInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your-email@example.com", "your-password"); } }); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("your-email@example.com")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com")); message.setSubject("Hello"); message.setText("This is the test email."); Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { e.printStackTrace(); } } }


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