Two activation. jar and mail. Jar packages are required for sending an email to javamail.

Source: Internet
Author: User
Tags how to send mail

Javamail
The required jar package contains two activation. jar and mail. Jar files (put in the mailbox)

Index. jsp fill in the relevant information. The email address and password used for sending emails must be correct.
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" errorpage = "../error. jsp" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Title> send email </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<LINK rel = "stylesheet" type = "text/CSS" href = "../image/com.css"/> <Body>
<Form method = "Post" name = "form1" Action = "sendemail. jsp" onsubmit = "return email ()">
<Table width = "480" Height = "393" border = "1" align = "center" cellpadding = "0" cellspacing = "0">
<Tr bordercolor = "# d4d0c8" bgcolor = "# feffc9">
<TD Height = "27" colspan = "2" align = "Left"> <Div align = "center"> <strong> send email </strong> </div> </TD>
</Tr>
<Tr bgcolor = "# feffc9">
<TD width = "68" Height = "27" align = "Left"> <Div align = "right"> recipient: </div> </TD>
<TD width = "406" Height = "27"> <Div align = "center">
<Input type = "text" name = "sname" size = "51">
</Div> </TD>
</Tr>
<Tr bgcolor = "# feffc9">
<TD Height = "27" align = "Left"> <Div align = "right"> Sender: </div> </TD>
<TD Height = "27"> <Div align = "center">
<Input type = "text" name = "jname" size = "51">
</Div> </TD>
</Tr>
<Tr bgcolor = "# feffc9">
<TD Height = "27" align = "Left"> <Div align = "right"> password & nbsp; Code: </div> </TD>
<TD Height = "27"> <Div align = "center"> <input type = "password" name = "password" size = "57"> </div> </ TD>
</Tr>
<Tr bgcolor = "# feffc9">
<TD Height = "27" align = "Left"> <Div align = "right"> main & nbsp; Question: </div> </TD>
<TD Height = "27"> <Div align = "center">
<Input name = "title" type = "text" size = "51">
</Div> </TD>
</Tr>
<Tr align = "Left" bgcolor = "# feffc9">
<TD Height = "227" valign = "TOP"> <Div align = "right"> content & nbsp;: </div> </TD>
<TD Height = "227"> <Div align = "center">
<Textarea name = "message" Cols = "50" rows = "15"> </textarea>
</Div> </TD>
</Tr>
<Tr align = "center" valign = "Middle" bordercolor = "# d4d0c8" bgcolor = "# feffc9">
<TD Height = "29" colspan = "2"> <input type = "Submit" name = "Submit" value = "send">
& Nbsp; <input type = "reset" name = "submit2" value = "clear"> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

Sendmail. jsp
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" errorpage = "" %>
<% @ Page import = "Java. Io. *" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "javax. Mail. *" %>
<% @ Page import = "javax. Mail. Internet. *" %>
<% @ Page import = "javax. Activation. *" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> send email </title>
</Head>
<LINK rel = "stylesheet" type = "text/CSS" href = "../image/com.css"/>
<%
Try {
Request. setcharacterencoding ("gb2312 ");
String from = request. getparameter ("jname ");
String to = request. getparameter ("sname ");
String subject = request. getparameter ("title ");
String messagetext = request. getparameter ("message ");
String Password = request. getparameter ("password ");

String S = request. getparameter ("jname ");
Int n = S. indexof ('@');
Int M = S. Length ();
String Server = S. substring (n + 1, M );

// Create a mail session
Properties pro = new properties ();
Pro. Put ("mail. SMTP. Host", "SMTP." + server );
Pro. Put ("mail. SMTP. Auth", "true ");
Session sess = session. getinstance (Pro );
Sess. setdebug (true );

// Create a message object
Mimemessage message = new mimemessage (sess );
// Set the sender
Internetaddress from_mail = new internetaddress (from );
Message. setfrom (from_mail );
// Set the recipient
Internetaddress to_mail = new internetaddress ();
Message. setrecipient (message. recipienttype. To, to_mail );
// Set the topic
Message. setsubject (subject );
// Set content
Message. settext (messagetext );
// Set the sending time
Message. setsentdate (new date ());
// Send an email
Message. savechanges (); // Save the email information
Transport transport = sess. gettransport ("SMTP ");
Transport. Connect ("SMTP." + server, from, password); // This sentence is critical, which is equivalent to logging on to the Administrator's mailbox to send an email
// Transport. Connect ("smtp.163.com", wode@163.com, "123456 ");
Transport. sendmessage (message, message. getallrecipients ());

Transport. Close ();
Out. Print ("email sent successfully ");

} Catch (exception e ){
Out. Print ("failed to send the email, probably because <ul> ");
Out. println (E. getmessage ());
}
%>
</Html>

