Created
August 21, 2015 02:50
-
-
Save kurtisdunn/8b53ae5506a9aa3b01fa to your computer and use it in GitHub Desktop.
Mailer in JAVA - Circa 2008
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package << package >>; | |
import java.io.*; | |
import java.util.*; | |
import javax.mail.*; | |
import javax.mail.event.*; | |
import javax.mail.internet.*; | |
public class TestMail { | |
public TestMail() {} | |
public void sendMail() { | |
try { | |
// Create mailer. | |
String hostname = "<< host >>"; | |
int port = 25; | |
String username = "<< user >>"; | |
String password = "<< pass >>"; | |
// Send mail. | |
String from = ""; | |
String to = "<< email >>"; | |
String subject = "Website Inquiry"; | |
String messageText = request.getParameter(); | |
boolean sessionDebug = false; | |
// Create some properties and get the default Session. | |
Properties props = System.getProperties(); | |
props.put("mail.host", hostname); | |
props.put("mail.transport.protocol", "smtp"); | |
Session mailSession = Session.getDefaultInstance(props, null); | |
// Set debug on the Session | |
// Passing false will not echo debug info, and passing True will. | |
mailSession.setDebug(sessionDebug); | |
// Instantiate a new MimeMessage and fill it with the | |
// required information. | |
Message msg = new MimeMessage(mailSession); | |
msg.setFrom(new InternetAddress(from)); | |
InternetAddress[] address = { | |
new InternetAddress(to) | |
}; | |
msg.setRecipients(Message.RecipientType.TO, address); | |
msg.setSubject(subject); | |
msg.setSentDate(new Date()); | |
msg.setText(message); | |
// Hand the message to the default transport service | |
// for delivery. | |
Transport.send(msg); | |
System.out.println("Mail was sent to " + to); | |
System.out.println(" from " + from); | |
System.out.println(" using host " + hostname + "."); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment