SlideShare a Scribd company logo
Server-side
Web Programming
    Lecture 19:
     Java Mail
How email works
Sending Client          Receiving Client

 Mail Client              Mail Client
 Software                 Software



         SMTP                    POP/ IMAP




Sending Server          Receiving Server
                 SMTP
  Mail Server              Mail Server
   Software                 Software
3 protocols
• SMTP: Simple Mail Transfer Protocol
  – is used to send a message from the mail client
    software to your mail server / or from one mail server
    to another
• POP: Post Office Protocol
  – transfers all messages from the mail server to the
    mail client. POP in version 3: POP3
• IMAP: Internet Message Access Protocol
  – Is used by web-based mail services such as Hotmail,
    Yahoo, and Gmail.
  – Allows a web browser to read messages that are
    stored on the mail server. IMAP in version 4: IMAP4
Normal Email Process
• Mail client software converts message to SMTP (or other) format
  before sending to server
• Common form for usual components of message:
    –   Sender
    –   Recipient(s)
    –   Subject
    –   Text
    –   Etc.
• Mail server sends message in MIME protocol to receiving server
    – Multipurpose Internet Message Extension
• Mail client software on receiving client accesses using mail client
  software (possibly different)
    – POP
    – IMAP
Java Mail
• Java Mail API is a programming interface that makes it easy for
  Java developers to write code that automatically sends an email.
• Key idea: Java Mail (or other mail software) must emulate what mail
  client software does
   – Create SMTP or other protocol message
   – Insert required mail components
   – Connect with sending server and transmit message

• Requires mail classes
   – Download from www.java.sun.com
   – Insert mail.jar and activation.jar file into java libraries
       • mail. jar: contains the Java classes for the Java Mail API
       • activation. jar: contains the Java classes for the JavaBean Activation
         Framework. These classes are necessary for the JavaMail API to run
How to install the Java Mail API and
 JavaBeans Activation Framework API
• Locate the download page for the JavaMail API/
  JavaBeans Activation Framework API on the
  Java website: www.java.sun.com
• Click on the Download button and follow the
  instructions
• Save the zip file: javamail-1_4.zip/jaf-1_1.zip to
  your hard disk
• Extract the files from the zip file
• Copy the mail.jar/activation.jar file to the JDK’s
  jrelibext directory
Steps in Java Mail
• Create a mail session
    – Creates email message object
• Set message components
    – Properties of message object
• Set addresses
    – Can be list for group of recipients
• Send message
    – May need to identify self to server


• Will need mail libraries
<%@ page import="javax.mail.*, javax.mail.internet.*, java.util.*"%>
Creating an Email Session
• Create a Properties object to store information:
   – Generic Java class to store attribute/value pairs
   – Property props = new Properties();
• Specify the sending server:
   – props.put(“mail.smtp.host”, URL of mail server);
• Create a new session object from those properties:
   – Session s = Session.getDefaultInstance(props);
• Create new MimeMessage object from session
   – MimeMessage message = new MimeMessage(s);
Common properties
• mail.transport.protocol: specifies the protocol that’s used
  for the session
• mail.smtp.host: specifies the host computer for SMTP
  server
• mail.smtp.port: specifies the port that the SMTP server is
  using
• mail.smtp.auth: specifies whether authentication is
  required to log in to the SMTP server
• mail.smtp.quitwait: this property can be set to false to
  prevent an SSLException from occurring when you
  attempt to connect to a Gmail SMTP server
• To set the properties of Properties object, you
  can use the put method to specify of a property
  name and value
• The static getDefaultInstance method of the
  Session class returns the default Session object
  for the application
• The setDebug method of the Session object can
  be used to print debugging information about the
  session to a log file
• If you change the properties for a Session
  object, you must restart Tomcat before the
  changes take effect
• If the Java application is running on the same
  computer as the SMTP server, you can use the
  localhost keyword to specify the SMTP host.
Setting Message Components
• Set the subject:
   – message.setSubject(“subject”);