Appendix
How to send mail via CERNET (2 jar packages)

Sendmail. jsp
String seting_stmp = "smtp.163.com ";
String user = "flypan_1 ";
String Password = "3118848 ";
String mail_from = "flypan_1@163.com ";
String mail_to = Email; // email is sent from the database.
String title = "you have successfully retrieved your password! ";
String content = "Congratulations! You have recovered your password:" + passwords + ". ";
Int A = mailsend. Send (seting_stmp, user, password, mail_from, mail_to, title, content );
If (A = 0)
{
Out. println ("<SCRIPT> ");
Out. println ("alert ('Congratulations, you have recovered your password. Please check it in your mailbox later! ')");
Out. println ("document. Location = '/index. jsp ';");
Out. println ("</SCRIPT> ");
}
Mailsend. Java

Package com. jetsum. util;

Import java. util. properties;
Import javax. Mail. bodypart;
Import javax. Mail. message;
Import javax. Mail. multipart;
Import javax. Mail. Session;
Import javax. Mail. Transport;
Import javax. Mail. Internet. internetaddress;
Import javax. Mail. Internet. mimebodypart;
Import javax. Mail. Internet. mimemessage;
Import javax. Mail. Internet. mimemultipart;

Public class mailsend {

Public mailsend (){
}

Public static int send (string seting_stmp, string user, string password,
String mail_from, string mail_to, String title,
String content ){
Int I = 0;
Try {

Properties props = new properties ();
Session sendsession;
Transport transport;

// Write the SMTP server address to the attribute
Props. Put ("mail. SMTP. Host", seting_stmp );

// Set the SMTP server to require permission Authentication
Props. Put ("mail. SMTP. Auth", "true ");

// Set output debugging information
// Popupauthenticator auth = new popupauthenticator ();

Sendsession = session. getinstance (props, null );
Sendsession. setdebug (true );

// Generate a message object based on the session
Message message = new mimemessage (sendsession );
// Set the sender address

Message. setfrom (New internetaddress (mail_from ));
// Set the recipient's address

Message. setrecipient (message. recipienttype. To, new internetaddress (mail_to ));

// Set the email title
Message. setsubject (title );

// Set the email sending time
// Message. setsentdate (new date ());

 

// Send an HTML-format email
// Create a bodypart object for storing the mail content
Bodypart MDP = new mimebodypart ();

// Set the content and format/encoding method for the bodypart object
MDP. setcontent (content, "text/html; charset = gb2312 ");

Multipart Mm = new mimemultipart ();
// Create a new mimemultipart object to store the bodypart pair.
// (In fact, multiple objects can be stored)
Mm. addbodypart (MDP );
// Add the bodypart to the mimemultipart object (multiple bodyparts can be added)

Message. setcontent (mm );

// Save the email Modification
Message. savechanges ();
// Generate a transport object based on the session
Transport = sendsession. gettransport ("SMTP ");
// Connect to the SMTP server
Transport. Connect (seting_stmp, mail_from, password );
// Send email
Transport. sendmessage (message, message. getallrecipients ());
// Disable the transport connection
Transport. Close ();
}
Catch (exception e ){
I = 1;
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
Return I;
}

}

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.