[英]sending mail from your computer using java what required?
我有Windows 7系統, 沒有安裝smtp服務器,只有java。 我想通過gmail從程序中發送郵件。 我已經編寫了一個Java程序來通過smtp連接google,並啟用了telnet選項,但是出現以下錯誤。 我嘗試使用端口465和587,但沒有更改。
錯誤:
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at mail1.SendMailSSL.main(SendMailSSL.java:44)
碼:
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;
public class SendMailSSL {
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.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props,newjavax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mymail@gmail.com","mypassword");
}
} );
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("mymail@gmail.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"to24n@gmail.com"));
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);
}
}
}
誰能告訴我從個人系統發送電子郵件需要做什么? 請分步告訴我,並包括安裝軟件之類的內容,因為我必須為我的項目這樣做。 非常感謝您的回答。 先感謝您。
兩年前,我遇到了同樣的問題,但我不記得所有對我有幫助的事情。
我認為這些帖子會有所幫助:
所需的jar文件:
發送郵件的步驟:
使用Google SMTP服務器端口465
在Windows 7中啟用telnet:
控制面板 > 程序 > 打開或關閉Windows功能
檢查“ Telnet服務器”和“ Telnet客戶端”
確保系統日期正確
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.