• Set the message text:
   – message.setText(“Thanks for your
     order!”); //”text/plain”
• Could also be html or other types
   – Will need to specify MIME type

  String response =
  “<html><head><title>Reciept</title</head><body><h3
  >Thank you for your order!</h3>”;

  message.setContent(response, “text/html”);
Setting Addresses
• Use setRecipient method
   – Specify internet address of recipient
       • Must use InternetAddress class
       • InternetAddress constructor requires email address, can also optionally
         give name
   – Specify TO, CC, or BCC
   – Can add multiple recipients


Message.setRecipient(Message.RecipientType.TO,
                    new InternetAddress(“slade@quarry.com
  ”));
Message.setRecipient(Message.RecipientType.CC,
  new InternetAddress(barney@aolrock.com, “Barney
  Rubble”));
Setting Addresses
• Address can also be a list
   – Good for mailing lists
   – Array of type Address
• Use setRecipients method
Address[] list = new Address[size of mailing list];
for (int i = 0; i < size of mailing list; i++) {
    list[i] = get next address for file;
    }
Message.setRecipients(Message.RecipientType.TO, list);
• Use addRecipient method
Message.addRecipient(Message.RecipientType.TO,
         new InternetAddress(“slade@quarry.com”) )
Using addRecipient/addRecipients to add email addresses to any
   existing addresses for a message
Sending Messages
• Simple method:
  Transport.send(message);

• More complex method if server requires authentication:
   – Create Transport object from sessions
     Transport t = session.getTransport();
   – Connect to server with name and password
     t.connect(“your name”, “your password”);
   – Send the message
     t.sendMessage(message, message.getAllRecipients());

More Related Content

What's hot

Spring integration
Spring integrationSpring integration
Spring integration
Dominik Strzyżewski
 
Cocoapods and Most common used library in Swift
Cocoapods and Most common used library in SwiftCocoapods and Most common used library in Swift
Cocoapods and Most common used library in Swift
Wan Muzaffar Wan Hashim
 
retrieving the mail
retrieving the mailretrieving the mail
retrieving the mail
tumetr1
 
Connectors in mule
Connectors in muleConnectors in mule
Connectors in mule
Sindhu VL
 
Google App Engine
Google App EngineGoogle App Engine
Google App Engine
cloudcampnigeria
 
Email using mule
Email using muleEmail using mule
Email using mule
Manav Prasad
 
S2GX 2012 - Introduction to Spring Integration and Spring Batch
S2GX 2012 - Introduction to Spring Integration and Spring BatchS2GX 2012 - Introduction to Spring Integration and Spring Batch
S2GX 2012 - Introduction to Spring Integration and Spring Batch
Gunnar Hillert
 
Managing ejabberd Platforms with Docker - ejabberd Workshop #1
Managing ejabberd Platforms with Docker - ejabberd Workshop #1Managing ejabberd Platforms with Docker - ejabberd Workshop #1
Managing ejabberd Platforms with Docker - ejabberd Workshop #1
Mickaël Rémond
 
Anypoint mq queues and exchanges
Anypoint mq queues and exchangesAnypoint mq queues and exchanges
Anypoint mq queues and exchanges
Son Nguyen
 
What is SMTP Server?
What is SMTP Server?What is SMTP Server?
What is SMTP Server?
SMTPGET
 
Ejabberd with xmpp
Ejabberd with xmppEjabberd with xmpp
Ejabberd with xmpp
Prem Narain
 
Muleesbcomponents1 160625154208
Muleesbcomponents1 160625154208Muleesbcomponents1 160625154208
Muleesbcomponents1 160625154208
ppts123456
 
Mule enricher component
Mule enricher component Mule enricher component
Mule enricher component
Gandham38
 
Mule batch
Mule batchMule batch
Mule batch
Gandham38
 
J meter introduction
J meter introductionJ meter introduction
J meter introduction
Bharath Kumar
 
Learning j meter in 60 minutes
Learning j meter in 60 minutesLearning j meter in 60 minutes
Learning j meter in 60 minutes
Alon Girmonsky
 
