Java Send Mail

xiaoxiao2021-03-06  15

Import javax.mail. *; import javax.mail.internet. *; import java.util. *; // this function can be used to send the mail // u Have To Specify To IT // U Have To Specify To SPECIFY THE SMTP Server through // Which u Have to send the mail // Since I Was Trying with a homenetmail // Account I Directly Sent THE Mail ITS Server // for sending this mail u Need a mail server // Which lets u to relay the message // THIS Thing for sending to a //www.homenetmail.com Account Because It lets /// u Send //mails to the accounting it to a "abc@homenetmail.com" // Account. Create The Mail Account in Home // Mail First. If u Get Any Other Server Which // Supports RELAYING U CAN TRY THIS IN THATION IN UR Servlet To Send // Mail By Calling The Function with the // parameters public void sendMail (String toAddr, String subject, String body, String fromAddr) throws RemoteException {try {Properties props = new Properties (); props.put ( "mail.smtp.host", "mail.homenetmail. COM "); // Here we specify the SMTP server through // which the mail should be delivered Session session = Session.getDefaultInstance (props, null); Message msg = new MimeMessage (session); msg.setFrom (new InternetAddress (fromAddr)); / / Specify the From Address InternetAddress [] tos = InternetAddress.parse (toAddr); // Specify the To Address msg.setRecipients (Message.RecipientType.TO, tos); msg.setSubject (subject); // Specify the Subject msg. Settext (body); // specify the body transport.send (MSG); System.out.Println ("Message is Sent");} catch (Exception E) {system.out.println (e);

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

New Post(0)