Kafedrasi “tda” fanidan


Download 118.8 Kb.
bet6/6
Sana23.04.2023
Hajmi118.8 Kb.
#1390675
1   2   3   4   5   6
Bog'liq
SMTP protokoli asosida tarmoq dasturini yaratish

3.3. SMTP dastur kodi
Prayekt oynasi

Proyekt kodi
import java.util.Properties;
import javax.swing.JOptionPane;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class smtp extends javax.swing.JFrame {

public smtp() {
initComponents();
}

@SuppressWarnings("unchecked")


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String from = jTextField1.getText();
String pass = jPasswordField1.getText();
String to = jTextField3.getText() ; // list of recipient email addresses
String title = jTextField4.getText();
String body = jTextArea1.getText();
// JOptionPane.showMessageDialog(null, "message sucessfully send to: "+pass);
sendMail(from,pass,to,title,body); // TODO add your handling code here:
}

private void jTextField6KeyTyped(java.awt.event.KeyEvent evt) {


jTextField2.setText(jTextField1.getText()+evt.getKeyChar());
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
static void sendMail(String from, String pass, String to, String title, String body){
byte error=0;
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props);


MimeMessage message = new MimeMessage(session);

try {
message.setFrom(new InternetAddress(from));


InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(title);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (AddressException ae) {
error=1;
JOptionPane.showMessageDialog(null, ae.toString());
ae.printStackTrace();
}
catch (MessagingException me) {
error=1;
JOptionPane.showMessageDialog(null, me.toString());
me.printStackTrace();
}finally{
if(error==0)
JOptionPane.showMessageDialog(null, "message sucessfully send to: "+to);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(smtp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(smtp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(smtp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(smtp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new smtp().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}
Xulosa
Bu kurs ishimavzusida SMTP protokoli asosida tarmoq dasturini tuzish, electron pochta va SMTP protokollari haqida ma’lumotlarga ega bo’lindi. Bunda keltirilgan dasturda proyekt oynasi xosil qilindi. Proyekt oynasida MAIL adresi, Parol, Jo’natuvchi, Qabul qiluvchi, Title, Text va Send maydonlari mavjud. Biz proyekt oynasining MAIL adresi, Parol, Jo’natuvchi, Qabul qiluvchi maydonlariga kerakli adresslarni kiritamiz, oynaning Title qismiga “Salom” so’zi kiritilgan. Text qismiga xam ma’lumotlarniyozib Send tugmasiorqali jo’natishimiz mumkin bo’ladi.
Elektron pochta ko‘p tomonlama oddiy pochta xizmatiga o‘xshab ketadi. Xabarlar foydalanuvchi tomonidan kompyuter ishchi stolidagi dastur yordamida, matnli redaktor yordamida yoki veb brauzer yordamida tayyorlanadi. Keyin foydalanuvchi elektron pochtani yuborish dasturi chaqiriladi.
Elektron pochta – ro‘yxatga olingan adreslar o‘rtasida xabarlar almashish xizmati hisoblanadi. Dastlab elektron pochta haqida gapirilganda matnli ma’lumotlarni jo‘natish tushuniladi. Xabarlarni raqamli ko‘rinishda tasvirlash uchun ASCII kodlash tizimi qo‘llaniladi. Unda kodlashda xar bir simvol uchun 8 bit ishlatiladi.
Elektron pochta adresi quyidagi ko‘rinishda bo‘ladi:
elektronpochta@pochta.domen, talaba@tuit.uz
Bu yerda, pochta.domen – domen nomi, elektronpochta – foydalanuvchi ismi, identifikatori. Pochta adresi bitta odamga, guruxga yoki tashkilotga tegishli bo‘lishi mumkin.
Foydalanilgan adabiyotlar ro’yxati
1.Ахbоrоt-kоmmunikatsion tizimlar xavsizligi G`aniyev S. K.,
2.Karimov М. М., Тashev К. А. Tohkent axborot texnologiyalari universiteti Axborot xavsizligidan o`quv uslubiy qullanma Toshkent_2010
3.www.google.com
4. www.ziyonet.uz
5.www.tuit.uz
6.www.tuitff.uz
Download 118.8 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling