public class Sendmailtest
{
public static void Main (string[] args)
{
String title = "Titletest";/the title of the message sent
String from = "sir_znp@163.com";//Sent from there
String sendto[] = {"Znp@163.net", "sir_znp@163.com"};//sent there
The text content of the message, which can contain HTML tags and is displayed as an HTML page
String content = "Mail TEST!!!!!! <br><a href=#>aaa</a> ";
The included annexes, and the renaming of 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) thr oWS Exception
{
ResourceBundle mailprops = Resourcebundle.getbundle ("Mail"); You can read the corresponding parameters from the configuration file
Properties Props = new properties ();
String SMTP = "smtp.163.com"; Set SMTP to use for sending messages
String servername = "SIR_ZNP";
String serverpaswd = "123";
Javax.mail.Session mailsession; Mail Session Object
Javax.mail.internet.MimeMessage mimemsg; MIME Mail Object
Props = Java.lang.System.getProperties (); Get System Properties Object
Props.put ("Mail.smtp.host", SMTP); Setting up an SMTP host
Props.put ("Mail.smtp.auth", "true"); Whether to the server username and password Authentication
Verify that the user name and password sent to the server are correct
Email_autherticatorbean myemailauther = new Email_autherticatorbean (servername, SERVERPASWD);
Set up a mail session
MailSession = javax.mail.Session.getInstance (props, (authenticator) myemailauther);
Set Transport protocol
Javax.mail.Transport transport = Mailsession.gettransport ("SMTP");
Set information from, to, and so on
mimemsg = new Javax.mail.internet.MimeMessage (mailsession);
if (! Stringutils.isempty (from))
{
InternetAddress sentfrom = new internetaddress (from);
Mimemsg.setfrom (Sentfrom); Set Sender Address
}
internetaddress[] SendTo = new Internetaddress[to.length];
for (int i = 0; i < to.length; i++)
{
System.out.println ("Send to:" + to[i]);
Sendto[i] = new internetaddress (To[i]);
}
Multipart Multipart = new Mimemultipart ();//Attachment transfer format
Multipart.addbodypart (MESSAGEBODYPART1);
for (int i = 0; i < filenames.length; i++) {
MimeBodyPart messageBodyPart2 = new MimeBodyPart ();
Select each attachment name
String filename = Filenames[i].split (",") [0];
System.out.println ("Appendix Name:" + filename);
String displayname = Filenames[i].split (",") [1];
Get the data source
Filedatasource FDS = new Filedatasource (filename);
Get the attachment itself and into the bodypart
Messagebodypart2.setdatahandler (New DataHandler (FDS));
Get file name same to enter BodyPart
Messagebodypart2.setfilename (displayname);
Messagebodypart2.setfilename (Fds.getname ());
Messagebodypart2.setfilename (Mimeutility.encodetext (displayname));
Multipart.addbodypart (MESSAGEBODYPART2);
}
Mimemsg.setcontent (multipart);
Set the date the letter header was sent
Mimemsg.setsentdate (New Date ());
Mimemsg.savechanges ();
Send mail
Transport.send (MIMEMSG);
Transport.close ();
}
}
Validation 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);
}
}
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.