CSCI 6962: Server-side Design and Programming Server Created
Normal Process Sending Client Mail Client Software Outlook, for example Sending Server Mail Server Software
Normal 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 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
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 requires mail classes – –Insert mail.jar file into java libraries Create a mail session –Creates 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 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 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 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
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”);
Setting Addresses Use setRecipient method –Specify internet address of recipient Must use InternetAddress class InternetAddress constructor requires address, can also optionally give name –Specify TO, CC, or BCC –Can add multiple recipients Message.setRecipient(Message.RecipientType.TO, new Message.setRecipient(Message.RecipientType.CC, new “Barney
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);
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 – 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