fork download
  1. import java.util.*;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. import javax.activation.*;
  5.  
  6. public class SendEmail
  7. {
  8. public static void main(String [] args)
  9. {
  10. // Recipient's email ID needs to be mentioned.
  11. String to = "abcd@gmail.com";
  12.  
  13. // Sender's email ID needs to be mentioned
  14. String from = "web@gmail.com";
  15.  
  16. // Assuming you are sending email from localhost
  17. String host = "localhost";
  18.  
  19. // Get system properties
  20. Properties properties = System.getProperties();
  21.  
  22. // Setup mail server
  23. properties.setProperty("mail.smtp.host", host);
  24.  
  25. // Get the default Session object.
  26. Session session = Session.getDefaultInstance(properties);
  27.  
  28. try{
  29. // Create a default MimeMessage object.
  30. MimeMessage message = new MimeMessage(session);
  31.  
  32. // Set From: header field of the header.
  33. message.setFrom(new InternetAddress(from));
  34.  
  35. // Set To: header field of the header.
  36. message.addRecipient(Message.RecipientType.TO,
  37. new InternetAddress(to));
  38.  
  39. // Set Subject: header field
  40. message.setSubject("This is the Subject Line!");
  41.  
  42. // Now set the actual message
  43. message.setText("This is actual message");
  44.  
  45. // Send message
  46. Transport.send(message);
  47. System.out.println("Sent message successfully....");
  48. }catch (MessagingException mex) {
  49. mex.printStackTrace();
  50. }
  51. }
  52. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:6: error: class SendEmail is public, should be declared in a file named SendEmail.java
public class SendEmail
       ^
Main.java:2: error: package javax.mail does not exist
import javax.mail.*;
^
Main.java:3: error: package javax.mail.internet does not exist
import javax.mail.internet.*;
^
Main.java:26: error: cannot find symbol
      Session session = Session.getDefaultInstance(properties);
      ^
  symbol:   class Session
  location: class SendEmail
Main.java:26: error: cannot find symbol
      Session session = Session.getDefaultInstance(properties);
                        ^
  symbol:   variable Session
  location: class SendEmail
Main.java:30: error: cannot find symbol
         MimeMessage message = new MimeMessage(session);
         ^
  symbol:   class MimeMessage
  location: class SendEmail
Main.java:30: error: cannot find symbol
         MimeMessage message = new MimeMessage(session);
                                   ^
  symbol:   class MimeMessage
  location: class SendEmail
Main.java:33: error: cannot find symbol
         message.setFrom(new InternetAddress(from));
                             ^
  symbol:   class InternetAddress
  location: class SendEmail
Main.java:36: error: package Message does not exist
         message.addRecipient(Message.RecipientType.TO,
                                     ^
Main.java:37: error: cannot find symbol
                                  new InternetAddress(to));
                                      ^
  symbol:   class InternetAddress
  location: class SendEmail
Main.java:46: error: cannot find symbol
         Transport.send(message);
         ^
  symbol:   variable Transport
  location: class SendEmail
Main.java:48: error: cannot find symbol
      }catch (MessagingException mex) {
              ^
  symbol:   class MessagingException
  location: class SendEmail
12 errors
stdout
Standard output is empty