JavaMail: Send Mail Using Gmail’s SMTP Server
Posted by blogmeister on
January 1, 2009
Sometimes having our own SMTP server can cause problems especially the newbie ones. If our SMTP server is being detected by such spam security sites like Spamhaus as a probable spam server, emails that we send to recipients will eventually be blocked. We would have to register our server to these sites so that they would get unblocked. If you are too lazy to wait or want to avoid the hassle of this kind of dilemna, you can actually use GMail’s SMTP server.
The code below sends an email using JavaMail, Java’s mail technology API to send and receive emails. It also includes a Reply-To Email parameter in case you want your Reply-To email address to be different. The other parameter option is to specify if you want to email to be sent at text or HTML. Just input html as the value if you want to send the email as HTML.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
public static boolean send(String replyTo, String from_email, String from_name, String to_email, String subject, String body, String type, String bcc_email) { boolean sent = false; try { Properties prop = new Properties(); prop.put("mail.smtp.port", "25"); prop.put("mail.smtp.socketFactory.fallback", "false"); prop.put("mail.smtp.quitwait", "false"); prop.put("mail.smtp.host", "smtp.gmail.com"); prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.starttls.enable", "true"); Session session = Session.getDefaultInstance(prop); String username = (String) prop.get("gmail.username"); String password = (String) prop.get("gmail.password"); Message msg = new MimeMessage(session); msg.setSubject(subject); InternetAddress from = new InternetAddress(from_email, from_name); InternetAddress to = new InternetAddress(to_email); msg.addRecipient(Message.RecipientType.TO, to); // add bcc if not null if ((bcc_email != null) && (!bcc_email.equals(""))) msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc_email)); msg.setFrom(from); // set reply to email address InternetAddress[] replyToAddress = new InternetAddress[1]; replyToAddress[0] = new InternetAddress(replyTo); msg.setReplyTo(replyToAddress); Multipart multipart = new MimeMultipart("related"); BodyPart htmlPart = new MimeBodyPart(); if (type.equals("html")) htmlPart.setContent(body, "text/html"); else htmlPart.setContent(body, "text/plain"); multipart.addBodyPart(htmlPart); msg.setContent(multipart); Transport transport = session.getTransport("smtp"); transport.connect(GMAIL_USERNAME, GMAIL_PASSWORD); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); sent = true; System.out.println("[MailTool] mail sent successfully ..."); } catch (Exception e) { System.err.println("[MailTool] send() : " + e.getMessage()); e.printStackTrace(); } return sent; } |
Note that connecting to GMail’s SMTP server requires you to authenticate first so you would have to use a GMail account before transactions push through.
Share the post "JavaMail: Send Mail Using Gmail’s SMTP Server"












