Send mail first introduce the package path of Java EE to classpath, already installed JBuilder can be found in its installation path Lib directory
To Mail.jar,activation.jar, add the two packages to the system's environment variables, so you can even do so without jbuilder development
was used.
/**
* <p>title: Simple mailbean</p>
* <p>description: Provides a Simple mail forwarding function (/p>
* <p>copyright:copyright (c) 2003</p>
* @version 1.0
*/
Class Extendstring {
Public extendstring () {
}
/**
Remove the white space characters at each end of the string and convert the string to the Chinese standard character gb2312 string.
*/
public string CS (String str) {//Remove white space characters at 2 end of string
try {
if (str = null)
Return "";
str = Str.trim ();
if (str = null)
Return "";
str = new String (str.getbytes ("8859_1"), "GBK");
}
catch (Exception e) {
System.out.println (e);
}
return str;
}
}
public class SendMail
{
Private String errmsg = "";
Private extendstring exstr = new extendstring ();
Private String sender = "";//Sender Address
Private String SMTPHost = "";//Mail sending server (SMTP)
Private String user = ""; Login user Name
Private String Password = "";//Login password
Private String subject = "";//mail theme
Public SendMail ()
{
This.setpropertiesattri ();
}
public void Setsender (String sender)
{
This.sender = sender;
}
Public String Getsender ()
{
return sender;
}
/** set up mail sending server (SMTP) * *
public void Setsmtphost (String smtphost) {this.smtphost = SMTPHost;}
Public String Getsmtphost () {return smtphost;}
/** Set Login User name * *
public void SetUser (String user)
{
This.user = user;
}
Public String GetUser ()
{
return user;
}
/** Set Login Password * *
public void SetPassword (String password)
{
This.password = password;
}
Public String GetPassword ()
{
return password;
}
/** Set Mail Theme * *
public void Setsubject (String subject)
{
This.subject = subject;
}
Public String Getsubject ()
{
return subject;
}
/**
* Use SMTP to send mail main program
* @throws Messagingexception Mail sent failed
*/
public void SMTP (String receiver,string content) throws Messagingexception
{
if (SMTPHost = = null) throw new Messagingexception ("SMTPHost not Found");
if (user = = null) throw new Messagingexception ("User not Found");
if (password = = null) throw new messagingexception ("Password not Found");
Session session = Session.getdefaultinstance (properties,
New Authenticator () {
Public Passwordauthentication getpasswordauthentication () {
return new passwordauthentication (user, password);
}
});
Get mail Session Object
MimeMessage mimemsg = new MimeMessage (session);//MIME Mail object creation
if (sender!= null)/Set Sender address
{
Mimemsg.setfrom (new InternetAddress (sender));
}
if (receiver!= null)/Set the recipient address
{
Mimemsg.setrecipients (Message.RecipientType.TO, Parse (receiver));
}
if (subject!= NULL)//Set Message subject
{
Mimemsg.setsubject (Subject, "GBK");
}
MimeBodyPart part = new MimeBodyPart ();//mail Content section
Part.settext (content = null?) "": Content, "GBK");
Set message format to HTML CQC
Part.setcontent (Content.tostring (), "TEXT/HTML;CHARSET=GBK");
Multipart Multipart = new Mimemultipart ();
Multipart.addbodypart (part)///Add Mail Content section to multipart
Mimemsg.setcontent (multipart)//increase multipart to the information body
Mimemsg.setsentdate (new Date ());//Set Send date
Transport.send (mimemsg);//Send mail
}
/** Resolve Address Collection String * *
Private internetaddress[] Parse (String addressset) throws Addressexception
{
ArrayList list = new ArrayList ();
StringTokenizer tokens = new StringTokenizer (AddressSet, ";");
while (Tokens.hasmoretokens ())
{
List.add (New InternetAddress (Tokens.nexttoken (). Trim ()));
}
internetaddress[] Addressarray = new internetaddress[list.size ()];
List.toarray (Addressarray);
return addressarray;
}
/**
* Interfaces for external calls
*/
public boolean sendmails (String mail,string content)
{
int maillen = 0;
int contentlen= 0;
if (mail = = null| | Content==null)
{
return false;
}
public static void Main (string[] args)
{
SendMail mail = new SendMail ();
String email = "feng_lei@ecfounder.com;ladofwind@163.com";
String content = "Account number: 123 Password: 123 <br/> Thank you for registering!<br/><a href= ' http://www.xxxx.com.cn ' target= ' _blank ' > Www.xxxx.com.cn</a><br/> sincerely <br/>xxxx <br/> Day ";
Try
{
Mail.sendmails (email,content);
}
catch (Exception ex)
{
System.err.println ("ex33:" +ex.tostring ());
}
}
}
After compiling, create a text file in the directory of class file Mailserver.properties,
Format:
Smtphost=smtp.163.com
User=user
Password=pwd
Sender=csdn@csdn.com
Subject=hello
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.