Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hitesh-dhamshaniya/3f3723ed441b8be254357e66d6186b57 to your computer and use it in GitHub Desktop.
Save hitesh-dhamshaniya/3f3723ed441b8be254357e66d6186b57 to your computer and use it in GitHub Desktop.
SMTP Mail sender help to send mail from Android application without user interaction.
public class SmtpSendMail {
public static void sendMail() {
// Recipient's email ID needs to be mentioned.
String to = "hitesh.dhamshaniya@devdigital.com";
// Sender's email ID needs to be mentioned
String from = to;//"web@gmail.com";
// Assuming you are sending email from localhost
// String host = "localhost";
String host = "cpanel.devdigital.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText(Html.fromHtml("<html><body><h1>Hello there</h1> <p> Good Morning <br> Work schedule of 28 June 2016.</p></body></html>").toString());
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment