JSP mail sending instance

Source: Internet
Author: User
Tags mail account

Vishal_donth gave this response on 10/18/2000:
// These are the pakages to be imported from
// Java Mail
// The Java Mail PAckage either be dowloaded
// Seperately
// Or else is Available in the J2sdkee1.2
// (Java Enterprise Edition)

Import javax. mail .*;
Import javax. mail. internet .*;
Import java. util .*;


// This function can be used to send the mail
// With the parameters given to it
// U have to specify the smtp server through
// Which u have to send the mail
// Since I was trying with a homenetmail
// Account I directly sent the mail its server
// For sending this mail u need a mail server
// Which lets u to relay the messages
// Try this thing for sending to
// Www.homenetmail.com account because it lets
// U send
// Mails to the accounts like example try
// Sending it to a "abc@homenetmail.com"
// Account. Create the mail account in homenet
// Mail first. If u get any other server which
// Supports relaying u can try this on that
// Also.

// Use this function in ur Servlet to send
// Mail by calling the function with
// Parameters

Public void sendMail (String toAddr, String subject, String body, String fromAddr) throws RemoteException {
Try {
Properties props = new Properties ();
Props. put ("mail. smtp. host", "mail.homenetmail.com ");
// Here we specify the SMTP server through
// Which the mail shocould be delivered
Session session = Session. getDefaultInstance (props, null );
Message msg = new MimeMessage (session );
Msg. setFrom (new InternetAddress (fromAddr ));
// Specify the From Address
InternetAddress [] tos = InternetAddress. parse (toAddr );
// Specify the To Address
Msg. setRecipients (Message. RecipientType. TO, tos );
Msg. setSubject (subject );
// Specify the Subject
Msg. setText (body );
// Specify the Body
Transport. send (msg );
System. out. println ("Message is Sent ");
}
Catch (Exception e ){
System. out. println (e );
}
}

// U have to run this function on a computer
// Which is directly connected
// To internet but not through
// Proxy... or else use a proxy which
// Supports SMTP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.