Java uses Commons-email-1.2.jar for simple e-mail

Source: Internet
Author: User

1, first introduced Commons-email-1.2.jar package

2, look directly at the code bar Commons-email-1.2.jar has already encapsulated the method for us

A, first to a simple can not send attachments

public class Testcommonemail {
public static void Main (string[] args) {
Simpleemail simpleemail = new Simpleemail ();
Set up a mail server to use to send mail if QQ is smtp.qq.com premise is QQ has already opened SMTP service
Simpleemail.sethostname ("smtp.163.com");

Prevent Chinese garbled characters
Simpleemail.setcharset ("Utf-8");


try {
Recipient mailbox fit and recipient alias
Simpleemail.addto ("[Email protected]", "fireworks with different colors");
Sender's mailbox and password
Simpleemail.setauthentication ("[Email protected]",
"XXX");

Sender's mailbox and alias of this email address should be set with the use of mail server, meaning that QQ can not be sent by the SMTP mail NetEase
Simpleemail.setfrom ("[Email protected]", "NetEase mailbox");
Simpleemail.setsubject ("subject");
Simpleemail.setmsg ("Hello world! Hello, world! ");
Simpleemail.send ();


} catch (Emailexception e) {
TODO auto-generated Catch block
System.out.println ("Send Failed");
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("sent successfully");
}


}

B. Send mail with Attachments

public class Testcommonemail {
public static void Main (string[] args) {

Multipartemail simpleemail = new Multipartemail ();
Set up a mail server to use to send mail
Simpleemail.sethostname ("smtp.163.com");
Simpleemail.setcharset ("Utf-8");


try {
Recipient mailbox fit and recipient alias
Simpleemail.addto ("[Email protected]", "fireworks with different colors");
Sender's mailbox and password
Simpleemail.setauthentication ("[Email protected]",
"XXX");


Sender mailboxes and aliases this mailbox should correspond to settings using the mail server
Simpleemail.setfrom ("[Email protected]", "NetEase mailbox");
Simpleemail.setsubject ("subject");
Simpleemail.setmsg ("Hello world! Hello, world! ");
Emailattachment emailattachment = new Emailattachment ();

Attachments are placed in the project root directory
Emailattachment.setpath ("Naaomei.jpeg");
Simpleemail.attach (emailattachment);


Simpleemail.send ();


} catch (Emailexception e) {
TODO auto-generated Catch block
System.out.println ("Send Failed");
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("sent successfully");
}


}

C, packaging the method of sending mail optimize all sender information and email_host in the configuration file

Config.properties

Email_host=smtp.163.com
[Email protected]
Email_password=xxx


Read the class of properties in the configuration file Propertiesutil.java

public class Propertiesutil {
private static InputStream InputStream = null;


public static string Readstringvalue (String propertyname) throws Exception {
String _propertyname = "";
ClassLoader default load is the absolute path starting with Classpath cannot add "/", otherwise cannot find the file
InputStream = PropertiesUtil.class.getClassLoader ()
. getResourceAsStream ("config.properties");
System.out.println (Inputstream.available ());
Properties P = new properties ();
System.out.println ("P:" + P);
try {
P.load (InputStream);
} catch (IOException E1) {
Inputstream.close ();
}
_propertyname = P.get (PropertyName). ToString ();
Inputstream.close ();
return _propertyname;
}

Send various messages Emailutil.java


public class Emailutil {


public static void SendMail (string to, string subject, String msg)
Throws Exception {
Htmlemail email = new Htmlemail ();
Email.sethostname (Propertiesutil.readstringvalue ("Email_host"));
Email.setcharset ("Utf-8");
Email.addto (to);
Email.setfrom (Propertiesutil.readstringvalue ("Email_from"));
Email.setauthentication (Propertiesutil.readstringvalue ("Email_from"),
Propertiesutil.readstringvalue ("Email_password"));
Email.setsubject (subject);
Email.sethtmlmsg (msg);
Email.send ();
}


public static void Sendmailwithattachment (string to, string subject,
String msg, emailattachment[] attachs) throws Exception {
Htmlemail email = new Htmlemail ();
Email.sethostname (Propertiesutil.readstringvalue ("Email_host"));
Email.setcharset ("Utf-8");
Email.addto (to);
Email.setfrom (Propertiesutil.readstringvalue ("Email_from"));
Email.setauthentication (Propertiesutil.readstringvalue ("Email_from"),
Propertiesutil.readstringvalue ("Email_password"));
Email.setsubject (subject);
Email.sethtmlmsg (msg);
for (int i = 0; i < attachs.length; i++) {
Email.attach (Attachs[i]);
}
Email.send ();
}


public static void Sendmultipartmail (string to, string subject, String msg,
String fileName) throws Exception {


Create an Attachment Message object


Multipartemail multipartmail = new Multipartemail ();
Multipartmail.setcharset ("Utf-8");
Multipartmail.sethostname (Propertiesutil.readstringvalue ("Email_host"));
Create an attachment (the attachment can be a local path or a network URL)
Emailattachment emailattachment = new Emailattachment ();
Emailattachment.setpath (FileName);
Emailattachment.setdescription (emailattachment.attachment);
Emailattachment.setdescription ("This is a attach test!");
try {
System.out.println ("email_from======="
+ Propertiesutil.readstringvalue ("Email_from"));
Multipartmail.setfrom (Propertiesutil.readstringvalue ("Email_from"));
Multipartmail.setauthentication (
Propertiesutil.readstringvalue ("Email_from"),
Propertiesutil.readstringvalue ("Email_password"));
Multipartmail.addto (to);
MULTIPARTMAIL.ADDCC ("");
Multipartmail.setsubject (subject);
Multipartmail.setmsg (msg);
Multipartmail.attach (emailattachment);
Add an attachment
Multipartmail.attach (emailattachment);
Multipartmail.attach (emailattachment);
Multipartmail.attach (emailattachment);
Multipartmail.attach (emailattachment);
Send
Multipartmail.send ();
} catch (Exception e) {
TODO auto-generated Catch
E.printstacktrace ();


}


}


public static void Main (string[] args) throws Exception {
SYSTEM.OUT.PRINTLN ("Send mail Start");
Emailutil.sendmail ("[Email protected]", "Hello", "nice to meet you!");
Emailutil.sendmultipartmail ("[Email protected]", "Nihaodjfajs",
"111sdfgsdfgsdfgsd", "d:/text.pdf");
emailattachment[] emailattachments = new emailattachment[2];
Emailattachment emailattachment = new Emailattachment ();
Emailattachment.setpath ("D:/text1.pdf");
Emailattachments[0] = emailattachment;
Emailattachment agreement = new emailattachment ();
Agreement.setpath ("D:/text.pdf");
/*
* Agreement.setdisposition (emailattachment.attachment);
* Agreement.setdescription ("Booking Business Cooperation Agreement (Shanghai Multi-Trip)");
* Agreement.setname (Mimeutility.encodetext ("Booking Business Cooperation Agreement (Shanghai Multi-trip). doc"));
*/
EMAILATTACHMENTS[1] = agreement;
Emailutil.sendmailwithattachment ("[Email protected]",
"Newnewnihaodjfajs", "111sdfgsdfgsdfgsd", emailattachments);
SYSTEM.OUT.PRINTLN ("Success");
}
}





Java uses Commons-email-1.2.jar for simple e-mail

Related Article

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.