Mule connectors-session1
Mule connectors-session1Mule connectors-session1
Mule connectors-session1
Vishnukanth Rachineni
 
Introduction to Long Running Workflows 3.7
Introduction to Long Running Workflows 3.7Introduction to Long Running Workflows 3.7
Introduction to Long Running Workflows 3.7
StephenKardian
 
APACHE WEB SERVER FOR LINUX
APACHE WEB SERVER FOR LINUXAPACHE WEB SERVER FOR LINUX
APACHE WEB SERVER FOR LINUXwebhostingguy
 
Mule Requester Usage Demo
Mule Requester Usage DemoMule Requester Usage Demo
Mule Requester Usage Demo
Ramakrishna Narkedamilli
 

What's hot (20)

Spring integration
Spring integrationSpring integration
Spring integration
 
Cocoapods and Most common used library in Swift
Cocoapods and Most common used library in SwiftCocoapods and Most common used library in Swift
Cocoapods and Most common used library in Swift
 
retrieving the mail
retrieving the mailretrieving the mail
retrieving the mail
 
Connectors in mule
Connectors in muleConnectors in mule
Connectors in mule
 
Google App Engine
Google App EngineGoogle App Engine
Google App Engine
 
Email using mule
Email using muleEmail using mule
Email using mule
 
S2GX 2012 - Introduction to Spring Integration and Spring Batch
S2GX 2012 - Introduction to Spring Integration and Spring BatchS2GX 2012 - Introduction to Spring Integration and Spring Batch
S2GX 2012 - Introduction to Spring Integration and Spring Batch
 
Managing ejabberd Platforms with Docker - ejabberd Workshop #1
Managing ejabberd Platforms with Docker - ejabberd Workshop #1Managing ejabberd Platforms with Docker - ejabberd Workshop #1
Managing ejabberd Platforms with Docker - ejabberd Workshop #1
 
Anypoint mq queues and exchanges
Anypoint mq queues and exchangesAnypoint mq queues and exchanges
Anypoint mq queues and exchanges
 
What is SMTP Server?
What is SMTP Server?What is SMTP Server?
What is SMTP Server?
 
Ejabberd with xmpp
Ejabberd with xmppEjabberd with xmpp
Ejabberd with xmpp
 
Muleesbcomponents1 160625154208
Muleesbcomponents1 160625154208Muleesbcomponents1 160625154208
Muleesbcomponents1 160625154208
 
Mule enricher component
Mule enricher component Mule enricher component
Mule enricher component
 
Mule batch
Mule batchMule batch
Mule batch
 
J meter introduction
J meter introductionJ meter introduction
J meter introduction
 
Learning j meter in 60 minutes
Learning j meter in 60 minutesLearning j meter in 60 minutes
Learning j meter in 60 minutes
 
Mule connectors-session1
Mule connectors-session1Mule connectors-session1
Mule connectors-session1
 
Introduction to Long Running Workflows 3.7
Introduction to Long Running Workflows 3.7Introduction to Long Running Workflows 3.7
Introduction to Long Running Workflows 3.7
 
APACHE WEB SERVER FOR LINUX
APACHE WEB SERVER FOR LINUXAPACHE WEB SERVER FOR LINUX
APACHE WEB SERVER FOR LINUX
 
Mule Requester Usage Demo
Mule Requester Usage DemoMule Requester Usage Demo
Mule Requester Usage Demo
 

Viewers also liked

Phương pháp tính
Phương pháp tínhPhương pháp tính
Phương pháp tính
hanoipost
 
Tailieu.vncty.com bai tap va bai giai phuong phap tinh
Tailieu.vncty.com   bai tap va bai giai phuong phap tinhTailieu.vncty.com   bai tap va bai giai phuong phap tinh
Tailieu.vncty.com bai tap va bai giai phuong phap tinh
Trần Đức Anh
 
