Download presentation
Presentation is loading. Please wait.
Published byVictoria Dawson Modified over 9 years ago
1
CSCI 6962: Server-side Design and Programming Server Created Email
2
Normal Email Process Sending Client Mail Client Software Outlook, for example Sending Server Mail Server Software
3
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.
4
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 Mail Server Software Receiving Server Mail Server Software Sending Client Mail Client Software
5
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
6
Java Mail Java email requires mail classes –http://www.oracle.com/technetwork/java/javamail/index.htmlhttp://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
7
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);
8
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");
9
Creating an Email Session Can also specify remote mail server if needed: Property props = new Properties(); props.put(“mail.transport.protocol”, “smtps”); props.put(“mail.smtps.host”, url of mail server); props.put(“mail.smtps.port”, portnumber); props.put(“mail.smtps.auth”, “true”); Session s = Session.getDefaultInstance(props); smtp.gmail.com, for example Server will generally require authentication, so use smtps protocol
10
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 = “ Reciept Th ank you for your order! ”; message.setText(response, “text/html”);
11
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”));slade@quarry.com Message.setRecipient(Message.RecipientType.CC, new InternetAddress(“barney@aolrock.com”, “Barney Rubble”));barney@aolrock.com
12
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);
13
Sending Messages Simple method: Transport.send(message);
14
Email in ASP Similar syntax to JSP –Must import System.Net.Mail package
15
Creating Messages Create MailMessage object –Set Subject and Body properties –Can set isBodyHtml to True for html messages
16
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
17
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.