Mercurial Hosting > nabble
comparison src/fschmidt/util/mail/javamail/SmtpServerImpl.java @ 68:00520880ad02
add fschmidt source
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 05 Oct 2025 17:24:15 -0600 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 67:9d0fefce6985 | 68:00520880ad02 |
|---|---|
| 1 /* | |
| 2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com> | |
| 3 | |
| 4 Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 of this software and associated documentation files (the "Software"), to deal | |
| 6 in the Software without restriction, including without limitation the rights | |
| 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 copies of the Software, and to permit persons to whom the Software is | |
| 9 furnished to do so, subject to the following conditions: | |
| 10 | |
| 11 The above copyright notice and this permission notice shall be included in | |
| 12 all copies or substantial portions of the Software. | |
| 13 | |
| 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 THE SOFTWARE. | |
| 21 */ | |
| 22 | |
| 23 package fschmidt.util.mail.javamail; | |
| 24 | |
| 25 import java.io.UnsupportedEncodingException; | |
| 26 import java.util.Properties; | |
| 27 | |
| 28 import javax.mail.Address; | |
| 29 import javax.mail.Authenticator; | |
| 30 import javax.mail.MessagingException; | |
| 31 import javax.mail.PasswordAuthentication; | |
| 32 import javax.mail.Session; | |
| 33 import javax.mail.Transport; | |
| 34 | |
| 35 import com.sun.mail.smtp.SMTPMessage; | |
| 36 | |
| 37 import fschmidt.util.mail.Mail; | |
| 38 import fschmidt.util.mail.MailAddress; | |
| 39 import fschmidt.util.mail.MailException; | |
| 40 import fschmidt.util.mail.SmtpServer; | |
| 41 | |
| 42 | |
| 43 final class SmtpServerImpl implements SmtpServer { | |
| 44 private final PasswordAuthentication pa; | |
| 45 private final Authenticator auth = new Authenticator() { | |
| 46 protected PasswordAuthentication getPasswordAuthentication() { | |
| 47 return pa; | |
| 48 } | |
| 49 }; | |
| 50 private final Properties props = new Properties(System.getProperties()); | |
| 51 private Session session = null; | |
| 52 | |
| 53 SmtpServerImpl(String machineName) { | |
| 54 props.setProperty("mail.smtp.host",machineName); | |
| 55 pa = null; | |
| 56 } | |
| 57 | |
| 58 SmtpServerImpl(String machineName,String username,String password) { | |
| 59 props.setProperty("mail.smtp.host",machineName); | |
| 60 props.setProperty("mail.smtp.auth","true"); | |
| 61 pa = new PasswordAuthentication(username,password); | |
| 62 } | |
| 63 | |
| 64 public String getUserName() { | |
| 65 return pa==null ? null : pa.getUserName(); | |
| 66 } | |
| 67 | |
| 68 public void useSsl() { | |
| 69 props.setProperty("mail.smtp.starttls.enable","true"); | |
| 70 props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | |
| 71 //props.setProperty("mail.smtp.socketFactory.port", "465"); | |
| 72 //props.setProperty("mail.smtp.port", "465"); | |
| 73 } | |
| 74 | |
| 75 public void useStartTls() { | |
| 76 props.setProperty("mail.smtp.starttls.enable","true"); | |
| 77 } | |
| 78 | |
| 79 public void setPort(int port) { | |
| 80 String s = Integer.toString(port); | |
| 81 props.setProperty("mail.smtp.socketFactory.port", s); | |
| 82 props.setProperty("mail.smtp.port", s); | |
| 83 } | |
| 84 /* | |
| 85 public void setSmtpFrom(String smtpFrom) { | |
| 86 props.setProperty("mail.smtp.from", smtpFrom); | |
| 87 } | |
| 88 */ | |
| 89 public void setDebug(boolean b) { | |
| 90 props.setProperty("mail.debug", Boolean.toString(b)); | |
| 91 } | |
| 92 | |
| 93 private synchronized Session getSession() { | |
| 94 if( session==null ) { | |
| 95 session = Session.getInstance(props,auth); | |
| 96 } | |
| 97 return session; | |
| 98 } | |
| 99 | |
| 100 public void send(Mail m,MailAddress... addresses) throws MailException { | |
| 101 try { | |
| 102 MailImpl.MyMimeMessage msg = ((MailImpl)m).msg; | |
| 103 msg.setSession(getSession()); | |
| 104 if( addresses.length==0 ) { | |
| 105 Transport.send(msg); | |
| 106 } else { | |
| 107 Address[] a = MailImpl.addr(addresses); | |
| 108 Transport.send(msg,a); | |
| 109 } | |
| 110 } catch(MessagingException e) { | |
| 111 throw MailImpl.e(e); | |
| 112 } catch(UnsupportedEncodingException e) { | |
| 113 throw new MailException(e); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 public void sendFrom(Mail m,String from) throws MailException { | |
| 118 try { | |
| 119 final MailImpl.MyMimeMessage msg = ((MailImpl)m).msg; | |
| 120 msg.setSession(getSession()); | |
| 121 SMTPMessage smtpMsg = new SMTPMessage(msg) { | |
| 122 protected void updateHeaders() throws MessagingException { | |
| 123 super.updateHeaders(); | |
| 124 setHeader("Message-ID", msg.getMessageID()); | |
| 125 } | |
| 126 }; | |
| 127 smtpMsg.setEnvelopeFrom(from); | |
| 128 Transport.send(smtpMsg); | |
| 129 } catch(MessagingException e) { | |
| 130 throw MailImpl.e(e); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 } |
