Tarmoqni dasturlash asoslari Mustaqil ish Mavzu: JavaMail api bajardi: Tohirov Ibrohim Tekshirdi
Download 70.73 Kb.
|
JavaMail Api
Amaliy qism:
Kod: Ibrohim, [24.11.2023 20:00] import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.io.File; import java.io.IOException; import java.util.Properties; public class SendMailWithAttachment { public void Send() throws IOException {
Properties prop = new Properties(); prop.put("mail.smtp.host", "smtp.gmail.com"); prop.put("mail.smtp.port", "465"); prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.socketFactory.port", "465"); prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); Session session = Session.getInstance(prop, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try {
Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject("Message from Java Simplifying Tech"); String msg = "This email sent using JavaMailer API from Java Code!!!"; MimeBodyPart mimeBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); MimeBodyPart attachmentBodyPart = new MimeBodyPart(); attachmentBodyPart.attachFile(new File("D://Dasturlar//nature.jpg")); multipart.addBodyPart(attachmentBodyPart); message.setContent(multipart); Transport.send(message); System.out.println("Mail successfully sent.."); } catch (MessagingException e) { e.printStackTrace(); } } } Ibrohim, [24.11.2023 20:00] import java.io.IOException; public class Sender { public static void main(String[] args) throws IOException { SendMailWithAttachment sender = new SendMailWithAttachment(); sender.Send(); } } Xulosa JavaMail - bu SMTP, POP3 va IMAP protokollaridan foydalangan holda elektron pochta xabarlarini qabul qilish va yuborish uchun mo'ljallangan Java API. JavaMail Java EE platformasining bir qismidir, lekin Java SE ilovalarida foydalanish uchun ixtiyoriy paket sifatida ham mavjud. JavaMail API elektron pochta tizimini o‘z ichiga oluvchi obyektlarni aniqlovchi abstrakt klasslar to‘plamini ta’minlaydi. Bu elektron pochtani o‘qish, yozish va jo‘natish uchun paketdir. Download 70.73 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling