Send mail via SMTP

Source: Internet
Author: User

Package Email;import Java.io.file;import Java.util.properties;import javax.activation.datahandler;import Javax.activation.filedatasource;import Javax.mail.message;import Javax.mail.messagingexception;import Javax.mail.session;import Javax.mail.transport;import Javax.mail.message.recipienttype;import Javax.mail.internet.internetaddress;import Javax.mail.internet.mimebodypart;import Javax.mail.internet.mimemessage;import Javax.mail.internet.mimemultipart;public class Email {public static void main ( String[] args) {if (Args.length < 4) {return;} Parameter 1:email address//e.g [email  Protected],[email protected]//parameter 2:the Path of Attachment//parameter 3:email Context//parameter 3:email Title//////////////////////////////////////////////////////////////////email email = new email (); email. Createemail (args);} public void Createemail (string[] args) {try {Properties properties = new Properties ();//PROPERTIES.SEtproperty ("Mail.smtp.auth", "true");p Roperties.setproperty ("Mail.transport.protocol", "SMTP"); Properties.setproperty ("Mail.smtp.host", "Server");p roperties.setproperty ("Mail.smtp.port", "25"); Session session = Session.getinstance (properties, NULL); Session.setdebug (false); Message message = new MimeMessage (session); Message.setfrom (New InternetAddress ("My Service <[email protected") > ") message.setsubject (Args[3]); Message.setrecipients (Recipienttype.to,internetaddress.parse (args[0])); MimeBodyPart Bodypartattch = createattachment (args[1]); MimeBodyPart bodypartcontentandpic = createcontent (args[2]); Mimemultipart Mimemuti = new Mimemultipart ("mixed"); Mimemuti.addbodypart (Bodypartattch); Mimemuti.addbodypart ( Bodypartcontentandpic); message.setcontent (Mimemuti); Message.savechanges (); Transport.send (message);} catch (Messagingexception e) {e.printstacktrace (); System.out.println ("Email List:" +args[0]); System.out.println ("Attachment Path:" +args[1]); System.out.println ("Content:" +args[2]);}}public mimebodypart createattachment (String path) throws Messagingexception {MimeBodyPart MimeBodyPart = new MimeBodyPart (); Filedatasource DataSource = new Filedatasource (new File); Mimebodypart.setdatahandler (New DataHandler ( DataSource)); Mimebodypart.setfilename (Datasource.getname ()); return MimeBodyPart;} Public MimeBodyPart createcontent (String content) throws Messagingexception {Mimemultipart Mimemutipart = new Mimemultipart ("related"); MimeBodyPart Contentbodypart = new MimeBodyPart () contentbodypart.setcontent ("<br> </br><br>" + Content + "</br>", "TEXT/HTML;CHARSET=GBK"); Mimemutipart.addbodypart (Contentbodypart); MimeBodyPart Allbodypart = new MimeBodyPart (); allbodypart.setcontent (Mimemutipart); return allbodypart;}}

Send mail via SMTP

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.