diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fschmidt/util/mail/javamail/SmtpServerImpl.java	Sun Oct 05 17:24:15 2025 -0600
@@ -0,0 +1,134 @@
+/*
+Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+package fschmidt.util.mail.javamail;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Properties;
+
+import javax.mail.Address;
+import javax.mail.Authenticator;
+import javax.mail.MessagingException;
+import javax.mail.PasswordAuthentication;
+import javax.mail.Session;
+import javax.mail.Transport;
+
+import com.sun.mail.smtp.SMTPMessage;
+
+import fschmidt.util.mail.Mail;
+import fschmidt.util.mail.MailAddress;
+import fschmidt.util.mail.MailException;
+import fschmidt.util.mail.SmtpServer;
+
+
+final class SmtpServerImpl implements SmtpServer {
+	private final PasswordAuthentication pa;
+	private final Authenticator auth = new Authenticator() {
+		protected PasswordAuthentication getPasswordAuthentication() {
+			return pa;
+		}
+	};
+	private final Properties props = new Properties(System.getProperties());
+	private Session session = null;
+
+	SmtpServerImpl(String machineName) {
+		props.setProperty("mail.smtp.host",machineName);
+		pa = null;
+	}
+
+	SmtpServerImpl(String machineName,String username,String password) {
+		props.setProperty("mail.smtp.host",machineName);
+		props.setProperty("mail.smtp.auth","true");
+		pa = new PasswordAuthentication(username,password);
+	}
+
+	public String getUserName() {
+		return pa==null ? null : pa.getUserName();
+	}
+
+	public void useSsl() {
+		props.setProperty("mail.smtp.starttls.enable","true");
+		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
+		//props.setProperty("mail.smtp.socketFactory.port", "465");
+		//props.setProperty("mail.smtp.port", "465");
+	}
+
+	public void useStartTls() {
+		props.setProperty("mail.smtp.starttls.enable","true");
+	}
+
+	public void setPort(int port) {
+		String s = Integer.toString(port);
+		props.setProperty("mail.smtp.socketFactory.port", s);
+		props.setProperty("mail.smtp.port", s);
+	}
+/*
+	public void setSmtpFrom(String smtpFrom) {
+		props.setProperty("mail.smtp.from", smtpFrom);
+	}
+*/
+	public void setDebug(boolean b) {
+		props.setProperty("mail.debug", Boolean.toString(b));
+	}
+	
+	private synchronized Session getSession() {
+		if( session==null ) {
+			session = Session.getInstance(props,auth);
+		}
+		return session;
+	}
+
+	public void send(Mail m,MailAddress... addresses) throws MailException {
+		try {
+			MailImpl.MyMimeMessage msg = ((MailImpl)m).msg;
+			msg.setSession(getSession());
+			if( addresses.length==0 ) {
+				Transport.send(msg);
+			} else {
+				Address[] a = MailImpl.addr(addresses);
+				Transport.send(msg,a);
+			}
+		} catch(MessagingException e) {
+			throw MailImpl.e(e);
+		} catch(UnsupportedEncodingException e) {
+			throw new MailException(e);
+		}
+	}
+
+	public void sendFrom(Mail m,String from) throws MailException {
+		try {
+			final MailImpl.MyMimeMessage msg = ((MailImpl)m).msg;
+			msg.setSession(getSession());
+			SMTPMessage smtpMsg = new SMTPMessage(msg) {
+				protected void updateHeaders() throws MessagingException {
+					super.updateHeaders();
+					setHeader("Message-ID", msg.getMessageID());
+				}
+			};
+			smtpMsg.setEnvelopeFrom(from);
+			Transport.send(smtpMsg);
+		} catch(MessagingException e) {
+			throw MailImpl.e(e);
+		}
+	}
+
+}