【发布时间】:2016-09-26 12:30:29
【问题描述】:
以下代码应该通过 gmail 发送电子邮件,但它给出了以下错误:
在我的 gmail 帐户上,我收到一条消息,指出登录被阻止,我应该使用像 gmail 这样的安全应用来访问我的帐户。源码如下图:
public void doSendMail(){
username = txtFrom.getText();
password= new String(txtPassword.getPassword());
to = txtTo.getText();
subject = txtSubject.getText();
email_body = jTextArea1.getText();
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username, password);
}
}
);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
message.setSubject(subject);
message.setText(email_body);
Transport.send(message);
JOptionPane.showMessageDialog(this, "Message Sent!","Sent",JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.toString());
}
}
我可以对代码做些什么以使其通过 gmail 发送邮件?
【问题讨论】:
-
能否贴出可以复制的错误信息文本?
-
我将 try...catch 块捕获的错误设置为显示在消息对话框中。我相信错误显示在我发布的消息对话框中