import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { final String username = "your_email@example.com"; final String password = "your_password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.example.com"); // Replace with your SMTP server props.put("mail.smtp.port", "587"); // Port number may vary Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your_email@example.com")); // Replace with your email message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); // Replace with recipient's email message.setSubject("Test Email"); message.setText("This is a test email sent using JavaMail."); Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { System.out.println("Email sending failed. Error: " + e.getMessage()); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter