Home > Back-end >  Commons mail mail the most simple example, testing is available
Commons mail mail the most simple example, testing is available

Time:10-07

Package com. Email;

The import java.net.MalformedURLException;
The import java.net.URL;
Import the Java. Util. Arrays;

The import org.apache.com mons. Mail. DefaultAuthenticator;
The import org.apache.com mons. Mail. Email;
The import org.apache.com mons. Mail. EmailAttachment;
The import org.apache.com mons. Mail. EmailException;
The import org.apache.com mons. Mail. HtmlEmail;
The import org.apache.com mons. Mail. MultiPartEmail;
The import org.apache.com mons. Mail. SimpleEmail;

Public class test1 {

Public static void main (String [] args) throws EmailException, MalformedURLException {

//Email Email=new SimpleEmail ();
//email. SetHostName (" smtp.qq.com ");
//email. SetSmtpPort (465);
//email. SetAuthenticator (new DefaultAuthenticator (" [email protected] ", "fqiullgsbiqhdeha"));
//email. SetSSLOnConnect (true);
//email. SetCharset (" utf-8 ");
//email. SetFrom (" [email protected] ", "asdasd");
//email. AddTo (" [email protected] ");
//
//email. SetSubject (" TestMail ");
//email. SetMsg (" hello, This is a test mail... : -) ");
//email. The send ();
//email. The send ();

HtmlEmail email=new HtmlEmail ();
Email. SetHostName (" smtp.qq.com ");
Email. SetSmtpPort (465);
Email. SetAuthenticator (new DefaultAuthenticator (" [email protected] ", "fqiudsddhdeha"));
Email. SetSSLOnConnect (true);
Email. SetCharset (" utf-8 ");
Email. SetFrom (" [email protected] ", "cloud tax on");
Email. AddTo (" [email protected] ");


Test HTML email. SetSubject (" email ");//theme
URL the URL=new URL (" http://5b0988e595225.cdn.sohucs.com/images/20190522/c302a3ce10eb4e3aa872aba4ae1df302.png ");
String cid=email. Embed (url, "Apache logo");
Email. SetHtmlMsg (" & lt; html> Here is the test HTML email content: & lt; Br>
"
+ "http://5b0988e595225.cdsn.sohucs.com/images/20200522/c302a3casd1df302.png"
+ "& lt; Br>
+ "" & gt;

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- & lt; Br>
"
+ "united people: comfortable & lt; Br>
Contact phone number: 13811111111 & lt; Br>
"
+ "company address: high-tech zone 16 layer 6 & lt; Br>
"
+ "company website: http://www.tcadxsgo.cn
"+ "& lt; Br>
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- & lt;/html>" );//send content

Email. Send ();

System. Out.println (" Success!" );


}

}
  • Related