Simple Example of sending an email by javamail _ from blogjava

Source: Internet
Author: User
Tags mailmessage

Http://www.blogjava.net/wangfun/archive/2009/04/15/265748.html

 

Today, I learned about javamail. It is really troublesome to send an email to javamail. For future convenience, I wrote some code and made it into a jar package to facilitate future use. Haha

The following three pieces of code are all my codes. If you want to use them, copy them directly.

First Class: mailsenderinfo. Java

Package com. util. mail;
/***//**
* Basic information required for sending emails
* Author by wangfun
Http://www.5a520.cn novels 520
*/
Import java. util. properties;
Public class mailsenderinfo {
// IP address and port of the server sending the mail
Private string mailserverhost;
Private string mailserverport = "25 ";
// The sender's address
Private string fromaddress;
// Email recipient's address
Private string toaddress;
// Log on to the email sending server's username and password
Private string username;
Private string password;
// Whether authentication is required
Private Boolean validate = false;
// Email Subject
Private string subject;
// Text of the email
Private string content;
// File Name of the email attachment
Private string [] attachfilenames;
/***//**
* Get mail session attributes
*/
Public properties getproperties (){
Properties P = new properties ();
P. Put ("mail. SMTP. Host", this. mailserverhost );
P. Put ("mail. SMTP. Port", this. mailserverport );
P. Put ("mail. SMTP. Auth", validate? "True": "false ");
Return P;
}
Public String getmailserverhost (){
Return mailserverhost;
}
Public void setmailserverhost (string mailserverhost ){
This. mailserverhost = mailserverhost;
}
Public String getmailserverport (){
Return mailserverport;
}
Public void setmailserverport (string mailserverport ){
This. mailserverport = mailserverport;
}
Public Boolean isvalidate (){
Return validate;
}
Public void setvalidate (Boolean validate ){
This. Validate = validate;
}
Public String [] getattachfilenames (){
Return attachfilenames;
}
Public void setattachfilenames (string [] filenames ){
This. attachfilenames = filenames;
}
Public String getfromaddress (){
Return fromaddress;
}
Public void setfromaddress (string fromaddress ){
This. fromaddress = fromaddress;
}
Public String GetPassword (){
Return password;
}
Public void setpassword (string password ){
This. Password = password;
}
Public String gettoaddress (){
Return toaddress;
}
Public void settoaddress (string toaddress ){
This. toaddress = toaddress;
}
Public String GetUserName (){
Return username;
}
Public void setusername (string username ){
This. Username = username;
}
Public String getsubject (){
Return subject;
}
Public void setsubject (string subject ){
This. Subject = subject;
}
Public String getcontent (){
Return content;
}
Public void setcontent (string textcontent ){
This. content = textcontent;
}
}

Second Class: simplemailsender. Java

Package com. util. mail;

Import java. util. date;
Import java. util. properties;
Import javax. Mail. address;
Import javax. Mail. bodypart;
Import javax. Mail. message;
Import javax. Mail. messagingexception;
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;

