Question about jmail failure to send emails

Source: Internet
Author: User

If the connection is normal, an error is reported. Check whether the following package is not imported into Lib,

Sample Code

Send in Sina mail, set pop and SMTP functions in Sina mail

Send. Java

Package com. Email;
Import javax. Mail .*;
Import javax. Mail. Message. recipienttype;
Import javax. Mail. Internet .*;
Import javax. Activation .*;
Import java. util .*;
Public class send {
String smtphost = "";
String user = "";
String Password = "";
String from = "";
Public String getfrom (){
Return from;
}

Public void setfrom (string from ){
This. From = from;
}

String to = "";
String subject = "";
String content = "";
 
// Send an email
Public Boolean send (){
Properties props = new properties ();//
Props. Put ("mail. SMTP. Host", this. smtphost );
Props. Put ("mail. SMTP. Auth", "true ");
Try {
Smtpauth auth = new smtpauth ();
Auth. setaccount (user, password );
Session mailsession = session. getdefaultinstance (props, auth );
Mailsession. setdebug (true );
Message message = new mimemessage (mailsession );
Message. setfrom (New internetaddress (from ));
Message. setrecipient (recipienttype. To, new internetaddress ());
Message. setsubject (this. Subject );
Message. settext (this. content );
Message. setsentdate (new date ());
Message. setheader ("X-priority", "1 ");
Message. savechanges ();
Transport transport = mailsession. gettransport ("SMTP ");
Transport. Connect (this. smtphost, user, password );
Transport. Send (message, message. getallrecipients ());
Transport. Close ();
Return true;
} Catch (exception e ){
// Todo: handle exception
E. printstacktrace ();
Return false;
}

}
Static class smtpauth extends authenticator {
String user, password;
Void setaccount (string user, string password ){
This. User = user;
This. Password = password;
}
Protected passwordauthentication getpasswordauthentication (){
Return new passwordauthentication (user, password );
}
}
///**
// * @ Param ARGs
//*/
// Public static void main (string [] ARGs ){
/// Todo auto-generated method stub
//
//}

Public String getsmtphost (){
Return smtphost;
}

Public void setsmtphost (string smtphost ){
Smtphost = smtphost;
}

Public String getuser (){
Return user;
}

Public void setuser (string user ){
This. User = user;
}

Public String GetPassword (){
Return password;
}

Public void setpassword (string password ){
This. Password = password;
}

 

Public String getto (){
Return;
}

Public void setto (string ){
This. To =;
}

Public String getsubject (){
Return subject;
}

Public void setsubject (string subject ){
This. Subject = subject;
}

Public String getcontent (){
Return content;
}

Public void setcontent (string content ){
Try {
Content = new string (content. getbytes ("iso8859-1"), "UTF-8 ");
} Catch (exception e ){
// Todo: handle exception
}
This. content = content;
}

}

Textmail. jsp

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">

<Title> my JSP 'textmail. jsp 'starting page </title>

<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->

</Head>

<Body>
<H2> send a text email </H2>
<Form name = "form1" method = "Post" Action = "sendmail1.jsp">
SMTP server: <input type = "text" id = "smtphost" name = "smtphost"/>
<Br>
Logon account: <input type = "text" id = "user" name = "user"/>
<Br>
Logon password: <input type = "password" id = "password" name = "password"/>
<Br>
Sender Email: <input type = "text" id = "from" name = "from"/>
<Br>
Recipient Email: <input type = "text" id = "to" name = "to"/>
<Br>
Email title: <input type = "text" id = "subject" name = "subject"/>
<Br>
Email content: <input type = "text" id = "content" name = "content"/>
<Br>
<Input type = "Submit" value = "developed">
</Form>
</Body>
</Html>

 

Sendmail1.jsp

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<JSP: usebean id = "mysend" class = "com. Email. Send"> </jsp: usebean>
<JSP: setproperty property = "*" name = "mysend"/>
<Body>
<%
Boolean status = mysend. Send ();
If (Status ){
Out. Print ("successful ");
} Else {
Out. Print ("failed ");
}

%>

 

 

 

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.