Download mail - YSU CSIS

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Dynamic Host Configuration Protocol wikipedia , lookup

SIP extensions for the IP Multimedia Subsystem wikipedia , lookup

Zero-configuration networking wikipedia , lookup

Remote Desktop Services wikipedia , lookup

Lag wikipedia , lookup

Real-Time Messaging Protocol wikipedia , lookup

DomainKeys Identified Mail wikipedia , lookup

Transcript
CSCI 6962:
Server-side Design and Programming
Server Created Email
Normal Email Process
Sending Client
Mail Client
Software
Sending Server
Mail Server
Software
Outlook, for
example
Normal Email Process
• Mail client software converts message to SMTP (or other)
format before sending to server
– Simple Mail Transfer Protocol
• Common form for usual
components of message:
–
–
–
–
–
Sender
Recipient(s)
Subject
Text
Etc.
Normal Email Process
• 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 (post office protocol)
– IMAP (internet message access protocol)
Sending Server
Receiving Server
Mail Server
Software
Mail Server
Software
Sending Client
Mail Client
Software
Server-Generated Mail
• Key idea:
Server-side 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
Java Mail
• Java email requires mail classes
– http://www.oracle.com/technetwork/java/javamail/index.html
– Insert mail.jar file into java libraries
• 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
– Must catch any possible MessagingExceptions thrown
Creating an Email Session
• Create a Properties object to store information:
– Generic Java class to store attribute/value pairs
– Properties props = new Properties();
• Specify the sending server (if local):
– props.put(“mail.smtp.host”, “localhost”);
• Create a new session object from those properties:
– Session s = Session.getDefaultInstance(props);
• Create new MimeMessage object from session
– MimeMessage message = new MimeMessage(s);
Creating an Email Session
• Can also specify port of mail server if needed:
Property props = new Properties();
props.put(“mail.transport.protocol”, “smtp”);
props.put(“mail.smtp.host”, “localhost”);
props.put(“mail.smtp.port”, portnumber);
Session s = Session.getDefaultInstance(props);
• May need to provide authentication to mail server:
props.setProperty("mail.user", “your login id");
props.setProperty("mail.password", “your password");
Creating an Email Session
• Can also specify remote mail server if needed:
Property props = new Properties();
Server will generally
require authentication,
so use smtps protocol
props.put(“mail.transport.protocol”, “smtps”);
props.put(“mail.smtps.host”, url of mail server);
smtp.gmail.com, for example
props.put(“mail.smtps.port”, portnumber);
props.put(“mail.smtps.auth”, “true”);
Session s = Session.getDefaultInstance(props);
Setting Message Components
• Set the subject:
– message.setSubject(“subject”);
• Set the message text:
– message.setText(“Thanks for your order!”);
• Could also be html or other types
– Will need to specify MIME type
String response =
“<html><head><title>Reciept</title</head><body><h3>Th
ank you for your order!</h3>”;
message.setText(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(“[email protected]”));
Message.setRecipient(Message.RecipientType.CC,
new InternetAddress(“[email protected]”,
“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++) {
toAddress = get next address from file
list[i] = new InternetAddress(toAddress);
}
Message.setRecipients(Message.RecipientType.TO, list);
Sending Messages
• Simple method:
Transport.send(message);
Email in ASP
• Similar syntax to JSP
– Must import System.Net.Mail package
Creating Messages
• Create MailMessage object
– Set Subject and Body properties
– Can set isBodyHtml to True for html messages
Setting Addresses
• Construct MailAddress objects
– Email address
– Label name to display
• Set address properties for MailMessage
– From property for sender
– Add to list of recipients as either To, CC, or BCC
Defining SMTP Server
• Construct new SmtpClient object
– Set its Host property
– If login is necessary, construct a
System.Net.NetworkCredential object setting username
and password
– Send method sends the message