Tính toán khoa học - Chương 5: Tính gần đúng đạo hàm và tích phân
Tính toán khoa học - Chương 5: Tính gần đúng đạo hàm và tích phânTính toán khoa học - Chương 5: Tính gần đúng đạo hàm và tích phân
Tính toán khoa học - Chương 5: Tính gần đúng đạo hàm và tích phânChien Dang
 
Giải bài tập Phương pháp tính
Giải bài tập Phương pháp tínhGiải bài tập Phương pháp tính
Giải bài tập Phương pháp tính
dinhtrongtran39
 
phương pháp hình thang,Công thức Simpson
phương pháp hình thang,Công thức Simpson phương pháp hình thang,Công thức Simpson
phương pháp hình thang,Công thức Simpson
caovanquy
 
Giai nhanh phuong phap tinh
Giai nhanh phuong phap tinhGiai nhanh phuong phap tinh
Giai nhanh phuong phap tinh
Pham Huy
 
30 bài toán phương pháp tính
30 bài toán phương pháp tính30 bài toán phương pháp tính
30 bài toán phương pháp tính
Pham Huy
 

Viewers also liked (8)

Chuong02
Chuong02Chuong02
Chuong02
 
Phương pháp tính
Phương pháp tínhPhương pháp tính
Phương pháp tính
 
Tailieu.vncty.com bai tap va bai giai phuong phap tinh
Tailieu.vncty.com   bai tap va bai giai phuong phap tinhTailieu.vncty.com   bai tap va bai giai phuong phap tinh
Tailieu.vncty.com bai tap va bai giai phuong phap tinh
 
Tính toán khoa học - Chương 5: Tính gần đúng đạo hàm và tích phân
Tính toán khoa học - Chương 5: Tính gần đúng đạo hàm và tích phânTính toán khoa học - Chương 5: Tính gần đúng đạo hàm và tích phân
Tính toán khoa học - Chương 5: Tính gần đúng đạo hàm và tích phân
 
Giải bài tập Phương pháp tính
Giải bài tập Phương pháp tínhGiải bài tập Phương pháp tính
Giải bài tập Phương pháp tính
 
phương pháp hình thang,Công thức Simpson
phương pháp hình thang,Công thức Simpson phương pháp hình thang,Công thức Simpson
phương pháp hình thang,Công thức Simpson
 
Giai nhanh phuong phap tinh
Giai nhanh phuong phap tinhGiai nhanh phuong phap tinh
Giai nhanh phuong phap tinh
 
30 bài toán phương pháp tính
30 bài toán phương pháp tính30 bài toán phương pháp tính
30 bài toán phương pháp tính
 

Similar to Lecture19

Voice enable smtp client
Voice enable smtp clientVoice enable smtp client
Voice enable smtp clientNilesh Padwal
 
Mail
MailMail
Tcpip services and applications
Tcpip services and applicationsTcpip services and applications
Tcpip services and applications
Online
 
window server 2008 mail configuration
window server 2008 mail configurationwindow server 2008 mail configuration
window server 2008 mail configuration
anwarkade1
 
Jms using j boss
Jms using j bossJms using j boss
Jms using j boss
Skillwise Group
 
Internet mail server
Internet mail server Internet mail server
Internet mail server
Akasha Kumar Das
 
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
ZahouAmel1
 
Postman.ppt
Postman.pptPostman.ppt
Postman.ppt
ParrotBAD
 
009478419.pdf
009478419.pdf009478419.pdf
009478419.pdf
EidTahir
 
Spam Filter
Spam FilterSpam Filter
Simple mail transfer protocol
Simple mail transfer protocolSimple mail transfer protocol
Simple mail transfer protocol
Anagha Ghotkar
 
Topic 2.4 network services
Topic 2.4 network servicesTopic 2.4 network services
Topic 2.4 network services
Atika Zaimi
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
MouDhara1
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
kstalin2
 
Servlet1.ppt
Servlet1.pptServlet1.ppt
Servlet1.ppt
KhushalChoudhary14
 
Electronic mail
Electronic mailElectronic mail
Computer networks unit v
Computer networks    unit vComputer networks    unit v
Computer networks unit v
JAIGANESH SEKAR
 

Similar to Lecture19 (20)

Lecture19
Lecture19Lecture19
Lecture19
 
Voice enable smtp client
Voice enable smtp clientVoice enable smtp client
Voice enable smtp client
 
Mail
MailMail
Mail
 
Tcpip services and applications
Tcpip services and applicationsTcpip services and applications
Tcpip services and applications
 
window server 2008 mail configuration
window server 2008 mail configurationwindow server 2008 mail configuration
window server 2008 mail configuration
 
Jms using j boss
Jms using j bossJms using j boss
Jms using j boss
 
Internet mail server
Internet mail server Internet mail server
Internet mail server
 
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
 
Postman.ppt
Postman.pptPostman.ppt
Postman.ppt
 
Ruby
RubyRuby
Ruby
 
009478419.pdf
009478419.pdf009478419.pdf
009478419.pdf
 
Spam Filter
Spam FilterSpam Filter
Spam Filter
 
Simple mail transfer protocol
Simple mail transfer protocolSimple mail transfer protocol
Simple mail transfer protocol
 
Topic 2.4 network services
Topic 2.4 network servicesTopic 2.4 network services
Topic 2.4 network services
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet1.ppt
Servlet1.pptServlet1.ppt
Servlet1.ppt
 
Ch22 system administration
Ch22 system administration Ch22 system administration
Ch22 system administration
 
Electronic mail
Electronic mailElectronic mail
Electronic mail
 
Computer networks unit v
Computer networks    unit vComputer networks    unit v
Computer networks unit v
 

More from Châu Thanh Chương (20)

Lecture18
Lecture18Lecture18
Lecture18
 
Lecture17
Lecture17Lecture17
Lecture17
 
Lecture16
Lecture16Lecture16
Lecture16
 
Lecture15
Lecture15Lecture15
Lecture15
 
Lecture14
Lecture14Lecture14
Lecture14
 
Lecture13
Lecture13Lecture13
Lecture13
 
Lecture12
Lecture12Lecture12
Lecture12
 
Lecture11
Lecture11Lecture11
Lecture11
 
Lecture10
Lecture10Lecture10
Lecture10
 
Lecture9
Lecture9Lecture9
Lecture9
 
Lecture8
Lecture8Lecture8
Lecture8
 
Lecture7 pattern
Lecture7 patternLecture7 pattern
Lecture7 pattern
 
Lecture6
Lecture6Lecture6
Lecture6
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture3
Lecture3Lecture3
Lecture3
 
Lecture2
Lecture2Lecture2
Lecture2
 
Lecture1
Lecture1Lecture1
Lecture1
 
7 data management design
7 data management design7 data management design
7 data management design
 
6 class design
6 class design6 class design
6 class design
 

Recently uploaded

By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
Jen Stirrup
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 

Recently uploaded (20)

By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 

