Wednesday, December 31, 2014

java send mail using ubuntu local sendmail smtp

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public static void main(String[] args) {

String to="recipient@example.com";
String from="sender@example.com";

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

// props.put("mail.smtp.host", "localhost"); // optional

String msgBody = "Sending email using JavaMail API...";

try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from, "NoReply"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(to, "Mr. Recipient"));
msg.setSubject("Welcome To Java Mail API");
msg.setText(msgBody);
Transport.send(msg);
System.out.println("Email sent successfully...");

} catch (AddressException e) {
throw new RuntimeException(e);
} catch (MessagingException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.