Sekedar sharing dan jangan terlalu di percaya ini hanya hasil contekan dari teman yang di edit sedemikian rupa
kl ngak salah begini programnya SMTP Server
Coding :
package SMTP;
import java.util.Properties;
import javax.mail.Message;
import
javax.mail.MessagingException;
import
javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import
javax.mail.internet.InternetAddress;
import
javax.mail.internet.MimeMessage;
/**
*
* @author Sutama_xtreme
*/
public class mailForm extends
javax.swing.JFrame {
/** membuat mailforlm baru */
public mailForm() {
initComponents();
}
private void initComponents() {
txtSMTPServer = new
javax.swing.JTextField();
txtTo = new javax.swing.JTextField();
txtFrom = new javax.swing.JTextField();
txtSubject = new
javax.swing.JTextField();
btnSend = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jScrollPane1 = new
javax.swing.JScrollPane();
txtMessages = new
javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnSend.setText("SEND");
btnSend.setName("btnSEND"); //
NOI18N
btnSend.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnSendActionPerformed(evt);
}
});
jLabel1.setText("SMTP");
jLabel2.setText("To");
jLabel3.setText("From");
jLabel4.setText("Subject");
txtMessages.setColumns(20);
txtMessages.setRows(5);
jScrollPane1.setViewportView(txtMessages);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(60, 60, 60)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtSMTPServer, javax.swing.GroupLayout.DEFAULT_SIZE, 339,
Short.MAX_VALUE)
.addComponent(txtTo, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
.addComponent(txtFrom, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
.addComponent(txtSubject,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)))
.addComponent(btnSend,
javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtSMTPServer, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtTo,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtFrom,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtSubject,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSend)
.addGap(54, 54, 54))
);
pack();
}
private void
btnSendActionPerformed(java.awt.event.ActionEvent evt) {
Properties props = new Properties();
props.put("mail.smtp.host",
"smtp.gmail.com");
props.put("mail.smtp.socketFactory.port",
"465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth",
"true");
props.put("mail.smtp.port",
"465");
Session
session = Session.getDefaultInstance(props,
new
javax.mail.Authenticator() {
protected
PasswordAuthentication getPasswordAuthentication() {
return
new PasswordAuthentication("sutama_xtreme@gmail.com","terunapolos");
}
});
try
{
Message
message = new MimeMessage(session);
message.setFrom(new
InternetAddress(txtFrom.getText()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(txtTo.getText()));
message.setSubject(txtSubject.getText());
message.setText(txtMessages.getText());
Transport.send(message);
System.out.println("Done");
}
catch (MessagingException e) {
throw
new RuntimeException(e);
}
}
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(mailForm.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(mailForm.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(mailForm.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(mailForm.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
/* membuat tampilan form */
java.awt.EventQueue.invokeLater(new
Runnable() {
public void run() {
new
mailForm().setVisible(true);
}
});
}
private javax.swing.JButton btnSend;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane
jScrollPane1;
private javax.swing.JTextField txtFrom;
private javax.swing.JTextArea txtMessages;
private javax.swing.JTextField
txtSMTPServer;
private javax.swing.JTextField txtSubject;
private javax.swing.JTextField txtTo;
}
Kalau sudah ngak da erorr lagi programnya kayak gni nie jadi tampilannya
No comments:
Post a Comment