Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.util.Properties;
- public class EmailTest {
- public void sendEmail(){
- String to="toid@gmail.com";
- String host="mail.javatpoint.com";
- final String user="fromid@gmail.com";
- final String password="password";
- //Get the session object
- Properties props = new Properties();
- props.put("mail.smtp.host",host);
- props.put("mail.smtp.auth", "true");
- System.out.println("here");
- Session session = Session.getDefaultInstance(props,
- new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user,password);
- }
- });
- //Compose the message
- System.out.println("here");
- MimeMessage message = new MimeMessage(session);
- try {
- message.setFrom(new InternetAddress(user));
- message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
- message.setSubject("javatpoint");
- message.setText("This is simple program of sending email using JavaMail API");
- //send the message
- Transport.send(message);
- System.out.println("message sent successfully...");
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- }
- }
- public class MobileRequestServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) {
- EmailTest test=new EmailTest();
- test.sendEmail();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement