Java email sending program (it can be sent to multiple addresses at the same time and can contain attachments)

Source: Internet
Author: User

Email Recipient Program

Import java. util. properties;
Import common. util. email_autherticatorbean;
Import javax. Mail. authenticator;
Import javax. Mail. Internet. internetaddress;
Import org. Apache. commons. Lang. stringutils;
Import javax. Mail. Internet. mimebodypart;
Import javax. Mail. multipart;
Import javax. Activation. filedatasource;
Import javax. Mail. Internet. mimemultipart;
Import javax. Activation. datahandler;
Import javax. Mail. Internet. mimeutility;
Import java. util. date;

/**
* Use the java. Mail email sending program
*/

Public class sendmailtest
{
Public static void main (string [] ARGs)
{
String title = "titletest"; // the title of the sent Email
String from = "sir_znp@163.com"; // send from there
String sendto [] = {"znp@163.net", "sir_znp@163.com"}; // send to there
// The text content of the email, which can contain HTML tags to display as HTML pages
String content = "mail test !!!!!! <Br> <a href = #> AAA </a> ";
// Include the attachment and rename the attachment
String filenames [] = {"F: \ music \ text1.txt,text1.txt", "F: \ music \ text2.txt,text2.txt "};
Try {
// Mailsender = new mailsender ();
Sendmail (title, from, sendto, content, filenames, "text/html; charset = gb2312 ");
} Catch (exception ex) {ex. printstacktrace ();}
}

Public static void Sendmail (string subject, string from, string [] to, string text, string [] filenames, string mimetype) throws exception
{
// Resourcebundle mailprops = resourcebundle. getbundle ("mail"); corresponding parameters can be read from the configuration file
Properties props = new properties ();

String SMTP = "smtp.163.com"; // you can specify the SMTP used to send emails.
String servername = "sir_znp ";
String serverpawd = "123 ";

Javax. Mail. Session mailsession; // mail Session Object
Javax. Mail. Internet. mimemessage mimemsg; // mime email object

Props = java. Lang. system. getproperties (); // obtain the system property object
Props. Put ("mail. SMTP. Host", SMTP); // set the SMTP host
Props. Put ("mail. SMTP. Auth", "true"); // verify the server user name and password
// Verify that the user name and password sent are correct on the server
Email_autherticatorbean myemailauther = new email_autherticatorbean (servername, serverpaswd );
// Set the email session
Mailsession = javax. Mail. session. getinstance (props, (authenticator) myemailauther );
// Set the transmission protocol
Javax. Mail. Transport transport = mailsession. gettransport ("SMTP ");
// Set information such as from and
Mimemsg = new javax. Mail. Internet. mimemessage (mailsession );
If (! Stringutils. isempty (from ))
{
Internetaddress sentfrom = new internetaddress (from );
Mimemsg. setfrom (sentfrom); // sets the sender address.
}

Internetaddress [] sendto = new internetaddress [to. Length];
For (INT I = 0; I <to. length; I ++)
{
System. Out. println ("sent to:" + to [I]);
Sendto [I] = new internetaddress (to [I]);
}

Mimemsg. setrecipients (javax. Mail. Internet. mimemessage. recipienttype. To, sendto );
Mimemsg. setsubject (subject, "gb2312 ");

Mimebodypart messagebodypart1 = new mimebodypart ();
// Messagebodypart. settext (unicodetochinese (text ));
Messagebodypart1.setcontent (text, mimetype );

Multipart = new mimemultipart (); // The attachment transmission format.
Multipart. addbodypart (messagebodypart1 );

For (INT I = 0; I <filenames. length; I ++ ){
Mimebodypart messagebodypart2 = new mimebodypart ();
// Select the name of each attachment
String filename = filenames [I]. Split (",") [0];
System. Out. println ("attachment name:" + filename );
String displayname = filenames [I]. Split (",") [1];
// Obtain the data source
Filedatasource FDS = new filedatasource (filename );
// Obtain the attachment and add it to the bodypart
Messagebodypart2.setdatahandler (New datahandler (FDS ));
// Get the same name as bodypart
// Messagebodypart2.setfilename (displayname );
// Messagebodypart2.setfilename (FDS. getname ());
Messagebodypart2.setfilename (mimeutility. encodetext (displayname ));
Multipart. addbodypart (messagebodypart2 );
}
Mimemsg. setcontent (multipart );
// Set the sending date of the mail header
Mimemsg. setsentdate (new date ());
Mimemsg. savechanges ();
// Send an email
Transport. Send (mimemsg );
Transport. Close ();
}

}

Verification class
Package common. util;
Import javax. Mail. authenticator;
Import javax. Mail. passwordauthentication;
Public class email_autherticatorbean extends authenticator
{
Private string m_username = NULL;
Private string m_userpass = NULL;

Public void setusername (string username)
{
M_username = username;
}

Public void setuserpass (string userpass)
{
M_userpass = userpass;
}

Public email_autherticatorbean (string username, string userpass)
{
Super ();
Setusername (username );
Setuserpass (userpass );

}
Public passwordauthentication getpasswordauthentication ()
{

Return new passwordauthentication (m_username, m_userpass );
}
}

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.