About author  I am Java/oracle professional.Working on Java/J2EE technologies and i.e Java,J2ee,Oracle ADF,hibernate,J2ee,PL/sql,Apps for 4+ years.I am passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion on vinay@techartifact.com http://www.linkedin.com/in/vinaykumar2 Read more from this author


We can send mail through JavaMail Api through java code.I have read somewhere and thought of sharing with everyone.
For using this code you need to JavaMail Api.

//get mail session

Properties props = new Properties();
props.put("mail.smtp.host","localhost");
Session session = session.getDefaultInstance(props);

// create the messge.
MimeMessage message = new MimeMessage(session);
message.setSubject("Vinay first Mail");
Message.setText("Vinay mail text ");

//address the message
InternetAddress addfrm = new InternetAddress("vinaycen@gmail.com");
message.setFrom(addfrm);
InternetAddress addto = new InternetAddress("vinaycen@yahoo.com");
message.setRecipient(Message.RecipientType.TO,addto);

//send message
Transport.send(message);

Update

Send email in text and html format Text and HTML format

pimp it

5 Responses to Using JavaMail API to send an email in JAVA


  1. Rommel SharmaNo Gravatar
    Aug 10, 2009

    To add to the above – not to forget the essentials:

    The Java Mail API used here is like a Mail User Agent. In the example above you need the mail transfer agent – that in this example assumes ‘localhost’.

    Please give your SMTP server address that acts as the MTA, without which it would not work.

    Rest looks good.

    Cheers,
    Rommel.


  2. AhmadNo Gravatar
    Aug 19, 2009

    What about adding attachments?


  3. waqas aliNo Gravatar
    Dec 02, 2009

    sir, i need help and source code form you about (“sending text from pc without using a fax machine”).

Trackbacks/Pingbacks

  1. PimpThisBlog.com
  2. java send mail in text and html | TechArtifact

Leave a Reply