import javax.mail.*; import javax.mail.internet.*; public class SendMail { public static void main(String[] args) { Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", "smtp.example.com"); properties.setProperty("mail.smtp.port", "465"); properties.setProperty("mail.smtp.auth", "true"); Session session = Session.getInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username", "password"); } }); try { 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, World!"); message.setText("This is a test email."); Transport.send(message); System.out.println("Mail sent successfully."); } catch (MessagingException mex) { mex.printStackTrace(); } } }


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