Java mail sending program (can be sent to multiple addresses at the same time, can carry attachments), java mail

Source: Internet
Author: User

Java mail sending program (can be sent to multiple addresses at the same time, can carry attachments), java mail
Main Program for sending emails

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 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 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.