簡體   English   中英

在Java中發送郵件異常

[英]Sending mail in java exception

我試圖用Java發送郵件,我用google搜索了一下代碼,然后嘗試運行它,但是它給了我這個例外:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
    java.net.ConnectException: Connection timed out: connect

這是我的代碼:

public class SendMail {

  public static void Send(String username, String email) {

  Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", true); // added this line
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.user", "username@gmail.com");
    props.put("mail.smtp.password", "password");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", true);

    Session session = Session.getInstance(props, null);
    MimeMessage message = new MimeMessage(session);

    System.out.println("Port: " + session.getProperty("mail.smtp.port"));
    try {
      InternetAddress from = new InternetAddress("username");
      message.setSubject("Yes we can");
      message.setFrom(from);
      message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
      Multipart multipart = new MimeMultipart("alternative");
      BodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText("your username :" + username);
      multipart.addBodyPart(messageBodyPart);
      messageBodyPart = new MimeBodyPart();
      String htmlMessage = "Our html text";
      messageBodyPart.setContent(htmlMessage, "text/html");
      multipart.addBodyPart(messageBodyPart);
      message.setContent(multipart);
      Transport transport = session.getTransport("smtp");
      transport.connect("smtp.gmail.com", "username@gmail.com",  "password");
      System.out.println("Transport: " + transport.toString());
      transport.sendMessage(message, message.getAllRecipients());

    } catch (AddressException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (MessagingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

我在這里使用的配置對我來說還可以

mail.from = myMail
mail.transport.protocol = smtp
mail.smtp.host = smtp.gmail.com
mail.smtp.timeout = 30000
mail.smtp.starttls.enable = true
mail.smtp.auth = true
mail.debug = false

當我發送消息時

Authenticator auth = new MailAuthenticator(myMail , myPass);
Session session = Session.getDefaultInstance(properties, auth);

這是一些對我有用的Gmail設置:

//these are fed into the Properties object below:
mail.smtp.starttls.enable = true
mail.transport.protocol   = smtp
mail.smtp.auth            = true

和一些Java:

Properties properties = ...

javax.mail.Session session = javax.mail.Session.getInstance(properties, null);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", username, password);

這對我有用。

public class SendEmail
{
   public static void main(String [] args)
  {    
     String to = "to@gmail.com";
     String from = "from@gmail.com";
     String host = "localhost";
     Properties properties = System.getProperties();
     properties.setProperty("mail.smtp.host", host);
     Session session = Session.getDefaultInstance(properties);

     try{
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));
        message.setSubject("Subject");
        message.setText("Message");
        Transport.send(message);
        System.out.println("Message sent.");
     }catch (MessagingException mex) {
        mex.printStackTrace();
     }
   }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM