Guest User

Untitled

a guest
Aug 9th, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. sending sms from pc to mobile using java
  2. package john;
  3.  
  4. import java.io.*;
  5. import java.net.InetAddress;
  6. import java.util.Properties;
  7. import java.util.Date;
  8. import javax.mail.*;
  9. import javax.mail.internet.*;
  10. import javax.activation.*;
  11.  
  12. public class SMTPSend {
  13.  
  14. public SMTPSend() {
  15. }
  16.  
  17. public void msgsend() {
  18. String username = "mygmailuserid@gmail.com";
  19. String password = "mygmailpassword";
  20. String smtphost = "smtp.gmail.com";
  21. String compression = "My SMS Compression Information";
  22. String from = "mygmailid@gmail.com";
  23. String to = "+91mymobilenumber@sms.gmail.com";
  24. String body = "Hello SMS World!";
  25. Transport myTransport = null;
  26.  
  27. try {
  28. Properties props = System.getProperties();
  29. props.put("mail.smtp.host", "smtp.gmail.com");
  30. props.put("mail.smtp.socketFactory.port", "465");
  31. props.put("mail.smtp.socketFactory.class",
  32. "javax.net.ssl.SSLSocketFactory");
  33. props.put("mail.smtp.auth", "true");
  34. props.put("mail.smtp.port", "465");
  35.  
  36. Session mailSession = Session.getDefaultInstance(props, null);
  37. Message msg = new MimeMessage(mailSession);
  38. msg.setFrom(new InternetAddress(from));
  39. InternetAddress[] address = {new InternetAddress(to)};
  40. msg.setRecipients(Message.RecipientType.TO, address);
  41. msg.setSubject(compression);
  42. msg.setText(body);
  43. msg.setSentDate(new Date());
  44.  
  45. myTransport = mailSession.getTransport("smtp");
  46. myTransport.connect(smtphost, username, password);
  47. msg.saveChanges();
  48. myTransport.sendMessage(msg, msg.getAllRecipients());
  49. myTransport.close();
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54.  
  55. public static void main(String[] argv) {
  56. SMTPSend smtpSend = new SMTPSend();
  57. smtpSend.msgsend();
  58. }
  59. } //
  60.  
  61. String smtphost = "gmail.com";
  62.  
  63. String smtphost = "smtp.gmail.com";
Add Comment
Please, Sign In to add comment