Created
October 11, 2019 19:05
-
-
Save AppLoidx/fb0ada6efeffdaaf3a685aa7b0a2cda0 to your computer and use it in GitHub Desktop.
gmail message sender simple example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mail; | |
import javax.mail.*; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeMessage; | |
import java.io.IOException; | |
import java.util.Properties; | |
/** | |
* @author Arthur Kupriyanov | |
*/ | |
public class MailSender { | |
public static boolean send(String toAdr, String userPassword) { | |
final String email = "your_email"; | |
final String password = "your_password"; | |
Properties p = new Properties(); | |
p.put("mail.smtp.host", "smtp.gmail.com"); | |
p.put("mail.smtp.socketFactory.port", 465); | |
p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | |
p.put("mail.smtp.auth", "true"); | |
p.put("mail.smtp.starttls.required", "true"); | |
p.put("mail.smtp.port", 465); | |
// 465 - default SMTP gmail port | |
Session s = Session.getDefaultInstance(p, | |
new javax.mail.Authenticator(){ | |
protected PasswordAuthentication getPasswordAuthentication(){ | |
return new PasswordAuthentication(email, password); | |
} | |
}); | |
try { | |
Message mess = new MimeMessage(s); | |
mess.setFrom(new InternetAddress(email)); | |
mess.setRecipient(Message.RecipientType.TO, new InternetAddress(toAdr)); | |
mess.setSubject("user password"); | |
mess.setText(String.valueOf(userPassword)); | |
// sending mail | |
Transport.send(mess); | |
// mail sent | |
return true; | |
}catch (Exception ex){ | |
// You can get bad credentials exception, when you try auth to gmail SMTP | |
// without turn on the "Less secure app access" | |
// read more info here: https://support.google.com/accounts/answer/6010255?hl=en | |
// And additional you can turn off the IMAP on gmail settings | |
// read more info here: https://www.lifewire.com/how-to-enable-gmail-via-imap-1170856 | |
ex.printStackTrace(); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment