Mail sender (you can send multiple addresses at the same time, you can take an attachment)

xiaoxiao2021-03-06  56

Sending mail main 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 ;

/ ** * Mail sender using Java.mail * /

Public class sendmailtest {public static void main (String [] args) {string title = "Titletest"; // Send message Title string from = "sir_znp@163.com"; // Send SENDTO [] = {"znp@163.net", "sir_znp@163.com"}; // Send text content to the // message, can include HTML tags displayed as html page string content = "mail test !!!!! !
aaa "; // The rename of attachments included, and attachments String filenames [] = {" f: //music//text1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.txt1.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"); can be read from the configuration file Corresponding parameters Properties PROPS = New Properties ();

String SMTP = "SMTP.163.com"; // Sets the SMTP STRING ServerName = "SIR_ZNP" used to send mail; string serverpaswd = "123";

Javax.mail.session mailsession; // message session object javax.mail.internet.mimeMimeMAGEMSG; // mime mail object props = java.lang.system.getProperties (); // Get system properties object Props.put ("Mail .smtp.host ", SMTP); // Set SMTP host Props.put (" mail.smtp.auth "," true "); // Whether to go to server username and password verification // User name and password to server verification sent correct Email_Autherticatorbean myEmailAuther = new Email_Autherticatorbean (servername, serverpaswd); // set the mail session mailSession = javax.mail.Session.getInstance (props, (Authenticator) myEmailAuther); // set the transfer protocol javax.mail.Transport transport = mailSession. getTransport ( "smtp"); // set from, to information mimeMsg = new javax.mail.internet.MimeMessage (mailSession); if (! StringUtils.isEmpty (from)) {InternetAddress sentFrom = new InternetAddress (from); mimeMsg .SetFrom (SENTFROM); // Set the sender address}

InternetAddress [] sendto = new internetdress [to.Length]; for (INT i = 0; i

MimeMsg.seTrecipients (javax.mail.internet.mimeMeMessage.RecipientType.to, Sendto); MIMEMSG.SETSUBJECT (Subject, "GB2312");

MIMEBODYPART MessageBodypart1 = new mimebodypart (); //messagebodypart.settext (tEXTODETOCHINESE (TEXT)); MessageBodypart1.setContent (Text, MimetyPE);

Multipart Multipart = new mimemultipart (); // Accessory Transport Format Multipart.addbodypart (MessageBodypart1);

For (int i = 0; i

Public void setusername (string username) {m_username = usrname;}

Public void setuserpass (string userpass) {m_userpass = usrpass;

Public email_autherticatorBean (String UserName, String Userpass) {super (); setUserName (username); setUserpass (Userpass);

} public passwordAuthentication getPasswordAuthentication () {

Return New PasswordAuthentication (m_username, m_userpass);}}

转载请注明原文地址:https://www.9cbs.com/read-93962.html

New Post(0)