edit-icon download-icon

SMTP - Java

Last Updated: Sep 03, 2018

The following example shows how to send an email through SMTP using Javamail.

  1. import javax.mail.*;
  2. import javax.mail.internet.InternetAddress;
  3. import javax.mail.internet.MimeMessage;
  4. import java.util.Properties;
  5. public class SampleMail {
  6. private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";
  7. private static final String ALIDM_SMTP_PORT = "25";//or "80"
  8. public static void main(String[] args) {
  9. // Configure the mail environment properties
  10. final Properties props = new Properties();
  11. // Indicate that identity authentication is required for sending mails via SMTP
  12. props.put("mail.smtp.auth", "true");
  13. props.put("mail.smtp.host", ALIDM_SMTP_HOST);
  14. props.put("mail.smtp.port", ALIDM_SMTP_PORT);
  15. // If SSL is used, remove the configuration of using Port 25 and add the following codes
  16. // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  17. // props.put("mail.smtp.socketFactory.port", "465");
  18. // props.put("mail.smtp.port", "465");
  19. // The sender account. Here's your sender email configured in Aliyun DirectMail Console.
  20. props.put("mail.user", "***");
  21. // The password required for accessing the SMTP service. Configured for sender email in Aliyun DirectMail Console.
  22. props.put("mail.password", "***");
  23. // Construct the authentication information for SMTP identity authentication.
  24. Authenticator authenticator = new Authenticator() {
  25. @Override
  26. protected PasswordAuthentication getPasswordAuthentication() {
  27. // Username and password
  28. String userName = props.getProperty("mail.user");
  29. String password = props.getProperty("mail.password");
  30. return new PasswordAuthentication(userName, password);
  31. }
  32. };
  33. // Create the mail session using the environmental attributes and authentication information.
  34. Session mailSession = Session.getInstance(props, authenticator);
  35. //mailSession.setDebug(true);
  36. // Create the mail message.
  37. MimeMessage message = new MimeMessage(mailSession);
  38. try {
  39. // Set the sender info.Here's your sender email configured in Aliyun DirectMail Console and it should be the same as mail.user value. You can define sender name as you like.
  40. InternetAddress from = new InternetAddress("***", "sender name(User Defined)");
  41. message.setFrom(from);
  42. //Optional. Set ReplyTo.
  43. Address[] a = new Address[1];
  44. a[0] = new InternetAddress("***");
  45. message.setReplyTo(a);
  46. // Set the recipient.
  47. InternetAddress to = new InternetAddress("***");
  48. message.setRecipient(MimeMessage.RecipientType.TO, to);
  49. //If you want to more than one recipent, you can replace the above codes with the following codes. You'd better send to one recipent for some recipentent mail system's limits. Besides, you can send to 30 recipents once at most.
  50. //InternetAddress[] adds = new InternetAddress[2];
  51. //adds[0] = new InternetAddress("xxxxx@qq.com");
  52. //adds[1] = new InternetAddress("xxxxx@qq.com");
  53. //message.setRecipients(Message.RecipientType.TO, adds);
  54. // Set the mail subject.
  55. message.setSubject("Test mail");
  56. // Set the mail body content.
  57. message.setContent("Test HTML mail", "text/html;charset=UTF-8");
  58. // Send the mail.
  59. Transport.send(message);
  60. }
  61. catch (MessagingException e) {
  62. String err = e.getMessage();
  63. // Here to deal with the message content, format is fixed.
  64. System.out.println(err);
  65. }
  66. }
  67. }
Thank you! We've received your feedback.