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. 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");
}< br> catch (exception e) {
system. out. println (E);
}< BR >}

// U have to run this function on a computer
// which is directly connected
// to Internet but not through a
/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.