Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java API
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Other JSE/JEE APIs
JavaMail help
Maximiliano Gonzalez
Greenhorn
Posts: 11
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello, I've never used JavaMail before.
I found this code in a blog and I think it helps but I need to know where should I place the user and password if I was to use Gmail's smtp. Here's the code:
package mailer; import java.io.*; import java.util.*; import javax.mail.*; import javax.mail.internet.*; public final class Mailer extends Object implements Serializable { /* Bean Properties */ private String to = null; private String from = null; private String subject = null; private String message = null; public static Properties props = null; public static Session session = null; static { /*Setting Properties for STMP host */ props = System.getProperties(); props.put("mail.smtp.host", "smtp.gmail.com"); session = Session.getDefaultInstance(props, null); } /* Setter Methods */ public void setTo(String to) { this.to = to; } public void setFrom(String from) { this.from = from; } public void setSubject(String subject) { this.subject = subject; } public void setMessage(String message) { this.message = message; } /* Sends Email */ public void sendMail() throws Exception { if(!this.everythingIsSet()) throw new Exception("Could not send email."); try { MimeMessage message = new MimeMessage(session); message.setRecipient(Message.RecipientType.TO, new InternetAddress(this.to)); message.setFrom(new InternetAddress(this.from)); message.setSubject(this.subject); message.setText(this.message); Transport.send(message); } catch (MessagingException e) { throw new Exception(e.getMessage()); } } /* Checks whether all properties have been set or not */ private boolean everythingIsSet() { if((this.to == null) || (this.from == null) || (this.subject == null) || (this.message == null)) return false; if((this.to.indexOf("@") == -1) || (this.to.indexOf(".") == -1)) return false; if((this.from.indexOf("@") == -1) || (this.from.indexOf(".") == -1)) return false; return true; } }
Thanks!
Ulf Dittmer
Rancher
Posts: 43081
77
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You might want to read through this excellent tutorial (which has many code examples):
http://java.sun.com/developer/onlineTraining/JavaMail/contents.html
Look for "authentication" in particular.
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Sending Email from jsp page
FileNotFoundException while sending Mail as attachment using javamail
FileNotFoundException while sending mail with attachment using javamail
Sending an email in JSP
query about javamail api
More...