import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; public class SendEmail { public static void main(String[] args) { Properties properties = new Properties(); properties.setProperty("mail.smtp.host", "your-smtp-server"); properties.setProperty("mail.smtp.port", "your-smtp-port"); properties.setProperty("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your-username", "your-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("Email sent successfully."); } catch (Exception e) { e.printStackTrace(); } } }


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