/***//**
* Simple email (email without attachments) sender
Http://www.bt285.cn BT download
*/
Public class simplemailsender {
/***//**
* Send emails in text format
* @ Param mailinfo information of the email to be sent
*/
Public Boolean sendtextmail (mailsenderinfo mailinfo ){
// Determine whether identity authentication is required
Myauthenticator authenticator = NULL;
Properties pro = mailinfo. getproperties ();
If (mailinfo. isvalidate ()){
// If You Need identity authentication, create a password authenticator
Authenticator = new myauthenticator (mailinfo. GetUserName (), mailinfo. GetPassword ());
}
// Construct a mail sending session based on the mail session attribute and the password validator
Session sendmailsession = session. getdefaultinstance (Pro, Authenticator );
Try {
// Create an email message based on the session
Message mailmessage = new mimemessage (sendmailsession );
// Create the mail sender address
Address from = new internetaddress (mailinfo. getfromaddress ());
// Set the sender of the email message
Mailmessage. setfrom (from );
// Create the email receiver address and set it to the email message
Address to = new internetaddress (mailinfo. gettoaddress ());
Mailmessage. setrecipient (message. recipienttype. To, );
// Set the subject of the email message
Mailmessage. setsubject (mailinfo. getsubject ());
// Set the mail message sending time
Mailmessage. setsentdate (new date ());
// Set the main content of the email message
String mailcontent = mailinfo. getcontent ();
Mailmessage. settext (mailcontent );
// Send an email
Transport. Send (mailmessage );
Return true;
} Catch (messagingexception ex ){
Ex. printstacktrace ();
}
Return false;
}

/***//**
* Send emails in HTML Format
* @ Param mailinfo refers to the mail information to be sent.
*/
Public static Boolean sendhtmlmail (mailsenderinfo mailinfo ){
// Determine whether identity authentication is required
Myauthenticator authenticator = NULL;
Properties pro = mailinfo. getproperties ();
// If You Need identity authentication, create a password authenticator
If (mailinfo. isvalidate ()){
Authenticator = new myauthenticator (mailinfo. GetUserName (), mailinfo. GetPassword ());
}
// Construct a mail sending session based on the mail session attribute and the password validator
Session sendmailsession = session. getdefaultinstance (Pro, Authenticator );
Try {
// Create an email message based on the session
Message mailmessage = new mimemessage (sendmailsession );
// Create the mail sender address
Address from = new internetaddress (mailinfo. getfromaddress ());
// Set the sender of the email message
Mailmessage. setfrom (from );
// Create the email receiver address and set it to the email message
Address to = new internetaddress (mailinfo. gettoaddress ());
// The message. recipienttype. To attribute indicates that the type of the recipient is
Mailmessage. setrecipient (message. recipienttype. To, );
// Set the subject of the email message
Mailmessage. setsubject (mailinfo. getsubject ());
// Set the mail message sending time
Mailmessage. setsentdate (new date ());
// The minimultipart class is a container class that contains objects of the mimebodypart type.
Multipart mainpart = new mimemultipart ();
// Create a mimebodypart containing HTML content
Bodypart html = new mimebodypart ();
// Set HTML content
Html. setcontent (mailinfo. getcontent (), "text/html; charset = UTF-8 ");
Mainpart. addbodypart (HTML );
// Set the minimultipart object to the Mail content
Mailmessage. setcontent (mainpart );
// Send an email
Transport. Send (mailmessage );
Return true;
} Catch (messagingexception ex ){
Ex. printstacktrace ();
}
Return false;
}
}

Third class: myauthenticator. Java

 

Package com. util. mail;

Import javax. Mail .*;

Public class myauthenticator extends authenticator {
String username = NULL;
String Password = NULL;

Public myauthenticator (){
}
Public myauthenticator (string username, string password ){
This. Username = username;
This. Password = password;
}
Protected passwordauthentication getpasswordauthentication (){
Return new passwordauthentication (username, password );
}
}

The following code uses the above three classes:

Public static void main (string [] ARGs ){
// This class is mainly used to set emails
Mailsenderinfo mailinfo = new mailsenderinfo ();
Mailinfo. setmailserverhost ("smtp.163.com ");
Mailinfo. setmailserverport ("25 ");
Mailinfo. setvalidate (true );
Mailinfo. setusername ("han2000lei@163.com ");
Mailinfo. setpassword ("***********"); // your email password
Mailinfo. setfromaddress ("han2000lei@163.com ");
Mailinfo. settoaddress ("han2000lei@163.com ");
Mailinfo. setsubject ("set the mailbox title such as http://www.guihua.org China Osmanthus network ");
Mailinfo. setcontent ("set email content such as http://www.guihua.org China Osmanthus network is China's largest Osmanthus website = ");
// This class mainly sends emails
Simplemailsender SMS = new simplemailsender ();
SMS. sendtextmail (mailinfo); // sending Style
SMS. sendhtmlmail (mailinfo); // sends the message in HTML format.
}
Finally, I will give my friends some notes:
1. You can use this code to complete the javamail mail sending function. Three categories are indispensable.
2. I package the com. util. mail package for these three classes. If you do not like the package, you can change it by yourself, but the three class files must be in the same package.
3. Do not use the email address you just registered to send emails in the program. If your 163 email address is just registered, do not use "smtp.163.com ". Because you cannot send it out. The newly registered email address does not give you such permissions, that is, you cannot pass verification. It takes a long time to use your frequently used email address.
4. Another problem is mailinfo. setmailserverhost ("smtp.163.com"); and mailinfo. setfromaddress ("han2000lei@163.com. That is, if you use the 163smtp server, you must use the 163 email address to send the email. If you do not use the email address, the email will not be sent successfully.
5. There are many online explanations about javamail verification errors, but I only see one. Is my third class. As long as you copy all the code, I think there will be no problem.

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.