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
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Other JSE/JEE APIs
Sending MAIL by Java
marlajee Borstone
Ranch Hand
Posts: 35
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
can some one please help me to write the
java
code which wil sending the mail - simple and with attachment both ?
thanks in advance...
Sect7waliMalaG
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 89
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi
Here is the code sample you can use.
1. For sending a SimpleMail
import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; class SimpleMail { public static void main(String[] args) throws Exception{ Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtphost.xxxx"); Session mailSession = Session.getDefaultInstance(props, null); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("Hi"); message.setContent("Hi sec7waalimalaG how are you ?", "text/plain"); message.addRecipient(Message.RecipientType.TO, new InternetAddress("xxx@xxxx.com")); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }
2.) Mail with some attachment messages:-
import javax.mail.*; import javax.mail.internet.*; import javax.activation.FileDataSource; import javax.activation.DataHandler; import java.util.Properties; class SimpleMailWithAttachment2 { public static void main(String[] args) throws Exception{ boolean debug = false; Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtphost.xxxx.xxx"); Session mailSession = Session.getDefaultInstance(props, null); mailSession.setDebug(debug); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("Subject here"); MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent("Hi all, <br>Please find attached ALL Report for 16 July 2010. <br><br> Regards <br>Ramesh <br> Gurgaon", "text/html"); MimeBodyPart attachFilePart = new MimeBodyPart(); FileDataSource fds = new FileDataSource("D:\\reports\\employeesNames.xls"); attachFilePart.setDataHandler(new DataHandler(fds)); attachFilePart.setFileName(fds.getName()); Multipart mp = new MimeMultipart(); mp.addBodyPart(textPart); mp.addBodyPart(attachFilePart); message.setFrom(new InternetAddress("xxxxxxx@xxxxx.com")); message.setContent(mp); message.addRecipient(Message.RecipientType.TO, new InternetAddress("xxxxx@xxxx.com")); message.setContent(mp); message.addRecipient(Message.RecipientType.CC, new InternetAddress("xxxxxx@xxxx.com;xxxxx@xxxxx.com")); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.sendMessage(message, message.getRecipients(Message.RecipientType.CC)); transport.close(); } }
regards,
Ramesh
Rob Spoor
Sheriff
Posts: 22821
132
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Moving to Other JSE/JEE APIs.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Javax.mail.MessagingException
how to implement sending mail?
problem in sending mail using jsp
How to send HTML page in mail insted of text
Mailing query
More...