SFAH
SFAH

Reputation: 644

Session in JavaMail

In the following Code in the session object (PasswordAuthentication ) what username and password we have to provide to send mail ? Sender's username password or receiver's credentials? I am really confused , I am using java Mail to send mail

public void sendMail(String email,String token)
    {
        // Recipient's email ID needs to be mentioned.
           String to = email;
          // Sender's email ID needs to be mentioned
          // Assuming you are sending email through relay.jangosmtp.net
          String host = "smtp.gmail.com";
          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", "587");

          // Get the Session object.
          Session session = Session.getInstance(props,
             new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                   return new PasswordAuthentication(username, password);
           }
             });

          try {
           // Create a default MimeMessage object.
           Message message = new MimeMessage(session);

           // Set From: header field of the header.
           message.setFrom(new InternetAddress("Issme-Customer-Service"));

           // Set To: header field of the header.
           message.setRecipients(Message.RecipientType.TO,
                   InternetAddress.parse(to));

          message.setSubject("Email Verification Of issme Account");

            message.setContent(
                    "<h2>Email Verification </h2>" + 
                    "<h3> Please goto  the following URL to verify your ISSME account\n </h3> " +
                     token , "text/html; charset=utf-8");


           // Send message
           Transport.send(message);

           System.out.println("Sent message successfully....");

          } catch (MessagingException e) {
             throw new RuntimeException(e);
          }
       }

Upvotes: 0

Views: 1482

Answers (1)

Sandeep Kaul
Sandeep Kaul

Reputation: 3267

You need to send the sender's credentials which gmail's SMTP server will authenticate and then it'll send the email.

Upvotes: 1

Related Questions