嗨我必須用java發郵件。用java發郵件不帶gmail驗證
以下已成功爲我工作。
public class SendMail {
public static void main(String[] args) {
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("[email protected]","xxxxx!1");
}
});
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);
}
}
}
這裏我提到了gmail的用戶名和密碼。但我必須在我的代碼中發送電子郵件,而不使用用戶名和密碼。我該如何開發這個。請幫幫我。
現在我要做的第一件事就是在這裏張貼後更改我的Gmail密碼!爲了提高安全性,請打開2因素身份驗證。 – jlordo
是的,當在stackoverflow上詢問時,您應該始終混淆敏感數據(公司名稱等)。 – jabal
脫離主題:你不應該寫一封電子郵件給垃圾郵件發送者 – kromit