Lecture19

  • 1. Server-side Web Programming Lecture 19: Java Mail
  • 2. How email works Sending Client Receiving Client Mail Client Mail Client Software Software SMTP POP/ IMAP Sending Server Receiving Server SMTP Mail Server Mail Server Software Software
  • 3. 3 protocols • SMTP: Simple Mail Transfer Protocol – is used to send a message from the mail client software to your mail server / or from one mail server to another • POP: Post Office Protocol – transfers all messages from the mail server to the mail client. POP in version 3: POP3 • IMAP: Internet Message Access Protocol – Is used by web-based mail services such as Hotmail, Yahoo, and Gmail. – Allows a web browser to read messages that are stored on the mail server. IMAP in version 4: IMAP4
  • 4. Normal Email Process • Mail client software converts message to SMTP (or other) format before sending to server • Common form for usual components of message: – Sender – Recipient(s) – Subject – Text – Etc. • Mail server sends message in MIME protocol to receiving server – Multipurpose Internet Message Extension • Mail client software on receiving client accesses using mail client software (possibly different) – POP – IMAP
  • 5. Java Mail • Java Mail API is a programming interface that makes it easy for Java developers to write code that automatically sends an email. • Key idea: Java Mail (or other mail software) must emulate what mail client software does – Create SMTP or other protocol message – Insert required mail components – Connect with sending server and transmit message • Requires mail classes – Download from www.java.sun.com – Insert mail.jar and activation.jar file into java libraries • mail. jar: contains the Java classes for the Java Mail API • activation. jar: contains the Java classes for the JavaBean Activation Framework. These classes are necessary for the JavaMail API to run
  • 6. How to install the Java Mail API and JavaBeans Activation Framework API • Locate the download page for the JavaMail API/ JavaBeans Activation Framework API on the Java website: www.java.sun.com • Click on the Download button and follow the instructions • Save the zip file: javamail-1_4.zip/jaf-1_1.zip to your hard disk • Extract the files from the zip file • Copy the mail.jar/activation.jar file to the JDK’s jrelibext directory
  • 7. Steps in Java Mail • Create a mail session – Creates email message object • Set message components – Properties of message object • Set addresses – Can be list for group of recipients • Send message – May need to identify self to server • Will need mail libraries <%@ page import="javax.mail.*, javax.mail.internet.*, java.util.*"%>
  • 8. Creating an Email Session • Create a Properties object to store information: – Generic Java class to store attribute/value pairs – Property props = new Properties(); • Specify the sending server: – props.put(“mail.smtp.host”, URL of mail server); • Create a new session object from those properties: – Session s = Session.getDefaultInstance(props); • Create new MimeMessage object from session – MimeMessage message = new MimeMessage(s);
  • 9. Common properties • mail.transport.protocol: specifies the protocol that’s used for the session • mail.smtp.host: specifies the host computer for SMTP server • mail.smtp.port: specifies the port that the SMTP server is using • mail.smtp.auth: specifies whether authentication is required to log in to the SMTP server • mail.smtp.quitwait: this property can be set to false to prevent an SSLException from occurring when you attempt to connect to a Gmail SMTP server
  • 10. • To set the properties of Properties object, you can use the put method to specify of a property name and value • The static getDefaultInstance method of the Session class returns the default Session object for the application • The setDebug method of the Session object can be used to print debugging information about the session to a log file • If you change the properties for a Session object, you must restart Tomcat before the changes take effect • If the Java application is running on the same computer as the SMTP server, you can use the localhost keyword to specify the SMTP host.
  • 11. Setting Message Components • Set the subject: – message.setSubject(“subject”); • Set the message text: – message.setText(“Thanks for your order!”); //”text/plain” • Could also be html or other types – Will need to specify MIME type String response = “<html><head><title>Reciept</title</head><body><h3 >Thank you for your order!</h3>”; message.setContent(response, “text/html”);
  • 12. Setting Addresses • Use setRecipient method – Specify internet address of recipient • Must use InternetAddress class • InternetAddress constructor requires email address, can also optionally give name – Specify TO, CC, or BCC – Can add multiple recipients Message.setRecipient(Message.RecipientType.TO, new InternetAddress(“slade@quarry.com ”)); Message.setRecipient(Message.RecipientType.CC, new InternetAddress(barney@aolrock.com, “Barney Rubble”));
  • 13. Setting Addresses • Address can also be a list – Good for mailing lists – Array of type Address • Use setRecipients method Address[] list = new Address[size of mailing list]; for (int i = 0; i < size of mailing list; i++) { list[i] = get next address for file; } Message.setRecipients(Message.RecipientType.TO, list); • Use addRecipient method Message.addRecipient(Message.RecipientType.TO, new InternetAddress(“slade@quarry.com”) ) Using addRecipient/addRecipients to add email addresses to any existing addresses for a message
  • 14. Sending Messages • Simple method: Transport.send(message); • More complex method if server requires authentication: – Create Transport object from sessions Transport t = session.getTransport(); – Connect to server with name and password t.connect(“your name”, “your password”); – Send the message t.sendMessage(message, message.getAllRecipients());