Witam, mam problem z wysłanie maili.
Kod:
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
@SuppressWarnings("serial")
public class MailPanel extends JPanel {
JButton send;
public MailPanel() {
setVisible(true);
send = new sendBtn();
add(send);
//setLayout(new GridLayout(2, 1));
}
void send() throws MessagingException {
final String username = "[email protected]";
final String password = "haslo";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
class sendBtn extends JButton implements ActionListener {
public sendBtn() {
super("Send");
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
new MailPanel().send();
} catch (MessagingException e1) {
e1.printStackTrace();
}
}
}
}